Skip to content

Commit 33c3052

Browse files
authored
Merge pull request #315 from dotkernel/issue-313
Issue #313: Remove `config` dependency from handlers.
2 parents 5cac6b3 + 59a9f33 commit 33c3052

23 files changed

+184
-118
lines changed

src/Admin/src/ConfigProvider.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Api\Admin\Entity\AdminRole;
1212
use Api\Admin\Factory\AdminCreateCommandFactory;
1313
use Api\Admin\Handler\AdminAccountHandler;
14+
use Api\Admin\Handler\AdminCollectionHandler;
1415
use Api\Admin\Handler\AdminHandler;
16+
use Api\Admin\Handler\AdminRoleCollectionHandler;
1517
use Api\Admin\Handler\AdminRoleHandler;
1618
use Api\Admin\Repository\AdminRepository;
1719
use Api\Admin\Repository\AdminRoleRepository;
@@ -46,14 +48,16 @@ public function getDependencies(): array
4648
],
4749
],
4850
'factories' => [
49-
AdminHandler::class => AttributedServiceFactory::class,
50-
AdminAccountHandler::class => AttributedServiceFactory::class,
51-
AdminRoleHandler::class => AttributedServiceFactory::class,
52-
AdminService::class => AttributedServiceFactory::class,
53-
AdminRoleService::class => AttributedServiceFactory::class,
54-
AdminCreateCommand::class => AdminCreateCommandFactory::class,
55-
AdminRepository::class => AttributedRepositoryFactory::class,
56-
AdminRoleRepository::class => AttributedRepositoryFactory::class,
51+
AdminHandler::class => AttributedServiceFactory::class,
52+
AdminCollectionHandler::class => AttributedServiceFactory::class,
53+
AdminAccountHandler::class => AttributedServiceFactory::class,
54+
AdminRoleHandler::class => AttributedServiceFactory::class,
55+
AdminRoleCollectionHandler::class => AttributedServiceFactory::class,
56+
AdminService::class => AttributedServiceFactory::class,
57+
AdminRoleService::class => AttributedServiceFactory::class,
58+
AdminCreateCommand::class => AdminCreateCommandFactory::class,
59+
AdminRepository::class => AttributedRepositoryFactory::class,
60+
AdminRoleRepository::class => AttributedRepositoryFactory::class,
5761
],
5862
'aliases' => [
5963
AdminServiceInterface::class => AdminService::class,

src/Admin/src/Handler/AdminAccountHandler.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ class AdminAccountHandler implements RequestHandlerInterface
2626
HalResponseFactory::class,
2727
ResourceGenerator::class,
2828
AdminServiceInterface::class,
29-
"config",
3029
)]
3130
public function __construct(
3231
protected HalResponseFactory $responseFactory,
3332
protected ResourceGenerator $resourceGenerator,
3433
protected AdminServiceInterface $adminService,
35-
protected array $config,
3634
) {
3735
}
3836

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Api\Admin\Handler;
6+
7+
use Api\Admin\Service\AdminServiceInterface;
8+
use Api\App\Exception\BadRequestException;
9+
use Api\App\Handler\HandlerTrait;
10+
use Dot\DependencyInjection\Attribute\Inject;
11+
use Mezzio\Hal\HalResponseFactory;
12+
use Mezzio\Hal\ResourceGenerator;
13+
use Psr\Http\Message\ResponseInterface;
14+
use Psr\Http\Message\ServerRequestInterface;
15+
use Psr\Http\Server\RequestHandlerInterface;
16+
17+
class AdminCollectionHandler implements RequestHandlerInterface
18+
{
19+
use HandlerTrait;
20+
21+
#[Inject(
22+
HalResponseFactory::class,
23+
ResourceGenerator::class,
24+
AdminServiceInterface::class,
25+
)]
26+
public function __construct(
27+
protected HalResponseFactory $responseFactory,
28+
protected ResourceGenerator $resourceGenerator,
29+
protected AdminServiceInterface $adminService,
30+
) {
31+
}
32+
33+
/**
34+
* @throws BadRequestException
35+
*/
36+
public function get(ServerRequestInterface $request): ResponseInterface
37+
{
38+
return $this->createResponse($request, $this->adminService->getAdmins($request->getQueryParams()));
39+
}
40+
}

src/Admin/src/Handler/AdminHandler.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ class AdminHandler implements RequestHandlerInterface
2626
HalResponseFactory::class,
2727
ResourceGenerator::class,
2828
AdminServiceInterface::class,
29-
"config",
3029
)]
3130
public function __construct(
3231
protected HalResponseFactory $responseFactory,
3332
protected ResourceGenerator $resourceGenerator,
3433
protected AdminServiceInterface $adminService,
35-
protected array $config,
3634
) {
3735
}
3836

@@ -58,14 +56,6 @@ public function get(ServerRequestInterface $request): ResponseInterface
5856
return $this->createResponse($request, $admin);
5957
}
6058

61-
/**
62-
* @throws BadRequestException
63-
*/
64-
public function getCollection(ServerRequestInterface $request): ResponseInterface
65-
{
66-
return $this->createResponse($request, $this->adminService->getAdmins($request->getQueryParams()));
67-
}
68-
6959
/**
7060
* @throws BadRequestException
7161
* @throws ConflictException
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Api\Admin\Handler;
6+
7+
use Api\Admin\Service\AdminRoleServiceInterface;
8+
use Api\App\Exception\BadRequestException;
9+
use Api\App\Handler\HandlerTrait;
10+
use Dot\DependencyInjection\Attribute\Inject;
11+
use Mezzio\Hal\HalResponseFactory;
12+
use Mezzio\Hal\ResourceGenerator;
13+
use Psr\Http\Message\ResponseInterface;
14+
use Psr\Http\Message\ServerRequestInterface;
15+
use Psr\Http\Server\RequestHandlerInterface;
16+
17+
class AdminRoleCollectionHandler implements RequestHandlerInterface
18+
{
19+
use HandlerTrait;
20+
21+
#[Inject(
22+
HalResponseFactory::class,
23+
ResourceGenerator::class,
24+
AdminRoleServiceInterface::class,
25+
)]
26+
public function __construct(
27+
protected HalResponseFactory $responseFactory,
28+
protected ResourceGenerator $resourceGenerator,
29+
protected AdminRoleServiceInterface $roleService,
30+
) {
31+
}
32+
33+
/**
34+
* @throws BadRequestException
35+
*/
36+
public function get(ServerRequestInterface $request): ResponseInterface
37+
{
38+
return $this->createResponse($request, $this->roleService->getAdminRoles($request->getQueryParams()));
39+
}
40+
}

src/Admin/src/Handler/AdminRoleHandler.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Api\Admin\Handler;
66

77
use Api\Admin\Service\AdminRoleServiceInterface;
8-
use Api\App\Exception\BadRequestException;
98
use Api\App\Exception\NotFoundException;
109
use Api\App\Handler\HandlerTrait;
1110
use Dot\DependencyInjection\Attribute\Inject;
@@ -23,13 +22,11 @@ class AdminRoleHandler implements RequestHandlerInterface
2322
HalResponseFactory::class,
2423
ResourceGenerator::class,
2524
AdminRoleServiceInterface::class,
26-
"config",
2725
)]
2826
public function __construct(
2927
protected HalResponseFactory $responseFactory,
3028
protected ResourceGenerator $resourceGenerator,
3129
protected AdminRoleServiceInterface $roleService,
32-
protected array $config,
3330
) {
3431
}
3532

@@ -42,12 +39,4 @@ public function get(ServerRequestInterface $request): ResponseInterface
4239

4340
return $this->createResponse($request, $role);
4441
}
45-
46-
/**
47-
* @throws BadRequestException
48-
*/
49-
public function getCollection(ServerRequestInterface $request): ResponseInterface
50-
{
51-
return $this->createResponse($request, $this->roleService->getAdminRoles($request->getQueryParams()));
52-
}
5342
}

src/Admin/src/RoutesDelegator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Api\Admin;
66

77
use Api\Admin\Handler\AdminAccountHandler;
8+
use Api\Admin\Handler\AdminCollectionHandler;
89
use Api\Admin\Handler\AdminHandler;
10+
use Api\Admin\Handler\AdminRoleCollectionHandler;
911
use Api\Admin\Handler\AdminRoleHandler;
1012
use Mezzio\Application;
1113
use Psr\Container\ContainerInterface;
@@ -44,7 +46,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal
4446
);
4547
$app->get(
4648
'/admin',
47-
AdminHandler::class,
49+
AdminCollectionHandler::class,
4850
'admin.list'
4951
);
5052
$app->patch(
@@ -60,7 +62,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal
6062

6163
$app->get(
6264
'/admin/role',
63-
AdminRoleHandler::class,
65+
AdminRoleCollectionHandler::class,
6466
'admin.role.list'
6567
);
6668
$app->get(

src/App/src/Handler/ErrorReportHandler.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ class ErrorReportHandler implements RequestHandlerInterface
2626
HalResponseFactory::class,
2727
ResourceGenerator::class,
2828
ErrorReportServiceInterface::class,
29-
"config",
3029
)]
3130
public function __construct(
3231
protected HalResponseFactory $responseFactory,
3332
protected ResourceGenerator $resourceGenerator,
3433
protected ErrorReportServiceInterface $errorReportService,
35-
protected array $config,
3634
) {
3735
}
3836

src/App/src/Handler/HandlerTrait.php

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,12 @@
1313
use Api\App\Exception\UnauthorizedException;
1414
use Dot\Mail\Exception\MailException;
1515
use Exception;
16-
use Fig\Http\Message\RequestMethodInterface;
1716
use Fig\Http\Message\StatusCodeInterface;
18-
use Mezzio\Hal\Metadata\MetadataMap;
19-
use Mezzio\Hal\Metadata\RouteBasedCollectionMetadata;
2017
use Mezzio\Hal\ResourceGenerator\Exception\OutOfBoundsException;
21-
use Mezzio\Router\RouteResult;
2218
use Psr\Http\Message\ResponseInterface;
2319
use Psr\Http\Message\ServerRequestInterface;
2420
use RuntimeException;
2521

26-
use function array_key_exists;
27-
use function assert;
28-
use function is_array;
2922
use function method_exists;
3023
use function sprintf;
3124
use function strtolower;
@@ -41,10 +34,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
4134
{
4235
try {
4336
$method = strtolower($request->getMethod());
44-
if ($this->isGetCollectionRequest($request, $this->config)) {
45-
$method = 'getCollection';
46-
}
47-
4837
if (! method_exists($this, $method)) {
4938
throw new MethodNotAllowedException(
5039
sprintf('Method %s is not implemented for the requested resource.', $method)
@@ -70,37 +59,4 @@ public function handle(ServerRequestInterface $request): ResponseInterface
7059
return $this->errorResponse($exception->getMessage());
7160
}
7261
}
73-
74-
/**
75-
* @throws RuntimeException
76-
*/
77-
private function isGetCollectionRequest(ServerRequestInterface $request, array $config): bool
78-
{
79-
if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) {
80-
return false;
81-
}
82-
83-
if (! array_key_exists(MetadataMap::class, $config)) {
84-
throw new RuntimeException(
85-
sprintf('Unable to load %s from config.', MetadataMap::class)
86-
);
87-
}
88-
89-
$routeResult = $request->getAttribute(RouteResult::class);
90-
assert($routeResult instanceof RouteResult);
91-
92-
$routeName = $routeResult->getMatchedRouteName();
93-
94-
$halConfig = null;
95-
foreach ($config[MetadataMap::class] as $cfg) {
96-
if ($cfg['route'] === $routeName) {
97-
$halConfig = $cfg;
98-
break;
99-
}
100-
}
101-
102-
return is_array($halConfig)
103-
&& array_key_exists('__class__', $halConfig)
104-
&& $halConfig['__class__'] === RouteBasedCollectionMetadata::class;
105-
}
10662
}

src/App/src/Handler/HomeHandler.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ class HomeHandler implements RequestHandlerInterface
2323
#[Inject(
2424
HalResponseFactory::class,
2525
ResourceGenerator::class,
26-
"config",
2726
)]
2827
public function __construct(
2928
protected HalResponseFactory $responseFactory,
3029
protected ResourceGenerator $resourceGenerator,
31-
protected array $config,
3230
) {
3331
}
3432

0 commit comments

Comments
 (0)