Skip to content

Commit

Permalink
Merge pull request #147 from maurobonfietti/2.19.0
Browse files Browse the repository at this point in the history
Version 2.19.0
  • Loading branch information
maurobonfietti authored Jan 23, 2023
2 parents 2778669 + 9144648 commit 0262319
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 41 deletions.
6 changes: 5 additions & 1 deletion extras/bin/restart-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
8 changes: 5 additions & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
};
12 changes: 9 additions & 3 deletions src/App/Repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
5 changes: 4 additions & 1 deletion src/App/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
3 changes: 0 additions & 3 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public function __construct(protected Container $container)
{
}

/**
* @param array|object|null $message
*/
protected function jsonResponse(
Response $response,
string $status,
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Task/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ protected function getTaskService(): TaskService
return $this->container->get('task_service');
}

/**
* @param array<object> $input
*/
protected function getAndValidateUserId(array $input): int
{
if (isset($input['decoded']) && isset($input['decoded']->sub)) {
Expand Down
9 changes: 7 additions & 2 deletions src/Controller/User/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> $input
*/
protected function getAndValidateUserId(array $input): int
{
if (isset($input['decoded']) && isset($input['decoded']->sub)) {
Expand Down
6 changes: 5 additions & 1 deletion src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ protected function getDb(): \PDO
return $this->database;
}

/**
* @param array<string, int|string> $params
*/
protected function getResultsWithPagination(
string $query,
int $page,
Expand All @@ -34,7 +37,8 @@ protected function getResultsWithPagination(
}

/**
* @param array<string> $params
* @param array<string, int|string> $params
*
* @return array<float|int|string>
*/
protected function getResultByPage(
Expand Down
3 changes: 1 addition & 2 deletions src/Repository/TaskRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
11 changes: 9 additions & 2 deletions src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Repository;

use App\Entity\User;
use App\Exception\User as UserException;

final class UserRepository extends BaseRepository
{
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Service/Note/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Service/Note/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Service\Note;

use App\Entity\Note;
use App\Exception\Note as NoteException;

final class Create extends Base
{
Expand All @@ -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);
Expand Down
38 changes: 23 additions & 15 deletions src/Service/Task/TaskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Service\Task;

use App\Entity\Task;
use App\Exception\Task as TaskException;

final class TaskService extends Base
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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<string> $input
*/
Expand Down Expand Up @@ -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));
Expand Down
7 changes: 4 additions & 3 deletions src/Service/User/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Service\User;

use App\Entity\User;
use App\Exception\User as UserException;

final class Create extends Base
{
Expand All @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion src/Service/User/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Service\User;

use App\Entity\User;
use App\Exception\User as UserException;

final class Update extends Base
{
Expand All @@ -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));
Expand Down

0 comments on commit 0262319

Please sign in to comment.