diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php index 3cadbc2e..7d57e15c 100644 --- a/src/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -3,7 +3,6 @@ namespace App\Controller; use Slim\Container; -use Slim\Http\Request; use Slim\Http\Response; abstract class BaseController @@ -13,35 +12,7 @@ abstract class BaseController */ protected $container; - /** - * @var Request $request - */ - protected $request; - - /** - * @var Response $response - */ - protected $response; - - /** - * @var array - */ - protected $args; - - protected function setParams(Request $request, Response $response, array $args) - { - $this->request = $request; - $this->response = $response; - $this->args = $args; - } - - /** - * @param string $status - * @param mixed $message - * @param int $code - * @return Response - */ - protected function jsonResponse(string $status, $message, int $code): Response + protected function jsonResponse(Response $response, string $status, $message, int $code): Response { $result = [ 'code' => $code, @@ -49,6 +20,6 @@ protected function jsonResponse(string $status, $message, int $code): Response 'message' => $message, ]; - return $this->response->withJson($result, $code, JSON_PRETTY_PRINT); + return $response->withJson($result, $code, JSON_PRETTY_PRINT); } } diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index bd1cebb0..f0de0cc4 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -8,7 +8,7 @@ class DefaultController extends BaseController { - const API_VERSION = '0.31.0'; + const API_VERSION = '0.32.0'; public function __construct(Container $container) { @@ -17,7 +17,6 @@ public function __construct(Container $container) public function getHelp(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $url = getenv('APP_DOMAIN'); $endpoints = [ 'tasks' => $url . '/api/v1/tasks', @@ -33,12 +32,11 @@ public function getHelp(Request $request, Response $response, array $args): Resp 'timestamp' => time(), ]; - return $this->jsonResponse('success', $message, 200); + return $this->jsonResponse($response, 'success', $message, 200); } public function getStatus(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $userService = $this->container->get('user_service'); $noteService = $this->container->get('note_service'); $taskService = $this->container->get('task_service'); @@ -53,6 +51,6 @@ public function getStatus(Request $request, Response $response, array $args): Re 'timestamp' => time(), ]; - return $this->jsonResponse('success', $status, 200); + return $this->jsonResponse($response, 'success', $status, 200); } } diff --git a/src/Controller/Note/CreateNote.php b/src/Controller/Note/CreateNote.php index 4696fc8d..22763967 100644 --- a/src/Controller/Note/CreateNote.php +++ b/src/Controller/Note/CreateNote.php @@ -9,10 +9,9 @@ class CreateNote extends BaseNote { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); $note = $this->getNoteService()->createNote($input); - return $this->jsonResponse('success', $note, 201); + return $this->jsonResponse($response, 'success', $note, 201); } } diff --git a/src/Controller/Note/DeleteNote.php b/src/Controller/Note/DeleteNote.php index 89ab8d3e..b6b98d90 100644 --- a/src/Controller/Note/DeleteNote.php +++ b/src/Controller/Note/DeleteNote.php @@ -9,9 +9,8 @@ class DeleteNote extends BaseNote { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); - $this->getNoteService()->deleteNote((int) $this->args['id']); + $this->getNoteService()->deleteNote((int) $args['id']); - return $this->jsonResponse('success', null, 204); + return $this->jsonResponse($response, 'success', null, 204); } } diff --git a/src/Controller/Note/GetAllNotes.php b/src/Controller/Note/GetAllNotes.php index b1b77c56..3be604bd 100644 --- a/src/Controller/Note/GetAllNotes.php +++ b/src/Controller/Note/GetAllNotes.php @@ -9,9 +9,8 @@ class GetAllNotes extends BaseNote { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $notes = $this->getNoteService()->getNotes(); - return $this->jsonResponse('success', $notes, 200); + return $this->jsonResponse($response, 'success', $notes, 200); } } diff --git a/src/Controller/Note/GetOneNote.php b/src/Controller/Note/GetOneNote.php index 2c53ad22..1c30620c 100644 --- a/src/Controller/Note/GetOneNote.php +++ b/src/Controller/Note/GetOneNote.php @@ -9,9 +9,8 @@ class GetOneNote extends BaseNote { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); - $note = $this->getNoteService()->getNote((int) $this->args['id']); + $note = $this->getNoteService()->getNote((int) $args['id']); - return $this->jsonResponse('success', $note, 200); + return $this->jsonResponse($response, 'success', $note, 200); } } diff --git a/src/Controller/Note/SearchNotes.php b/src/Controller/Note/SearchNotes.php index 5db303e9..bf4dfe2f 100644 --- a/src/Controller/Note/SearchNotes.php +++ b/src/Controller/Note/SearchNotes.php @@ -9,9 +9,8 @@ class SearchNotes extends BaseNote { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); - $notes = $this->getNoteService()->searchNotes($this->args['query']); + $notes = $this->getNoteService()->searchNotes($args['query']); - return $this->jsonResponse('success', $notes, 200); + return $this->jsonResponse($response, 'success', $notes, 200); } } diff --git a/src/Controller/Note/UpdateNote.php b/src/Controller/Note/UpdateNote.php index a74c0038..a322d6c7 100644 --- a/src/Controller/Note/UpdateNote.php +++ b/src/Controller/Note/UpdateNote.php @@ -9,10 +9,9 @@ class UpdateNote extends BaseNote { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); - $note = $this->getNoteService()->updateNote($input, (int) $this->args['id']); + $note = $this->getNoteService()->updateNote($input, (int) $args['id']); - return $this->jsonResponse('success', $note, 200); + return $this->jsonResponse($response, 'success', $note, 200); } } diff --git a/src/Controller/Task/CreateTask.php b/src/Controller/Task/CreateTask.php index 8004925b..9d088ec3 100644 --- a/src/Controller/Task/CreateTask.php +++ b/src/Controller/Task/CreateTask.php @@ -9,10 +9,9 @@ class CreateTask extends BaseTask { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); $task = $this->getTaskService()->createTask($input); - return $this->jsonResponse('success', $task, 201); + return $this->jsonResponse($response, 'success', $task, 201); } } diff --git a/src/Controller/Task/DeleteTask.php b/src/Controller/Task/DeleteTask.php index 209fbce1..0fc83d3b 100644 --- a/src/Controller/Task/DeleteTask.php +++ b/src/Controller/Task/DeleteTask.php @@ -9,12 +9,11 @@ class DeleteTask extends BaseTask { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); - $taskId = (int) $this->args['id']; + $taskId = (int) $args['id']; $userId = (int) $input['decoded']->sub; $task = $this->getTaskService()->deleteTask($taskId, $userId); - return $this->jsonResponse('success', $task, 204); + return $this->jsonResponse($response, 'success', $task, 204); } } diff --git a/src/Controller/Task/GetAllTasks.php b/src/Controller/Task/GetAllTasks.php index 7e434c41..fb17c0e4 100644 --- a/src/Controller/Task/GetAllTasks.php +++ b/src/Controller/Task/GetAllTasks.php @@ -9,11 +9,10 @@ class GetAllTasks extends BaseTask { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); $userId = (int) $input['decoded']->sub; $tasks = $this->getTaskService()->getTasks($userId); - return $this->jsonResponse('success', $tasks, 200); + return $this->jsonResponse($response, 'success', $tasks, 200); } } diff --git a/src/Controller/Task/GetOneTask.php b/src/Controller/Task/GetOneTask.php index 8046492c..e96fdc98 100644 --- a/src/Controller/Task/GetOneTask.php +++ b/src/Controller/Task/GetOneTask.php @@ -9,12 +9,11 @@ class GetOneTask extends BaseTask { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); - $taskId = (int) $this->args['id']; + $taskId = (int) $args['id']; $userId = (int) $input['decoded']->sub; $task = $this->getTaskService()->getTask($taskId, $userId); - return $this->jsonResponse('success', $task, 200); + return $this->jsonResponse($response, 'success', $task, 200); } } diff --git a/src/Controller/Task/SearchTasks.php b/src/Controller/Task/SearchTasks.php index b31d9630..3a2c3973 100644 --- a/src/Controller/Task/SearchTasks.php +++ b/src/Controller/Task/SearchTasks.php @@ -9,16 +9,15 @@ class SearchTasks extends BaseTask { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); $userId = (int) $input['decoded']->sub; $query = ''; - if (isset($this->args['query'])) { - $query = $this->args['query']; + if (isset($args['query'])) { + $query = $args['query']; } $status = $request->getParam('status', null); $tasks = $this->getTaskService()->searchTasks($query, $userId, $status); - return $this->jsonResponse('success', $tasks, 200); + return $this->jsonResponse($response, 'success', $tasks, 200); } } diff --git a/src/Controller/Task/UpdateTask.php b/src/Controller/Task/UpdateTask.php index 244b32e6..149c42aa 100644 --- a/src/Controller/Task/UpdateTask.php +++ b/src/Controller/Task/UpdateTask.php @@ -9,10 +9,9 @@ class UpdateTask extends BaseTask { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); - $task = $this->getTaskService()->updateTask($input, (int) $this->args['id']); + $task = $this->getTaskService()->updateTask($input, (int) $args['id']); - return $this->jsonResponse('success', $task, 200); + return $this->jsonResponse($response, 'success', $task, 200); } } diff --git a/src/Controller/User/BaseUser.php b/src/Controller/User/BaseUser.php index 742f4175..822e9d0e 100644 --- a/src/Controller/User/BaseUser.php +++ b/src/Controller/User/BaseUser.php @@ -19,9 +19,9 @@ protected function getUserService(): UserService return $this->container->get('user_service'); } - protected function checkUserPermissions($userIdLogged) + protected function checkUserPermissions($userId, $userIdLogged) { - if ($this->args['id'] != $userIdLogged) { + if ($userId != $userIdLogged) { throw new UserException('User permission failed.', 400); } } diff --git a/src/Controller/User/CreateUser.php b/src/Controller/User/CreateUser.php index c064a35c..479fe6e9 100644 --- a/src/Controller/User/CreateUser.php +++ b/src/Controller/User/CreateUser.php @@ -9,10 +9,9 @@ class CreateUser extends BaseUser { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); $user = $this->getUserService()->createUser($input); - return $this->jsonResponse('success', $user, 201); + return $this->jsonResponse($response, 'success', $user, 201); } } diff --git a/src/Controller/User/DeleteUser.php b/src/Controller/User/DeleteUser.php index 8b8b8700..d918c14d 100644 --- a/src/Controller/User/DeleteUser.php +++ b/src/Controller/User/DeleteUser.php @@ -9,12 +9,11 @@ class DeleteUser extends BaseUser { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); $userIdLogged = $input['decoded']->sub; - $this->checkUserPermissions($userIdLogged); - $user = $this->getUserService()->deleteUser((int) $this->args['id']); + $this->checkUserPermissions($args['id'], $userIdLogged); + $user = $this->getUserService()->deleteUser((int) $args['id']); - return $this->jsonResponse('success', $user, 204); + return $this->jsonResponse($response, 'success', $user, 204); } } diff --git a/src/Controller/User/GetAllUsers.php b/src/Controller/User/GetAllUsers.php index 202179bb..a9516c07 100644 --- a/src/Controller/User/GetAllUsers.php +++ b/src/Controller/User/GetAllUsers.php @@ -9,9 +9,8 @@ class GetAllUsers extends BaseUser { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $users = $this->getUserService()->getUsers(); - return $this->jsonResponse('success', $users, 200); + return $this->jsonResponse($response, 'success', $users, 200); } } diff --git a/src/Controller/User/GetOneUser.php b/src/Controller/User/GetOneUser.php index 501085bc..6543c60c 100644 --- a/src/Controller/User/GetOneUser.php +++ b/src/Controller/User/GetOneUser.php @@ -9,9 +9,8 @@ class GetOneUser extends BaseUser { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); - $user = $this->getUserService()->getUser((int) $this->args['id']); + $user = $this->getUserService()->getUser((int) $args['id']); - return $this->jsonResponse('success', $user, 200); + return $this->jsonResponse($response, 'success', $user, 200); } } diff --git a/src/Controller/User/LoginUser.php b/src/Controller/User/LoginUser.php index fdd52938..fa405f7e 100644 --- a/src/Controller/User/LoginUser.php +++ b/src/Controller/User/LoginUser.php @@ -9,13 +9,12 @@ class LoginUser extends BaseUser { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); $jwt = $this->getUserService()->loginUser($input); $message = [ 'Authorization' => 'Bearer ' . $jwt, ]; - return $this->jsonResponse('success', $message, 200); + return $this->jsonResponse($response, 'success', $message, 200); } } diff --git a/src/Controller/User/SearchUsers.php b/src/Controller/User/SearchUsers.php index a2d894a6..7a890ad9 100644 --- a/src/Controller/User/SearchUsers.php +++ b/src/Controller/User/SearchUsers.php @@ -9,9 +9,8 @@ class SearchUsers extends BaseUser { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); - $users = $this->getUserService()->searchUsers($this->args['query']); + $users = $this->getUserService()->searchUsers($args['query']); - return $this->jsonResponse('success', $users, 200); + return $this->jsonResponse($response, 'success', $users, 200); } } diff --git a/src/Controller/User/UpdateUser.php b/src/Controller/User/UpdateUser.php index c9b8b00e..a5ba7a7a 100644 --- a/src/Controller/User/UpdateUser.php +++ b/src/Controller/User/UpdateUser.php @@ -9,12 +9,11 @@ class UpdateUser extends BaseUser { public function __invoke(Request $request, Response $response, array $args): Response { - $this->setParams($request, $response, $args); $input = $request->getParsedBody(); $userIdLogged = $input['decoded']->sub; - $this->checkUserPermissions($userIdLogged); - $user = $this->getUserService()->updateUser($input, (int) $this->args['id']); + $this->checkUserPermissions($args['id'], $userIdLogged); + $user = $this->getUserService()->updateUser($input, (int) $args['id']); - return $this->jsonResponse('success', $user, 200); + return $this->jsonResponse($response, 'success', $user, 200); } }