diff --git a/extras/bin/restart-db.php b/extras/bin/restart-db.php index f44ec54..d4b62c4 100755 --- a/extras/bin/restart-db.php +++ b/extras/bin/restart-db.php @@ -12,7 +12,11 @@ $pass = $db['pass']; $port = $db['port']; - $pdo = new PDO("mysql:host=${host};port=${port};charset=utf8", $user, $pass); + $pdo = new PDO( + "mysql:host=${host};port=${port};charset=utf8", + $user, + $pass + ); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("DROP DATABASE IF EXISTS ${name}"); diff --git a/rector.php b/rector.php index 3ada77e..f88171a 100644 --- a/rector.php +++ b/rector.php @@ -2,9 +2,11 @@ declare(strict_types=1); +use Rector\Config\RectorConfig; use Rector\Set\ValueObject\SetList; -use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; -return static function (ContainerConfigurator $containerConfigurator): void { - $containerConfigurator->import(SetList::PHP_80); +return static function (RectorConfig $rectorConfig): void { + $rectorConfig->sets([ + SetList::PHP_80 + ]); }; diff --git a/src/App/Repositories.php b/src/App/Repositories.php index 6d85e13..4c8f4ab 100644 --- a/src/App/Repositories.php +++ b/src/App/Repositories.php @@ -7,8 +7,14 @@ use App\Repository\UserRepository; use Psr\Container\ContainerInterface; -$container['user_repository'] = static fn (ContainerInterface $container): UserRepository => new UserRepository($container->get('db')); +$container['user_repository'] = static fn ( + ContainerInterface $container +): UserRepository => new UserRepository($container->get('db')); -$container['task_repository'] = static fn (ContainerInterface $container): TaskRepository => new TaskRepository($container->get('db')); +$container['task_repository'] = static fn ( + ContainerInterface $container +): TaskRepository => new TaskRepository($container->get('db')); -$container['note_repository'] = static fn (ContainerInterface $container): NoteRepository => new NoteRepository($container->get('db')); +$container['note_repository'] = static fn ( + ContainerInterface $container +): NoteRepository => new NoteRepository($container->get('db')); diff --git a/src/App/Settings.php b/src/App/Settings.php index 5f12b95..bfa318e 100644 --- a/src/App/Settings.php +++ b/src/App/Settings.php @@ -4,7 +4,10 @@ return [ 'settings' => [ - 'displayErrorDetails' => filter_var($_SERVER['DISPLAY_ERROR_DETAILS'], FILTER_VALIDATE_BOOLEAN), + 'displayErrorDetails' => filter_var( + $_SERVER['DISPLAY_ERROR_DETAILS'], + FILTER_VALIDATE_BOOLEAN + ), 'db' => [ 'host' => $_SERVER['DB_HOST'], 'name' => $_SERVER['DB_NAME'], diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php index d3847b4..f0ab1c5 100644 --- a/src/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -13,9 +13,6 @@ public function __construct(protected Container $container) { } - /** - * @param array|object|null $message - */ protected function jsonResponse( Response $response, string $status, diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 3fe78f8..a1c2716 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -9,7 +9,7 @@ final class DefaultController extends BaseController { - private const API_VERSION = '2.18.0'; + private const API_VERSION = '2.19.0'; public function getHelp(Request $request, Response $response): Response { diff --git a/src/Controller/Task/Base.php b/src/Controller/Task/Base.php index 686fe31..2ef7dcd 100644 --- a/src/Controller/Task/Base.php +++ b/src/Controller/Task/Base.php @@ -15,6 +15,9 @@ protected function getTaskService(): TaskService return $this->container->get('task_service'); } + /** + * @param array $input + */ protected function getAndValidateUserId(array $input): int { if (isset($input['decoded']) && isset($input['decoded']->sub)) { diff --git a/src/Controller/User/Base.php b/src/Controller/User/Base.php index 4d3e3bf..fda1c7c 100644 --- a/src/Controller/User/Base.php +++ b/src/Controller/User/Base.php @@ -39,13 +39,18 @@ protected function getLoginUserService(): Login return $this->container->get('login_user_service'); } - protected function checkUserPermissions(int $userId, int $userIdLogged): void - { + protected function checkUserPermissions( + int $userId, + int $userIdLogged + ): void { if ($userId !== $userIdLogged) { throw new User('User permission failed.', 400); } } + /** + * @param array $input + */ protected function getAndValidateUserId(array $input): int { if (isset($input['decoded']) && isset($input['decoded']->sub)) { diff --git a/src/Repository/BaseRepository.php b/src/Repository/BaseRepository.php index 99dee77..a7256b3 100644 --- a/src/Repository/BaseRepository.php +++ b/src/Repository/BaseRepository.php @@ -15,6 +15,9 @@ protected function getDb(): \PDO return $this->database; } + /** + * @param array $params + */ protected function getResultsWithPagination( string $query, int $page, @@ -34,7 +37,8 @@ protected function getResultsWithPagination( } /** - * @param array $params + * @param array $params + * * @return array */ protected function getResultByPage( diff --git a/src/Repository/TaskRepository.php b/src/Repository/TaskRepository.php index 35a3d2a..d74540d 100644 --- a/src/Repository/TaskRepository.php +++ b/src/Repository/TaskRepository.php @@ -45,14 +45,13 @@ public function getTasksByPage( $statement->bindParam('description', $params['description']); $statement->bindParam('status', $params['status']); $statement->execute(); - $total = $statement->rowCount(); return $this->getResultsWithPagination( $query, $page, $perPage, $params, - $total + $statement->rowCount() ); } diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index cb85840..67e87fa 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -5,6 +5,7 @@ namespace App\Repository; use App\Entity\User; +use App\Exception\User as UserException; final class UserRepository extends BaseRepository { @@ -99,10 +100,16 @@ public function loginUser(string $email, string $password): User $statement->execute(); $user = $statement->fetchObject(User::class); if (! $user) { - throw new \App\Exception\User('Login failed: Email or password incorrect.', 400); + throw new UserException( + 'Login failed: Email or password incorrect.', + 400 + ); } if (! password_verify($password, $user->getPassword())) { - throw new \App\Exception\User('Login failed: Email or password incorrect.', 400); + throw new UserException( + 'Login failed: Email or password incorrect.', + 400 + ); } return $user; diff --git a/src/Service/Note/Base.php b/src/Service/Note/Base.php index ccb5f1b..7271635 100644 --- a/src/Service/Note/Base.php +++ b/src/Service/Note/Base.php @@ -5,6 +5,7 @@ namespace App\Service\Note; use App\Entity\Note; +use App\Exception\Note as NoteException; use App\Repository\NoteRepository; use App\Service\BaseService; use App\Service\RedisService; @@ -23,7 +24,7 @@ public function __construct( protected static function validateNoteName(string $name): string { if (! v::length(1, 50)->validate($name)) { - throw new \App\Exception\Note('The name of the note is invalid.', 400); + throw new NoteException('The name of the note is invalid.', 400); } return $name; diff --git a/src/Service/Note/Create.php b/src/Service/Note/Create.php index 60b7b60..8034eee 100644 --- a/src/Service/Note/Create.php +++ b/src/Service/Note/Create.php @@ -5,6 +5,7 @@ namespace App\Service\Note; use App\Entity\Note; +use App\Exception\Note as NoteException; final class Create extends Base { @@ -15,11 +16,11 @@ public function create(array $input): object { $data = json_decode((string) json_encode($input), false); if (! isset($data->name)) { - throw new \App\Exception\Note('Invalid data: name is required.', 400); + throw new NoteException('Invalid data: name is required.', 400); } $mynote = new Note(); $mynote->updateName(self::validateNoteName($data->name)); - $description = isset($data->description) ? $data->description : null; + $description = $data->description ?? null; $mynote->updateDescription($description); /** @var Note $note */ $note = $this->noteRepository->createNote($mynote); diff --git a/src/Service/Task/TaskService.php b/src/Service/Task/TaskService.php index e83f08e..27568e9 100644 --- a/src/Service/Task/TaskService.php +++ b/src/Service/Task/TaskService.php @@ -5,6 +5,7 @@ namespace App\Service\Task; use App\Entity\Task; +use App\Exception\Task as TaskException; final class TaskService extends Base { @@ -64,20 +65,7 @@ public function create(array $input): object if (! isset($data->name)) { throw new \App\Exception\Task('The field "name" is required.', 400); } - $mytask = new Task(); - $mytask->updateName(self::validateTaskName($data->name)); - $description = isset($data->description) ? $data->description : null; - $mytask->updateDescription($description); - $status = 0; - if (isset($data->status)) { - $status = self::validateTaskStatus($data->status); - } - $mytask->updateStatus($status); - $userId = null; - if (isset($data->decoded) && isset($data->decoded->sub)) { - $userId = (int) $data->decoded->sub; - } - $mytask->updateUserId($userId); + $mytask = $this->createTask($data); /** @var Task $task */ $task = $this->getTaskRepository()->create($mytask); if (self::isRedisEnabled() === true) { @@ -91,6 +79,26 @@ public function create(array $input): object return $task->toJson(); } + public function createTask(object $data): Task + { + $task = new Task(); + $task->updateName(self::validateTaskName($data->name)); + $description = $data->description ?? null; + $task->updateDescription($description); + $status = 0; + if (isset($data->status)) { + $status = self::validateTaskStatus($data->status); + } + $task->updateStatus($status); + $userId = null; + if (isset($data->decoded) && isset($data->decoded->sub)) { + $userId = (int) $data->decoded->sub; + } + $task->updateUserId($userId); + + return $task; + } + /** * @param array $input */ @@ -124,7 +132,7 @@ private function validateTask(array $input, int $taskId): Task $task = $this->getTaskFromDb($taskId, (int) $input['decoded']->sub); $data = json_decode((string) json_encode($input), false); if (! isset($data->name) && ! isset($data->status)) { - throw new \App\Exception\Task('Enter the data to update the task.', 400); + throw new TaskException('Enter the data to update the task.', 400); } if (isset($data->name)) { $task->updateName(self::validateTaskName($data->name)); diff --git a/src/Service/User/Create.php b/src/Service/User/Create.php index 384ed1d..0705da0 100644 --- a/src/Service/User/Create.php +++ b/src/Service/User/Create.php @@ -5,6 +5,7 @@ namespace App\Service\User; use App\Entity\User; +use App\Exception\User as UserException; final class Create extends Base { @@ -30,13 +31,13 @@ private function validateUserData(array $input): User { $user = json_decode((string) json_encode($input), false); if (! isset($user->name)) { - throw new \App\Exception\User('The field "name" is required.', 400); + throw new UserException('The field "name" is required.', 400); } if (! isset($user->email)) { - throw new \App\Exception\User('The field "email" is required.', 400); + throw new UserException('The field "email" is required.', 400); } if (! isset($user->password)) { - throw new \App\Exception\User('The field "password" is required.', 400); + throw new UserException('The field "password" is required.', 400); } $myuser = new User(); $myuser->updateName(self::validateUserName($user->name)); diff --git a/src/Service/User/Update.php b/src/Service/User/Update.php index 94494f7..cbf5845 100644 --- a/src/Service/User/Update.php +++ b/src/Service/User/Update.php @@ -5,6 +5,7 @@ namespace App\Service\User; use App\Entity\User; +use App\Exception\User as UserException; final class Update extends Base { @@ -31,7 +32,7 @@ private function validateUserData(array $input, int $userId): User $user = $this->getUserFromDb($userId); $data = json_decode((string) json_encode($input), false); if (! isset($data->name) && ! isset($data->email)) { - throw new \App\Exception\User('Enter the data to update the user.', 400); + throw new UserException('Enter the data to update the user.', 400); } if (isset($data->name)) { $user->updateName(self::validateUserName($data->name));