Skip to content

Commit

Permalink
feat: Altera endpoints para usar o wrapper de resposta HTTP da lib ti…
Browse files Browse the repository at this point in the history
…ny-blocks/http.

* feat: Altera endpoints para usar o wrapper de resposta HTTP da lib tiny-blocks/http.
  • Loading branch information
gustavofreze authored Jun 14, 2023
1 parent 8cbf7c3 commit a7e4bb8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 64 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM gustavofreze/php:8.1.7-fpm
FROM gustavofreze/php:8.2.6-fpm

RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN docker-php-ext-install mysqli pdo_mysql
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.1.7-fpm
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.2.6-fpm
DOCKER_EXEC = docker exec -it points-of-interest
DOCKER_COMPOSE = docker-compose
DOCKER_IMAGE_PRUNE = docker image prune --all --force
DOCKER_NETWORK_PRUNE = docker network prune --force

FLYWAY = docker run --rm -v ${PWD}/db/mysql/migrations:/flyway/sql --network=points-of-interest_default --env-file=config/configs.env flyway/flyway:9.1.2-alpine
FLYWAY = docker run --rm -v ${PWD}/db/mysql/migrations:/flyway/sql --network=points-of-interest_default --env-file=config/configs.env flyway/flyway:9.19.4-alpine

.PHONY: configure run test test-no-coverage review show-reports stop clean clean-all

Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@
}
},
"require": {
"php": "^8.1",
"slim/psr7": "^1.5",
"slim/slim": "^4.10",
"doctrine/dbal": "^3.3",
"php": "^8.1||^8.2",
"slim/psr7": "^1.6",
"slim/slim": "4.11.0",
"doctrine/dbal": "^3.6",
"respect/validation": "^2.2",
"php-di/slim-bridge": "^3.2",
"tiny-blocks/http": "^1.0",
"php-di/slim-bridge": "^3.3",
"tiny-blocks/http": "^2.1",
"tiny-blocks/serializer": "^1.0",
"tiny-blocks/value-object": "^1.0"
},
"require-dev": {
"infection/infection": "^0.26",
"phpmd/phpmd": "^2.13",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.7"
},
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions src/Driver/Http/PointOfInterest/Register/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
use PointsOfInterest\Driver\Http\PointOfInterest\Register\Dtos\Response;
use PointsOfInterest\Driver\Http\PointOfInterest\Register\Exceptions\PointOfInterestAlreadyExists;
use PointsOfInterest\Driver\Http\Shared\HttpResponseAdapter;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TinyBlocks\Http\HttpCode;
use TinyBlocks\Http\HttpResponse;

final class Register extends HttpResponseAdapter
{
public function __construct(private readonly Points $points)
{
}

protected function handle(ServerRequestInterface $request): array
protected function handle(ServerRequestInterface $request): ResponseInterface
{
$request = new Request(request: $this->requestWithParsedBody());
$pointOfInterest = $request->toPointOfInterest();
Expand All @@ -29,9 +30,8 @@ protected function handle(ServerRequestInterface $request): array

$this->points->save(pointOfInterest: $pointOfInterest);

return $this
->withHttpCode(httpCode: HttpCode::CREATED)
->withResponse(httpResponse: (new Response(pointOfInterest: $pointOfInterest)))
->reply();
$response = new Response(pointOfInterest: $pointOfInterest);

return HttpResponse::created(data: $response);
}
}
12 changes: 6 additions & 6 deletions src/Driver/Http/PointOfInterest/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
use PointsOfInterest\Driver\Http\PointOfInterest\Search\Dtos\Request;
use PointsOfInterest\Driver\Http\PointOfInterest\Search\Dtos\Response;
use PointsOfInterest\Driver\Http\Shared\HttpResponseAdapter;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TinyBlocks\Http\HttpCode;
use TinyBlocks\Http\HttpResponse;

final class Search extends HttpResponseAdapter
{
public function __construct(private readonly Points $points)
{
}

protected function handle(ServerRequestInterface $request): array
protected function handle(ServerRequestInterface $request): ResponseInterface
{
$request = new Request(request: $request->getQueryParams());
$pointsOfInterest = $this->points->findAll();
Expand All @@ -27,9 +28,8 @@ protected function handle(ServerRequestInterface $request): array
maximumDistance: $request->toDistance()
);

return $this
->withHttpCode(httpCode: HttpCode::OK)
->withResponse(httpResponse: (new Response(pointsOfInterest: $pointsOfInterest)))
->reply();
$response = new Response(pointsOfInterest: $pointsOfInterest);

return HttpResponse::ok(data: $response->toArray());
}
}
46 changes: 7 additions & 39 deletions src/Driver/Http/Shared/HttpResponseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,39 @@
use Psr\Http\Message\ServerRequestInterface as Request;
use Throwable;
use TinyBlocks\Http\HttpCode;
use TinyBlocks\Http\HttpContentType;

abstract class HttpResponseAdapter
{
protected Request $request;
protected Response $response;
private HttpCode $httpCode = HttpCode::OK;
private ?HttpResponse $httpResponse = null;

private const CONTENT_TYPE = 'application/json';

abstract protected function handle(Request $request): array;
abstract protected function handle(Request $request): Response;

public function __invoke(Request $request, Response $response): Response
{
$this->request = $request;
$this->response = $response;

try {
return $this->replyWithSuccess();
return $this->handle(request: $this->request);
} catch (Throwable $exception) {
return $this->replyWithException(exception: $exception);
return $this->handleException(exception: $exception);
}
}

public function reply(): array
{
return $this->httpResponse ? $this->httpResponse->toArray() : [];
}

public function withHttpCode(HttpCode $httpCode): HttpResponseAdapter
{
$this->httpCode = $httpCode;

return $this;
}

public function withResponse(HttpResponse $httpResponse): HttpResponseAdapter
{
$this->httpResponse = $httpResponse;

return $this;
}

protected function requestWithParsedBody(): array
{
return json_decode($this->request->getBody()->__toString(), true);
}

private function replyWithSuccess(): Response
{
$data = $this->handle(request: $this->request);
$this->response->getBody()->write(json_encode($data));

return $this->response
->withStatus($this->httpCode->value)
->withHeader('Content-type', self::CONTENT_TYPE);
}

private function replyWithException(Throwable $exception): Response
private function handleException(Throwable $exception): Response
{
$data = ['error' => $exception->getMessage()];
$code = $exception->getCode();
$httpCode = (empty($code) || !HttpCode::isHttpCode(httpCode: $code))
? HttpCode::INTERNAL_SERVER_ERROR
: HttpCode::from($code);
: HttpCode::from(value: $code);

if ($exception instanceof HttpException) {
$data = ['error' => $exception->getErrors()];
Expand All @@ -80,6 +48,6 @@ private function replyWithException(Throwable $exception): Response

return $this->response
->withStatus($httpCode->value)
->withHeader('Content-type', self::CONTENT_TYPE);
->withHeader(HttpContentType::APPLICATION_JSON->key(), HttpContentType::APPLICATION_JSON->value);
}
}
5 changes: 3 additions & 2 deletions tests/Mock/ActionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use PointsOfInterest\Driver\Http\Shared\HttpResponseAdapter;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Psr\Http\Message\ResponseInterface;

final class ActionMock extends HttpResponseAdapter
{
protected function handle(ServerRequestInterface $request): array
protected function handle(ServerRequestInterface $request): ResponseInterface
{
$code = intval($request->getQueryParams()['code']);
throw new RuntimeException('Unexpected error.', $code);
throw new RuntimeException(message: 'Unexpected error.', code: $code);
}
}

0 comments on commit a7e4bb8

Please sign in to comment.