Skip to content

Commit

Permalink
Prototype route, from model -> serializer -> controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Jul 3, 2017
1 parent a5e4aff commit d8a7f4a
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 6 deletions.
13 changes: 13 additions & 0 deletions bootstrap.php
Expand Up @@ -8,6 +8,7 @@
require_once __DIR__ . '/vendor/autoload.php';

use Aura\Di\ContainerBuilder;
use Aura\Sql\ExtendedPdo;
use AvalancheDevelopment\Talus\Talus;

// load the config for the application
Expand Down Expand Up @@ -35,6 +36,18 @@
new DateTimeZone('America/Phoenix')
));

// set up db and models
$di->set('dbal', $di->lazyNew(
'Aura\Sql\ExtendedPdo',
(array) $config->database
));
$di->types['Aura\Sql\ExtendedPdo'] = $di->lazyGet('dbal');

$di->set('typeModel', $di->lazyNew('Jacobemerick\LifestreamService\Model\Type'));

// set up serializers
$di->set('typeSerializer', $di->lazyNew('Jacobemerick\LifestreamService\Serializer\Type'));

// set up swagger
$handle = fopen(__DIR__ . '/swagger.json', 'r');
$swagger = '';
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -7,7 +7,8 @@
"avalanche-development/talus": "^1.8",
"avalanche-development/swagger-router-middleware": "^1.2",
"container-interop/container-interop": "^1.2",
"aura/di": "^3.2"
"aura/di": "^3.2",
"aura/sql": "^2.5"
},
"require-dev": {
"codeclimate/php-test-reporter": "^0.4.4",
Expand Down
57 changes: 55 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/Controller/Type.php
Expand Up @@ -27,9 +27,11 @@ public function __construct(Container $container)
*/
public function getTypes(Request $request, Response $response)
{
$types = [
'message' => 'hello world',
];
$types = $this->container
->get('typeModel')
->getTypes();

$types = array_map($this->container->get('typeSerializer'), $types);
$types = json_encode($types);
$response->getBody()->write($types);
return $response;
Expand Down
33 changes: 33 additions & 0 deletions src/Model/Type.php
@@ -0,0 +1,33 @@
<?php

namespace Jacobemerick\LifestreamService\Model;

use Aura\Sql\ExtendedPdo;

class Type
{

/** @var ExtendedPdo */
protected $extendedPdo;

/**
* @param ExtendedPdo $extendedPdo
*/
public function __construct(ExtendedPdo $extendedPdo)
{
$this->extendedPdo = $extendedPdo;
}

/**
* @return array
*/
public function getTypes()
{
$query = "
SELECT `id`, `name`
FROM `type`
ORDER BY `name` ASC";

return $this->extendedPdo->fetchAll($query);
}
}
19 changes: 19 additions & 0 deletions src/Serializer/Type.php
@@ -0,0 +1,19 @@
<?php

namespace Jacobemerick\LifestreamService\Serializer;

class Type
{

/**
* @param array $type
* @return array
*/
public function __invoke(array $type)
{
return [
'id' => $type['id'],
'name' => $type['name'],
];
}
}

0 comments on commit d8a7f4a

Please sign in to comment.