Skip to content

Commit

Permalink
ResponseTrait -> HandlerTrait and ResponseTrait
Browse files Browse the repository at this point in the history
Signed-off-by: alexmerlin <[email protected]>
  • Loading branch information
alexmerlin committed May 30, 2024
1 parent 8fd4440 commit ff1740f
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 180 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
stopOnError="true"
stopOnFailure="true"
colors="true">
<testsuites>
Expand Down
6 changes: 6 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@
<code>matching</code>
</UndefinedInterfaceMethod>
</file>
<file src="src/App/src/Handler/ResponseTrait.php">
<UndefinedThisPropertyFetch>
<code>$this->responseFactory</code>
<code>$this->resourceGenerator</code>
</UndefinedThisPropertyFetch>
</file>
</files>
8 changes: 4 additions & 4 deletions src/Admin/src/Entity/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ class Admin extends AbstractEntity implements UserEntityInterface
];

#[ORM\Column(name: "identity", type: "string", length: 100, unique: true)]
protected string $identity;
protected string $identity = '';

#[ORM\Column(name: "firstName", type: "string", length: 255)]
protected string $firstName;
protected string $firstName = '';

#[ORM\Column(name: "lastName", type: "string", length: 255)]
protected string $lastName;
protected string $lastName = '';

#[ORM\Column(name: "password", type: "string", length: 100)]
protected string $password;
protected string $password = '';

#[ORM\Column(name: "status", type: "string", length: 20)]
protected string $status = self::STATUS_ACTIVE;
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/src/Entity/AdminRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AdminRole extends AbstractEntity implements RoleInterface
];

#[ORM\Column(name: "name", type: "string", length: 30, unique: true)]
protected string $name;
protected string $name = '';

public function getName(): string
{
Expand Down
6 changes: 3 additions & 3 deletions src/Admin/src/Handler/AdminAccountHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\ResponseTrait;
use Api\App\Handler\HandlerTrait;
use Dot\AnnotatedServices\Annotation\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
Expand All @@ -20,7 +20,7 @@

class AdminAccountHandler implements RequestHandlerInterface
{
use ResponseTrait;
use HandlerTrait;

/**
* @Inject({
Expand Down Expand Up @@ -50,7 +50,7 @@ public function get(ServerRequestInterface $request): ResponseInterface
*/
public function patch(ServerRequestInterface $request): ResponseInterface
{
$inputFilter = (new UpdateAdminInputFilter())->setData($request->getParsedBody());
$inputFilter = (new UpdateAdminInputFilter())->setData((array) $request->getParsedBody());
if (! $inputFilter->isValid()) {
throw (new BadRequestException())->setMessages($inputFilter->getMessages());
}
Expand Down
8 changes: 4 additions & 4 deletions src/Admin/src/Handler/AdminHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\ResponseTrait;
use Api\App\Handler\HandlerTrait;
use Api\App\Message;
use Dot\AnnotatedServices\Annotation\Inject;
use Mezzio\Hal\HalResponseFactory;
Expand All @@ -24,7 +24,7 @@

class AdminHandler implements RequestHandlerInterface
{
use ResponseTrait;
use HandlerTrait;

/**
* @Inject({
Expand Down Expand Up @@ -86,7 +86,7 @@ public function getCollection(ServerRequestInterface $request): ResponseInterfac
*/
public function patch(ServerRequestInterface $request): ResponseInterface
{
$inputFilter = (new UpdateAdminInputFilter())->setData($request->getParsedBody());
$inputFilter = (new UpdateAdminInputFilter())->setData((array) $request->getParsedBody());
if (! $inputFilter->isValid()) {
throw (new BadRequestException())->setMessages($inputFilter->getMessages());
}
Expand All @@ -109,7 +109,7 @@ public function patch(ServerRequestInterface $request): ResponseInterface
*/
public function post(ServerRequestInterface $request): ResponseInterface
{
$inputFilter = (new CreateAdminInputFilter())->setData($request->getParsedBody());
$inputFilter = (new CreateAdminInputFilter())->setData((array) $request->getParsedBody());
if (! $inputFilter->isValid()) {
throw (new BadRequestException())->setMessages($inputFilter->getMessages());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Admin/src/Handler/AdminRoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Api\Admin\Entity\AdminRole;
use Api\Admin\Service\AdminRoleServiceInterface;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\ResponseTrait;
use Api\App\Handler\HandlerTrait;
use Api\App\Message;
use Dot\AnnotatedServices\Annotation\Inject;
use Mezzio\Hal\HalResponseFactory;
Expand All @@ -20,7 +20,7 @@

class AdminRoleHandler implements RequestHandlerInterface
{
use ResponseTrait;
use HandlerTrait;

/**
* @Inject({
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Handler/ErrorReportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
class ErrorReportHandler implements RequestHandlerInterface
{
use ResponseTrait;
use HandlerTrait;

/**
* @Inject({
Expand Down
104 changes: 104 additions & 0 deletions src/App/src/Handler/HandlerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Api\App\Handler;

use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
use Api\App\Exception\ExpiredException;
use Api\App\Exception\ForbiddenException;
use Api\App\Exception\MethodNotAllowedException;
use Api\App\Exception\NotFoundException;
use Api\App\Exception\UnauthorizedException;
use Dot\Mail\Exception\MailException;

Check warning on line 14 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'MailException'

Check warning on line 14 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Exception'
use Exception;
use Fig\Http\Message\RequestMethodInterface;

Check warning on line 16 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RequestMethodInterface'

Check warning on line 16 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Message'
use Fig\Http\Message\StatusCodeInterface;

Check warning on line 17 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'StatusCodeInterface'

Check warning on line 17 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Message'
use Mezzio\Hal\Metadata\MetadataMap;

Check warning on line 18 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'MetadataMap'

Check warning on line 18 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Metadata'
use Mezzio\Hal\Metadata\RouteBasedCollectionMetadata;

Check warning on line 19 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RouteBasedCollectionMetadata'

Check warning on line 19 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Metadata'
use Mezzio\Hal\ResourceGenerator\Exception\OutOfBoundsException;

Check warning on line 20 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'OutOfBoundsException'

Check warning on line 20 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Exception'
use Mezzio\Router\RouteResult;

Check warning on line 21 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RouteResult'

Check warning on line 21 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Router'
use Psr\Http\Message\ResponseInterface;

Check warning on line 22 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'ResponseInterface'

Check warning on line 22 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Message'
use Psr\Http\Message\ServerRequestInterface;

Check warning on line 23 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'ServerRequestInterface'

Check warning on line 23 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Message'
use RuntimeException;

use function array_key_exists;
use function is_array;
use function method_exists;
use function sprintf;
use function strtolower;

trait HandlerTrait
{
use ResponseTrait;

/**
* @throws Exception
*/
public function handle(ServerRequestInterface $request): ResponseInterface

Check warning on line 39 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'ServerRequestInterface'

Check warning on line 39 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'ResponseInterface'
{
try {
$method = strtolower($request->getMethod());
if ($this->isGetCollectionRequest($request, $this->config)) {
$method = 'getCollection';
}

if (! method_exists($this, $method)) {
throw new MethodNotAllowedException(
sprintf('Method %s is not implemented for the requested resource.', $method)
);
}

return $this->$method($request);
} catch (ConflictException $exception) {
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_CONFLICT);

Check warning on line 55 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'StatusCodeInterface'
} catch (ForbiddenException $exception) {
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_FORBIDDEN);

Check warning on line 57 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'StatusCodeInterface'
} catch (ExpiredException $exception) {
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_GONE);

Check warning on line 59 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'StatusCodeInterface'
} catch (OutOfBoundsException | NotFoundException $exception) {

Check warning on line 60 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'OutOfBoundsException'
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_NOT_FOUND);

Check warning on line 61 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'StatusCodeInterface'
} catch (UnauthorizedException $exception) {
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_UNAUTHORIZED);

Check warning on line 63 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'StatusCodeInterface'
} catch (MethodNotAllowedException $exception) {
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED);

Check warning on line 65 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'StatusCodeInterface'
} catch (BadRequestException $exception) {
return $this->errorResponse($exception->getMessages(), StatusCodeInterface::STATUS_BAD_REQUEST);

Check warning on line 67 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'StatusCodeInterface'
} catch (MailException | RuntimeException | Exception $exception) {

Check warning on line 68 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'MailException'
return $this->errorResponse($exception->getMessage());
}
}

/**
* @throws RuntimeException
*/
private function isGetCollectionRequest(ServerRequestInterface $request, array $config): bool

Check warning on line 76 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'ServerRequestInterface'
{
if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) {

Check warning on line 78 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RequestMethodInterface'
return false;
}

if (! array_key_exists(MetadataMap::class, $config)) {

Check warning on line 82 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'MetadataMap'
throw new RuntimeException(
sprintf('Unable to load %s from config.', MetadataMap::class)

Check warning on line 84 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'MetadataMap'
);
}

/** @var RouteResult $routeResult */

Check warning on line 88 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RouteResult'
$routeResult = $request->getAttribute(RouteResult::class);

Check warning on line 89 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RouteResult'
$routeName = $routeResult->getMatchedRouteName();

$halConfig = null;
foreach ($config[MetadataMap::class] as $cfg) {

Check warning on line 93 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'MetadataMap'
if ($cfg['route'] === $routeName) {
$halConfig = $cfg;
break;
}
}

return is_array($halConfig)
&& array_key_exists('__class__', $halConfig)
&& $halConfig['__class__'] === RouteBasedCollectionMetadata::class;

Check warning on line 102 in src/App/src/Handler/HandlerTrait.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RouteBasedCollectionMetadata'
}
}
5 changes: 2 additions & 3 deletions src/App/src/Handler/HomeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
use Mezzio\Hal\HalResponseFactory;

Check warning on line 8 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'HalResponseFactory'

Check warning on line 8 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Hal'
use Mezzio\Hal\ResourceGenerator;

Check warning on line 9 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'ResourceGenerator'

Check warning on line 9 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Hal'
use Psr\Http\Message\ResponseInterface;

Check warning on line 10 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'ResponseInterface'

Check warning on line 10 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Message'
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

Check warning on line 11 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RequestHandlerInterface'

Check warning on line 11 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined namespace

Undefined namespace 'Server'

class HomeHandler implements RequestHandlerInterface

Check warning on line 13 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'RequestHandlerInterface'
{
use ResponseTrait;
use HandlerTrait;

/**
* @Inject({
Expand All @@ -29,7 +28,7 @@ public function __construct(
) {
}

public function get(ServerRequestInterface $request): ResponseInterface
public function get(): ResponseInterface

Check warning on line 31 in src/App/src/Handler/HomeHandler.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Undefined class

Undefined class 'ResponseInterface'
{
return $this->jsonResponse(['message' => 'Welcome to DotKernel API!']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Handler/NotFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class NotFoundHandler implements RequestHandlerInterface
{
use ResponseTrait;
use HandlerTrait;

public function handle(ServerRequestInterface $request): ResponseInterface
{
Expand Down
Loading

0 comments on commit ff1740f

Please sign in to comment.