Skip to content

Commit

Permalink
Merge pull request #142 from maurobonfietti/2.16.0
Browse files Browse the repository at this point in the history
Version 2.16.0
  • Loading branch information
maurobonfietti authored Jan 7, 2022
2 parents 19e7630 + e90277e commit 07c90f7
Show file tree
Hide file tree
Showing 34 changed files with 245 additions and 79 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"prefer-stable": true,
"require": {
"php": "^8.0",
"composer/composer": "2.1.9",
"composer/composer": "^2.0",
"firebase/php-jwt": "^5.0",
"palanik/corsslim": "dev-slim3",
"predis/predis": "^1.1",
Expand All @@ -37,7 +37,8 @@
"pestphp/pest": "^1.7",
"phpstan/phpstan": "^1.2",
"phpunit/phpunit": "^9.0",
"rector/rector": "^0.12.4"
"rector/rector": "^0.12.4",
"vimeo/psalm": "^4.18"
},
"config": {
"sort-packages": true,
Expand Down
2 changes: 1 addition & 1 deletion extras/bin/restart-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$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
2 changes: 1 addition & 1 deletion src/App/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
require __DIR__ . '/Dependencies.php';
require __DIR__ . '/Services.php';
require __DIR__ . '/Repositories.php';
require __DIR__ . '/Routes.php';
(require __DIR__ . '/Routes.php')($app);
54 changes: 29 additions & 25 deletions src/App/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@
use App\Controller\User;
use App\Middleware\Auth;

$app->get('/', 'App\Controller\DefaultController:getHelp');
$app->get('/status', 'App\Controller\DefaultController:getStatus');
$app->post('/login', \App\Controller\User\Login::class);
return function ($app) {
$app->get('/', 'App\Controller\DefaultController:getHelp');
$app->get('/status', 'App\Controller\DefaultController:getStatus');
$app->post('/login', \App\Controller\User\Login::class);

$app->group('/api/v1', function () use ($app): void {
$app->group('/tasks', function () use ($app): void {
$app->get('', Task\GetAll::class);
$app->post('', Task\Create::class);
$app->get('/{id}', Task\GetOne::class);
$app->put('/{id}', Task\Update::class);
$app->delete('/{id}', Task\Delete::class);
})->add(new Auth());
$app->group('/api/v1', function () use ($app): void {
$app->group('/tasks', function () use ($app): void {
$app->get('', Task\GetAll::class);
$app->post('', Task\Create::class);
$app->get('/{id}', Task\GetOne::class);
$app->put('/{id}', Task\Update::class);
$app->delete('/{id}', Task\Delete::class);
})->add(new Auth());

$app->group('/users', function () use ($app): void {
$app->get('', User\GetAll::class)->add(new Auth());
$app->post('', User\Create::class);
$app->get('/{id}', User\GetOne::class)->add(new Auth());
$app->put('/{id}', User\Update::class)->add(new Auth());
$app->delete('/{id}', User\Delete::class)->add(new Auth());
});
$app->group('/users', function () use ($app): void {
$app->get('', User\GetAll::class)->add(new Auth());
$app->post('', User\Create::class);
$app->get('/{id}', User\GetOne::class)->add(new Auth());
$app->put('/{id}', User\Update::class)->add(new Auth());
$app->delete('/{id}', User\Delete::class)->add(new Auth());
});

$app->group('/notes', function () use ($app): void {
$app->get('', Note\GetAll::class);
$app->post('', Note\Create::class);
$app->get('/{id}', Note\GetOne::class);
$app->put('/{id}', Note\Update::class);
$app->delete('/{id}', Note\Delete::class);
$app->group('/notes', function () use ($app): void {
$app->get('', Note\GetAll::class);
$app->post('', Note\Create::class);
$app->get('/{id}', Note\GetOne::class);
$app->put('/{id}', Note\Update::class);
$app->delete('/{id}', Note\Delete::class);
});
});
});

return $app;
};
40 changes: 30 additions & 10 deletions src/App/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,72 @@
use App\Service\User;
use Psr\Container\ContainerInterface;

$container['find_user_service'] = static fn (ContainerInterface $container): User\Find => new User\Find(
$container['find_user_service'] = static fn (
ContainerInterface $container
): User\Find => new User\Find(
$container->get('user_repository'),
$container->get('redis_service')
);

$container['create_user_service'] = static fn (ContainerInterface $container): User\Create => new User\Create(
$container['create_user_service'] = static fn (
ContainerInterface $container
): User\Create => new User\Create(
$container->get('user_repository'),
$container->get('redis_service')
);

$container['update_user_service'] = static fn (ContainerInterface $container): User\Update => new User\Update(
$container['update_user_service'] = static fn (
ContainerInterface $container
): User\Update => new User\Update(
$container->get('user_repository'),
$container->get('redis_service')
);

$container['delete_user_service'] = static fn (ContainerInterface $container): User\Delete => new User\Delete(
$container['delete_user_service'] = static fn (
ContainerInterface $container
): User\Delete => new User\Delete(
$container->get('user_repository'),
$container->get('redis_service')
);

$container['login_user_service'] = static fn (ContainerInterface $container): User\Login => new User\Login(
$container['login_user_service'] = static fn (
ContainerInterface $container
): User\Login => new User\Login(
$container->get('user_repository'),
$container->get('redis_service')
);

$container['task_service'] = static fn (ContainerInterface $container): TaskService => new TaskService(
$container['task_service'] = static fn (
ContainerInterface $container
): TaskService => new TaskService(
$container->get('task_repository'),
$container->get('redis_service')
);

$container['find_note_service'] = static fn (ContainerInterface $container): Note\Find => new Note\Find(
$container['find_note_service'] = static fn (
ContainerInterface $container
): Note\Find => new Note\Find(
$container->get('note_repository'),
$container->get('redis_service')
);

$container['create_note_service'] = static fn (ContainerInterface $container): Note\Create => new Note\Create(
$container['create_note_service'] = static fn (
ContainerInterface $container
): Note\Create => new Note\Create(
$container->get('note_repository'),
$container->get('redis_service')
);

$container['update_note_service'] = static fn (ContainerInterface $container): Note\Update => new Note\Update(
$container['update_note_service'] = static fn (
ContainerInterface $container
): Note\Update => new Note\Update(
$container->get('note_repository'),
$container->get('redis_service')
);

$container['delete_note_service'] = static fn (ContainerInterface $container): Note\Delete => new Note\Delete(
$container['delete_note_service'] = static fn (
ContainerInterface $container
): Note\Delete => new Note\Delete(
$container->get('note_repository'),
$container->get('redis_service')
);
2 changes: 1 addition & 1 deletion src/App/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'url' => $_SERVER['REDIS_URL'],
],
'app' => [
'domain' => $_SERVER['APP_DOMAIN'],
'domain' => $_SERVER['APP_DOMAIN'] ?? '',
'secret' => $_SERVER['SECRET_KEY'],
],
],
Expand Down
8 changes: 5 additions & 3 deletions src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

final class DefaultController extends BaseController
{
private const API_VERSION = '2.15.0';
private const API_VERSION = '2.16.0';

public function getHelp(Request $request, Response $response): Response
{
$app = $this->container->get('settings')['app'];
$url = $app['domain'];
$url = $this->container->get('settings')['app']['domain'];
$endpoints = [
'tasks' => $url . '/api/v1/tasks',
'users' => $url . '/api/v1/users',
Expand Down Expand Up @@ -45,6 +44,9 @@ public function getStatus(Request $request, Response $response): Response
return $this->jsonResponse($response, 'success', $status, 200);
}

/**
* @return array<int>
*/
private function getDbStats(): array
{
$taskService = $this->container->get('task_service');
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Note/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

final class Delete extends Base
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Note/GetOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

final class GetOne extends Base
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
Expand Down
6 changes: 5 additions & 1 deletion src/Controller/Note/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

final class Update extends Base
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$input = (array) $request->getParsedBody();
$note = $this->getServiceUpdateNote()->update($input, (int) $args['id']);
$id = (int) $args['id'];
$note = $this->getServiceUpdateNote()->update($input, $id);

return $this->jsonResponse($response, 'success', $note, 200);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Controller/Task/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@

final class Delete extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$input = (array) $request->getParsedBody();
$taskId = (int) $args['id'];
$userId = $this->getAndValidateUserId($input);
Expand Down
10 changes: 8 additions & 2 deletions src/Controller/Task/GetOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@

final class GetOne extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$input = (array) $request->getParsedBody();
$taskId = (int) $args['id'];
$userId = $this->getAndValidateUserId($input);
Expand Down
10 changes: 8 additions & 2 deletions src/Controller/Task/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@

final class Update extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$input = (array) $request->getParsedBody();
$task = $this->getTaskService()->update($input, (int) $args['id']);

Expand Down
15 changes: 11 additions & 4 deletions src/Controller/User/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@

final class Delete extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$input = (array) $request->getParsedBody();
$userIdLogged = $this->getAndValidateUserId($input);
$this->checkUserPermissions((int) $args['id'], (int) $userIdLogged);
$this->getDeleteUserService()->delete((int) $args['id']);
$id = (int) $args['id'];
$this->checkUserPermissions($id, $userIdLogged);
$this->getDeleteUserService()->delete($id);

return $this->jsonResponse($response, 'success', null, 204);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Controller/User/GetOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@

final class GetOne extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$user = $this->getFindUserService()->getOne((int) $args['id']);

return $this->jsonResponse($response, 'success', $user, 200);
Expand Down
15 changes: 11 additions & 4 deletions src/Controller/User/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@

final class Update extends Base
{
public function __invoke(Request $request, Response $response, array $args): Response
{
/**
* @param array<string> $args
*/
public function __invoke(
Request $request,
Response $response,
array $args
): Response {
$input = (array) $request->getParsedBody();
$id = (int) $args['id'];
$userIdLogged = $this->getAndValidateUserId($input);
$this->checkUserPermissions((int) $args['id'], (int) $userIdLogged);
$user = $this->getUpdateUserService()->update($input, (int) $args['id']);
$this->checkUserPermissions($id, $userIdLogged);
$user = $this->getUpdateUserService()->update($input, $id);

return $this->jsonResponse($response, 'success', $user, 200);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getUserId(): int
return $this->userId;
}

public function updateUserId(int $userId): self
public function updateUserId(?int $userId): self
{
$this->userId = $userId;

Expand Down
4 changes: 4 additions & 0 deletions src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ protected function getResultsWithPagination(
];
}

/**
* @param array<string> $params
* @return array<float|int|string>
*/
protected function getResultByPage(
string $query,
int $page,
Expand Down
Loading

0 comments on commit 07c90f7

Please sign in to comment.