Moving routes and functions to controller
This commit is contained in:
parent
dc64198558
commit
a7c56f3c61
|
@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||||
use App\Validator\SmsValidator;
|
use App\Validator\SmsValidator;
|
||||||
use App\Factory\SmsFactory;
|
use App\Factory\SmsFactory;
|
||||||
use Propel\Runtime\Propel;
|
use Propel\Runtime\Propel;
|
||||||
|
use App\Controller\Controller;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class Application.
|
* class Application.
|
||||||
|
@ -22,9 +23,9 @@ class Application extends SilexApplication
|
||||||
protected $rootDir;
|
protected $rootDir;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Configures the application.
|
* Init the application.
|
||||||
*/
|
*/
|
||||||
public function configure()
|
public function init()
|
||||||
{
|
{
|
||||||
Propel::init($this->getRootDir().'etc/propel/config.php');
|
Propel::init($this->getRootDir().'etc/propel/config.php');
|
||||||
$users = json_decode(file_get_contents($this->getRootDir().'/etc/security/users.json'), true);
|
$users = json_decode(file_get_contents($this->getRootDir().'/etc/security/users.json'), true);
|
||||||
|
@ -49,6 +50,8 @@ class Application extends SilexApplication
|
||||||
'message' => $e->getMessage(),
|
'message' => $e->getMessage(),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -74,4 +77,12 @@ class Application extends SilexApplication
|
||||||
{
|
{
|
||||||
return $this->rootDir;
|
return $this->rootDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addController(Controller $controller)
|
||||||
|
{
|
||||||
|
$controller->setApp($this);
|
||||||
|
$controller->registerRoutes();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
51
src/App/Controller/Controller.php
Normal file
51
src/App/Controller/Controller.php
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Application;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class Controller.
|
||||||
|
*
|
||||||
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
|
*/
|
||||||
|
abstract class Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Application
|
||||||
|
*/
|
||||||
|
protected $app;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set the value of "app".
|
||||||
|
*
|
||||||
|
* @param Application $app
|
||||||
|
*
|
||||||
|
* @return Controller
|
||||||
|
*/
|
||||||
|
public function setApp($app)
|
||||||
|
{
|
||||||
|
$this->app = $app;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the value of "app".
|
||||||
|
*
|
||||||
|
* @return Application
|
||||||
|
*/
|
||||||
|
public function getApp()
|
||||||
|
{
|
||||||
|
return $this->app;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers routes.
|
||||||
|
*
|
||||||
|
* @param Application $app
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
abstract public function registerRoutes();
|
||||||
|
}
|
125
src/App/Controller/SmsController.php
Normal file
125
src/App/Controller/SmsController.php
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class SmsController.
|
||||||
|
*
|
||||||
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
|
*/
|
||||||
|
class SmsController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function registerRoutes()
|
||||||
|
{
|
||||||
|
$this->app
|
||||||
|
->post('/api/sms/create', [$this, 'create'])
|
||||||
|
->bind('api_sms_create');
|
||||||
|
|
||||||
|
$this->app
|
||||||
|
->delete('/api/sms/delete/{id}', [$this, 'delete'])
|
||||||
|
->bind('api_sms_delete')
|
||||||
|
->assert('id', '\d+')
|
||||||
|
->convert('id', function ($value) {
|
||||||
|
return (int) $value;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->app
|
||||||
|
->get('/api/sms/list', [$this, 'list'])
|
||||||
|
->bind('api_sms_list');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create action.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
||||||
|
*
|
||||||
|
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
||||||
|
*/
|
||||||
|
public function create(Request $request)
|
||||||
|
{
|
||||||
|
$app = $this->app;
|
||||||
|
|
||||||
|
if (!$app['sms.validator']->validateRequest($request)) {
|
||||||
|
return $app->abort(422, 'Invalid request.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$persisted = $app['sms.factory']->createByRequest($request);
|
||||||
|
|
||||||
|
if (!$persisted) {
|
||||||
|
return $app->abort(500, 'An error occured.');
|
||||||
|
} else {
|
||||||
|
return $app->json([
|
||||||
|
'status' => true,
|
||||||
|
'code' => 201,
|
||||||
|
'message' => 'Resource created.',
|
||||||
|
], 201);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete action.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @param int $id
|
||||||
|
*
|
||||||
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
||||||
|
*
|
||||||
|
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
||||||
|
*/
|
||||||
|
public function delete(Request $request, $id)
|
||||||
|
{
|
||||||
|
$app = $this->app;
|
||||||
|
|
||||||
|
$object = $app['sms.factory']->get($id);
|
||||||
|
|
||||||
|
if (!$object) {
|
||||||
|
return $app->json([
|
||||||
|
'status' => false,
|
||||||
|
'code' => 404,
|
||||||
|
'message' => 'Resource not found.',
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$app['sms.factory']->remove($object);
|
||||||
|
|
||||||
|
return $app->json([
|
||||||
|
'status' => true,
|
||||||
|
'code' => 204,
|
||||||
|
'message' => 'Resource removed.',
|
||||||
|
], 204);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List action.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
||||||
|
*/
|
||||||
|
public function list()
|
||||||
|
{
|
||||||
|
$app = $this->app;
|
||||||
|
|
||||||
|
$objects = $app['sms.factory']->getAll()->getData();
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
foreach ($objects as $object) {
|
||||||
|
$data[] = [
|
||||||
|
'id' => $object->getId(),
|
||||||
|
'sender' => $object->getSender(),
|
||||||
|
'message' => $object->getMessage(),
|
||||||
|
'received_at' => (int) $object->getTime(),
|
||||||
|
'notified_at' => $object->getCreatedAt()->getTimestamp(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $app->json($data);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,75 +2,14 @@
|
||||||
|
|
||||||
use App\Application;
|
use App\Application;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use App\Controller\SmsController;
|
||||||
|
|
||||||
require_once __DIR__.'/../vendor/autoload.php';
|
require_once __DIR__.'/../vendor/autoload.php';
|
||||||
|
|
||||||
$app = new Application();
|
$app = new Application();
|
||||||
$app->setRootDir(__DIR__.'/../')->configure();
|
|
||||||
|
|
||||||
$app
|
$app
|
||||||
->post('/api/sms/create', function (Request $request) use ($app) {
|
->setRootDir(__DIR__.'/../')
|
||||||
if (!$app['sms.validator']->validateRequest($request)) {
|
->init()
|
||||||
return $app->abort(422, 'Invalid request.');
|
->addController(new SmsController())
|
||||||
}
|
->run();
|
||||||
|
|
||||||
$persisted = $app['sms.factory']->createByRequest($request);
|
|
||||||
|
|
||||||
if (!$persisted) {
|
|
||||||
return $app->abort(500, 'An error occured.');
|
|
||||||
} else {
|
|
||||||
return $app->json([
|
|
||||||
'status' => true,
|
|
||||||
'code' => 201,
|
|
||||||
'message' => 'Resource created.',
|
|
||||||
], 201);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
->bind('api_sms_create');
|
|
||||||
|
|
||||||
$app
|
|
||||||
->delete('/api/sms/delete/{id}', function (Request $request, $id) use ($app) {
|
|
||||||
$object = $app['sms.factory']->get($id);
|
|
||||||
|
|
||||||
if (!$object) {
|
|
||||||
return $app->json([
|
|
||||||
'status' => false,
|
|
||||||
'code' => 404,
|
|
||||||
'message' => 'Resource not found.',
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$app['sms.factory']->remove($object);
|
|
||||||
|
|
||||||
return $app->json([
|
|
||||||
'status' => true,
|
|
||||||
'code' => 204,
|
|
||||||
'message' => 'Resource removed.',
|
|
||||||
], 204);
|
|
||||||
})
|
|
||||||
->bind('api_sms_delete')
|
|
||||||
->assert('id', '\d+')
|
|
||||||
->convert('id', function ($value) {
|
|
||||||
return (int) $value;
|
|
||||||
});
|
|
||||||
|
|
||||||
$app
|
|
||||||
->get('/api/sms/list', function (Request $request) use ($app) {
|
|
||||||
$objects = $app['sms.factory']->getAll()->getData();
|
|
||||||
$data = [];
|
|
||||||
|
|
||||||
foreach ($objects as $object) {
|
|
||||||
$data[] = [
|
|
||||||
'id' => $object->getId(),
|
|
||||||
'sender' => $object->getSender(),
|
|
||||||
'message' => $object->getMessage(),
|
|
||||||
'received_at' => (int) $object->getTime(),
|
|
||||||
'notified_at' => $object->getCreatedAt()->getTimestamp(),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $app->json($data);
|
|
||||||
})
|
|
||||||
->bind('api_sms_list');
|
|
||||||
|
|
||||||
$app->run();
|
|
||||||
|
|
Loading…
Reference in a new issue