Skip to content

Commit ff1740f

Browse files
committed
ResponseTrait -> HandlerTrait and ResponseTrait
Signed-off-by: alexmerlin <[email protected]>
1 parent 8fd4440 commit ff1740f

22 files changed

+169
-180
lines changed

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
44
bootstrap="vendor/autoload.php"
5+
stopOnError="true"
56
stopOnFailure="true"
67
colors="true">
78
<testsuites>

psalm-baseline.xml

+6
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@
2727
<code>matching</code>
2828
</UndefinedInterfaceMethod>
2929
</file>
30+
<file src="src/App/src/Handler/ResponseTrait.php">
31+
<UndefinedThisPropertyFetch>
32+
<code>$this->responseFactory</code>
33+
<code>$this->resourceGenerator</code>
34+
</UndefinedThisPropertyFetch>
35+
</file>
3036
</files>

src/Admin/src/Entity/Admin.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ class Admin extends AbstractEntity implements UserEntityInterface
2828
];
2929

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

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

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

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

4242
#[ORM\Column(name: "status", type: "string", length: 20)]
4343
protected string $status = self::STATUS_ACTIVE;

src/Admin/src/Entity/AdminRole.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AdminRole extends AbstractEntity implements RoleInterface
2222
];
2323

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

2727
public function getName(): string
2828
{

src/Admin/src/Handler/AdminAccountHandler.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Api\App\Exception\BadRequestException;
1111
use Api\App\Exception\ConflictException;
1212
use Api\App\Exception\NotFoundException;
13-
use Api\App\Handler\ResponseTrait;
13+
use Api\App\Handler\HandlerTrait;
1414
use Dot\AnnotatedServices\Annotation\Inject;
1515
use Mezzio\Hal\HalResponseFactory;
1616
use Mezzio\Hal\ResourceGenerator;
@@ -20,7 +20,7 @@
2020

2121
class AdminAccountHandler implements RequestHandlerInterface
2222
{
23-
use ResponseTrait;
23+
use HandlerTrait;
2424

2525
/**
2626
* @Inject({
@@ -50,7 +50,7 @@ public function get(ServerRequestInterface $request): ResponseInterface
5050
*/
5151
public function patch(ServerRequestInterface $request): ResponseInterface
5252
{
53-
$inputFilter = (new UpdateAdminInputFilter())->setData($request->getParsedBody());
53+
$inputFilter = (new UpdateAdminInputFilter())->setData((array) $request->getParsedBody());
5454
if (! $inputFilter->isValid()) {
5555
throw (new BadRequestException())->setMessages($inputFilter->getMessages());
5656
}

src/Admin/src/Handler/AdminHandler.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Api\App\Exception\BadRequestException;
1212
use Api\App\Exception\ConflictException;
1313
use Api\App\Exception\NotFoundException;
14-
use Api\App\Handler\ResponseTrait;
14+
use Api\App\Handler\HandlerTrait;
1515
use Api\App\Message;
1616
use Dot\AnnotatedServices\Annotation\Inject;
1717
use Mezzio\Hal\HalResponseFactory;
@@ -24,7 +24,7 @@
2424

2525
class AdminHandler implements RequestHandlerInterface
2626
{
27-
use ResponseTrait;
27+
use HandlerTrait;
2828

2929
/**
3030
* @Inject({
@@ -86,7 +86,7 @@ public function getCollection(ServerRequestInterface $request): ResponseInterfac
8686
*/
8787
public function patch(ServerRequestInterface $request): ResponseInterface
8888
{
89-
$inputFilter = (new UpdateAdminInputFilter())->setData($request->getParsedBody());
89+
$inputFilter = (new UpdateAdminInputFilter())->setData((array) $request->getParsedBody());
9090
if (! $inputFilter->isValid()) {
9191
throw (new BadRequestException())->setMessages($inputFilter->getMessages());
9292
}
@@ -109,7 +109,7 @@ public function patch(ServerRequestInterface $request): ResponseInterface
109109
*/
110110
public function post(ServerRequestInterface $request): ResponseInterface
111111
{
112-
$inputFilter = (new CreateAdminInputFilter())->setData($request->getParsedBody());
112+
$inputFilter = (new CreateAdminInputFilter())->setData((array) $request->getParsedBody());
113113
if (! $inputFilter->isValid()) {
114114
throw (new BadRequestException())->setMessages($inputFilter->getMessages());
115115
}

src/Admin/src/Handler/AdminRoleHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Api\Admin\Entity\AdminRole;
88
use Api\Admin\Service\AdminRoleServiceInterface;
99
use Api\App\Exception\NotFoundException;
10-
use Api\App\Handler\ResponseTrait;
10+
use Api\App\Handler\HandlerTrait;
1111
use Api\App\Message;
1212
use Dot\AnnotatedServices\Annotation\Inject;
1313
use Mezzio\Hal\HalResponseFactory;
@@ -20,7 +20,7 @@
2020

2121
class AdminRoleHandler implements RequestHandlerInterface
2222
{
23-
use ResponseTrait;
23+
use HandlerTrait;
2424

2525
/**
2626
* @Inject({

src/App/src/Handler/ErrorReportHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
class ErrorReportHandler implements RequestHandlerInterface
2424
{
25-
use ResponseTrait;
25+
use HandlerTrait;
2626

2727
/**
2828
* @Inject({

src/App/src/Handler/HandlerTrait.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Api\App\Handler;
6+
7+
use Api\App\Exception\BadRequestException;
8+
use Api\App\Exception\ConflictException;
9+
use Api\App\Exception\ExpiredException;
10+
use Api\App\Exception\ForbiddenException;
11+
use Api\App\Exception\MethodNotAllowedException;
12+
use Api\App\Exception\NotFoundException;
13+
use Api\App\Exception\UnauthorizedException;
14+
use Dot\Mail\Exception\MailException;
15+
use Exception;
16+
use Fig\Http\Message\RequestMethodInterface;
17+
use Fig\Http\Message\StatusCodeInterface;
18+
use Mezzio\Hal\Metadata\MetadataMap;
19+
use Mezzio\Hal\Metadata\RouteBasedCollectionMetadata;
20+
use Mezzio\Hal\ResourceGenerator\Exception\OutOfBoundsException;
21+
use Mezzio\Router\RouteResult;
22+
use Psr\Http\Message\ResponseInterface;
23+
use Psr\Http\Message\ServerRequestInterface;
24+
use RuntimeException;
25+
26+
use function array_key_exists;
27+
use function is_array;
28+
use function method_exists;
29+
use function sprintf;
30+
use function strtolower;
31+
32+
trait HandlerTrait
33+
{
34+
use ResponseTrait;
35+
36+
/**
37+
* @throws Exception
38+
*/
39+
public function handle(ServerRequestInterface $request): ResponseInterface
40+
{
41+
try {
42+
$method = strtolower($request->getMethod());
43+
if ($this->isGetCollectionRequest($request, $this->config)) {
44+
$method = 'getCollection';
45+
}
46+
47+
if (! method_exists($this, $method)) {
48+
throw new MethodNotAllowedException(
49+
sprintf('Method %s is not implemented for the requested resource.', $method)
50+
);
51+
}
52+
53+
return $this->$method($request);
54+
} catch (ConflictException $exception) {
55+
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_CONFLICT);
56+
} catch (ForbiddenException $exception) {
57+
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_FORBIDDEN);
58+
} catch (ExpiredException $exception) {
59+
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_GONE);
60+
} catch (OutOfBoundsException | NotFoundException $exception) {
61+
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_NOT_FOUND);
62+
} catch (UnauthorizedException $exception) {
63+
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_UNAUTHORIZED);
64+
} catch (MethodNotAllowedException $exception) {
65+
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED);
66+
} catch (BadRequestException $exception) {
67+
return $this->errorResponse($exception->getMessages(), StatusCodeInterface::STATUS_BAD_REQUEST);
68+
} catch (MailException | RuntimeException | Exception $exception) {
69+
return $this->errorResponse($exception->getMessage());
70+
}
71+
}
72+
73+
/**
74+
* @throws RuntimeException
75+
*/
76+
private function isGetCollectionRequest(ServerRequestInterface $request, array $config): bool
77+
{
78+
if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) {
79+
return false;
80+
}
81+
82+
if (! array_key_exists(MetadataMap::class, $config)) {
83+
throw new RuntimeException(
84+
sprintf('Unable to load %s from config.', MetadataMap::class)
85+
);
86+
}
87+
88+
/** @var RouteResult $routeResult */
89+
$routeResult = $request->getAttribute(RouteResult::class);
90+
$routeName = $routeResult->getMatchedRouteName();
91+
92+
$halConfig = null;
93+
foreach ($config[MetadataMap::class] as $cfg) {
94+
if ($cfg['route'] === $routeName) {
95+
$halConfig = $cfg;
96+
break;
97+
}
98+
}
99+
100+
return is_array($halConfig)
101+
&& array_key_exists('__class__', $halConfig)
102+
&& $halConfig['__class__'] === RouteBasedCollectionMetadata::class;
103+
}
104+
}

src/App/src/Handler/HomeHandler.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
use Mezzio\Hal\HalResponseFactory;
99
use Mezzio\Hal\ResourceGenerator;
1010
use Psr\Http\Message\ResponseInterface;
11-
use Psr\Http\Message\ServerRequestInterface;
1211
use Psr\Http\Server\RequestHandlerInterface;
1312

1413
class HomeHandler implements RequestHandlerInterface
1514
{
16-
use ResponseTrait;
15+
use HandlerTrait;
1716

1817
/**
1918
* @Inject({
@@ -29,7 +28,7 @@ public function __construct(
2928
) {
3029
}
3130

32-
public function get(ServerRequestInterface $request): ResponseInterface
31+
public function get(): ResponseInterface
3332
{
3433
return $this->jsonResponse(['message' => 'Welcome to DotKernel API!']);
3534
}

src/App/src/Handler/NotFoundHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class NotFoundHandler implements RequestHandlerInterface
1212
{
13-
use ResponseTrait;
13+
use HandlerTrait;
1414

1515
public function handle(ServerRequestInterface $request): ResponseInterface
1616
{

0 commit comments

Comments
 (0)