Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
Initial Import
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarany committed May 23, 2014
1 parent dcec8bc commit 039f965
Show file tree
Hide file tree
Showing 31 changed files with 1,552 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
/.idea
/vendor
/config/Config.php
6 changes: 6 additions & 0 deletions app/Barany/Command.php
@@ -0,0 +1,6 @@
<?php
namespace Barany;

interface Command {
public function execute();
}
87 changes: 87 additions & 0 deletions app/Barany/Controller/Api.php
@@ -0,0 +1,87 @@
<?php
namespace Barany\Controller;

use Barany\Core\AppController;
use Barany\Model\Account;
use Barany\Model\Institution;
use Barany\Model\User;
use Doctrine\ORM\Query\ResultSetMapping;
use Httpful\Request;
use Httpful\Response;

class Api extends AppController {
public function institutions($httpRequest) {
$rsm = new ResultSetMapping();
$rsm
->addEntityResult('Barany\Model\Institution', 'i')
->addFieldResult('i', 'id', 'id')
->addFieldResult('i', 'name', 'name')
->addFieldResult('i', 'code', 'code');
$sql = "SELECT i.id, i.name, i.code FROM institution i FORCE INDEX(name_idx) ORDER BY i.name ASC";
$query = $this
->getEntityManager()
->createNativeQuery($sql, $rsm);

/** @var Institution[] $institutions */
$institutions = $query->getResult();
$this->renderJson($institutions);
}

public function accounts($httpRequest) {
//@todo: Use session uid
$userId = 1;
/** @var User $user */
$user = $this->getEntityManager()->find('Barany\Model\User', $userId);
$this->renderJson($user ? $user->getAccounts() : []);
}

public function account($httpRequest) {
/** @var Account $account */
$account = $this->getEntityManager()->find('Barany\Model\Account', $httpRequest->account_id);

$plaidData = $this->getPlaidAccount($account);

$accounts = [];
foreach ($plaidData->body->accounts as $bankAccount) {
$accounts[$bankAccount->_id] = [
'account' => $bankAccount,
'transactions' => [],
];
}
foreach ($plaidData->body->transactions as $transaction) {
$accounts[$transaction->_account]['transactions'][] = $transaction;
}

$this->renderJson(
[
'institution' => $account->getInstitution()->toApi(),
'accounts' => array_values($accounts),
]
);
}

/**
* @param Account $account
* @return Response
*/
private function getPlaidAccount(Account $account) {
$access_token = $account->getAccessToken();
$params = array_merge(
$this->getAppConfig()->getPlaidApiCredentials(),
array(
'access_token' => $access_token,
)
);
$concatedParams = array();
foreach ($params as $k => $v) {
$concatedParams[] = "$k=$v";
}
$request = Request::get(
$this->getAppConfig()->getPlaidApiEndpoint() . 'connect?' . implode('&', $concatedParams),
'application/json'
)
->expects('application/json');

return $request->send();
}
}
71 changes: 71 additions & 0 deletions app/Barany/Controller/Index.php
@@ -0,0 +1,71 @@
<?php
namespace Barany\Controller;

use Barany\Core\AppController;
use Httpful\Request;

class Index extends AppController {
public function index() {
$this->render();
}
public function connect() {
exit;
$request = Request::post(
$this->getAppConfig()->getPlaidApiEndpoint() . 'connect',
array_merge(
$this->getAppConfig()->getPlaidApiCredentials(),
array(
'credentials' =>array(
// 'username' => 'plaid_test',
// 'password' => 'plaid_good',
),
// 'email' => 'email-test@plaid.com',
'type' => 'citi',
'options' => array(
'login' => true,
),
)
),
'application/json'
)
->expects('application/json');


$response = $request->send();


echo '<pre>';
var_dump($request->serialized_payload);
print_r($response->headers);
print_r($response->body);
print_r($response);
echo '</pre>';
}

public function connectStep() {
exit;
$request = Request::post(
$this->getAppConfig()->getPlaidApiEndpoint() . 'connect/step',
array_merge(
$this->getAppConfig()->getPlaidApiCredentials(),
array(
'access_token' => '',
'mfa' => '',
)
),
'application/json'
)
->expects('application/json');


$response = $request->send();


echo '<pre>';
var_dump($request->serialized_payload);
print_r($response->headers);
print_r($response->body);
print_r($response);
echo '</pre>';
}
}
16 changes: 16 additions & 0 deletions app/Barany/Core/AppConfig.php
@@ -0,0 +1,16 @@
<?php
namespace Barany\Core;

abstract class AppConfig {
public abstract function getPlaidApiEndpoint();
public abstract function getPlaidApiClientId();
public abstract function getPlaidApiSecret();
public abstract function getDatabaseCredentials();

public function getPlaidApiCredentials() {
return array(
'client_id' => $this->getPlaidApiClientId(),
'secret' => $this->getPlaidApiSecret(),
);
}
}
51 changes: 51 additions & 0 deletions app/Barany/Core/AppController.php
@@ -0,0 +1,51 @@
<?php
namespace Barany\Core;

use Barany\Model\Exportable;
use Barany\Core\Http\Kernel;
use Doctrine\Common\Collections\Collection;

class AppController {
/**
* @var Kernel
*/
private $kernel;

private $view;

public function __construct(Kernel $kernel, $view) {
$this->kernel = $kernel;
$this->view = new View($view);
}

protected function getAppConfig() {
return $this->kernel->getAppConfig();
}

protected function getEntityManager() {
return $this->kernel->getEntityManager();
}

protected function render() {
$this->view->render();
}

/**
* @param mixed $data
*/
protected function renderJson($data = null) {
header('Content-Type: application/json');
if (null === $data) {
return;
}
if (!is_array($data) && !$data instanceof Collection) {
echo json_encode($data);
return;
}
$exportedData = [];
foreach ($data as $k => $v) {
$exportedData[$k] = $v instanceof Exportable ? $v->toApi() : $v;
}
echo json_encode($exportedData);
}
}
53 changes: 53 additions & 0 deletions app/Barany/Core/Http/Kernel.php
@@ -0,0 +1,53 @@
<?php
namespace Barany\Core\Http;

use Barany\Command;
use Barany\Core\AppConfig;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
use Doctrine\ORM\Tools\Setup;

class Kernel implements Command {
/**
* @var AppConfig
*/
private $appConfig;

/**
* @var Router
*/
private $router;

/**
* @var EntityManager
*/
private $entityManager;

public function __construct(AppConfig $appConfig) {
$this->appConfig = $appConfig;
$this->router = new Router($this);
$this->setupDoctrine();
}

private function setupDoctrine() {
$paths = array(ROOT . '/app/Barany/Model');
//@todo: Set via config
$isDevMode = true;

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$config->setNamingStrategy(new UnderscoreNamingStrategy());
$this->entityManager = EntityManager::create($this->appConfig->getDatabaseCredentials(), $config);
}

public function execute() {
$this->router->dispatch();
}

public function getAppConfig() {
return $this->appConfig;
}

public function getEntityManager() {
return $this->entityManager;
}
}
47 changes: 47 additions & 0 deletions app/Barany/Core/Http/Router.php
@@ -0,0 +1,47 @@
<?php
namespace Barany\Core\Http;

use Barany\Core\AppController;
use Klein\Klein;

class Router {
/**
* @var \Klein\Klein
*/
private $klein;

/**
* @var Kernel
*/
private $kernel;

public function __construct(Kernel $kernel) {
$this->kernel = $kernel;
$this->klein = new Klein();
$this->load();
}

public function dispatch() {
$this->klein->dispatch();
}

/**
* @todo: don't eagerly create a new object for each route
*/
private function load() {
$this->klein->get('/', $this->toCallable('Index', 'index'));
// $this->klein->get('/connect', $this->toCallable('Index', 'connect'));
// $this->klein->get('/connect/step', $this->toCallable('Index', 'connectStep'));

$this->klein->get('/api/institutions', $this->toCallable('Api', 'institutions'));
$this->klein->get('/api/accounts', $this->toCallable('Api', 'accounts'));
$this->klein->get('/api/account/[:account_id]', $this->toCallable('Api', 'account'));
}

private function toCallable($controller, $action) {
$class = '\Barany\Controller\\' . $controller;
/** @var AppController $controllerObject */
$controllerObject = new $class($this->kernel, $controller . '/' . $action);
return array($controllerObject, $action);
}
}
30 changes: 30 additions & 0 deletions app/Barany/Core/View.php
@@ -0,0 +1,30 @@
<?php
namespace Barany\Core;

class View {
private $layout = 'default';
private $view = null;

public function __construct($view = null) {
$this->view = $view;
}

public function setLayout($layout) {
$this->layout = $layout;
}

public function setView($view) {
$this->view = $view;
}

public function render() {
if ($this->view === null) {
throw new \RuntimeException('View is null!');
}

$layout = file_get_contents(ROOT . '/view/layouts/' . $this->layout . '.html');
$view = file_get_contents(ROOT . '/view/views/' . $this->view . '.html');

echo str_replace('{{body}}', $view, $layout);
}
}

0 comments on commit 039f965

Please sign in to comment.