From a7c56f3c616b24c4f9eca11df528baf1e9a35c4e Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 16 Jan 2018 09:14:43 +0100 Subject: [PATCH] Moving routes and functions to controller --- src/App/Application.php | 15 +++- src/App/Controller/Controller.php | 51 +++++++++++ src/App/Controller/SmsController.php | 125 +++++++++++++++++++++++++++ web/index.php | 71 ++------------- 4 files changed, 194 insertions(+), 68 deletions(-) create mode 100644 src/App/Controller/Controller.php create mode 100644 src/App/Controller/SmsController.php diff --git a/src/App/Application.php b/src/App/Application.php index d9e6b02..8d96b7a 100644 --- a/src/App/Application.php +++ b/src/App/Application.php @@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\Request; use App\Validator\SmsValidator; use App\Factory\SmsFactory; use Propel\Runtime\Propel; +use App\Controller\Controller; /** * class Application. @@ -22,9 +23,9 @@ class Application extends SilexApplication protected $rootDir; /* - * Configures the application. + * Init the application. */ - public function configure() + public function init() { Propel::init($this->getRootDir().'etc/propel/config.php'); $users = json_decode(file_get_contents($this->getRootDir().'/etc/security/users.json'), true); @@ -49,6 +50,8 @@ class Application extends SilexApplication 'message' => $e->getMessage(), ]); }); + + return $this; } /* @@ -74,4 +77,12 @@ class Application extends SilexApplication { return $this->rootDir; } + + public function addController(Controller $controller) + { + $controller->setApp($this); + $controller->registerRoutes(); + + return $this; + } } diff --git a/src/App/Controller/Controller.php b/src/App/Controller/Controller.php new file mode 100644 index 0000000..ee74e4e --- /dev/null +++ b/src/App/Controller/Controller.php @@ -0,0 +1,51 @@ + + */ +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(); +} diff --git a/src/App/Controller/SmsController.php b/src/App/Controller/SmsController.php new file mode 100644 index 0000000..5db8b76 --- /dev/null +++ b/src/App/Controller/SmsController.php @@ -0,0 +1,125 @@ + + */ +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); + } +} diff --git a/web/index.php b/web/index.php index 54b35ab..0755cd5 100644 --- a/web/index.php +++ b/web/index.php @@ -2,75 +2,14 @@ use App\Application; use Symfony\Component\HttpFoundation\Request; +use App\Controller\SmsController; require_once __DIR__.'/../vendor/autoload.php'; $app = new Application(); -$app->setRootDir(__DIR__.'/../')->configure(); $app - ->post('/api/sms/create', function (Request $request) use ($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); - } - }) - ->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(); + ->setRootDir(__DIR__.'/../') + ->init() + ->addController(new SmsController()) + ->run();