Skip to content

Commit

Permalink
Issue #313: Remove config dependency from handlers.
Browse files Browse the repository at this point in the history
Signed-off-by: alexmerlin <[email protected]>
  • Loading branch information
alexmerlin committed Sep 10, 2024
1 parent 5cac6b3 commit 59a9f33
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 118 deletions.
20 changes: 12 additions & 8 deletions src/Admin/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Api\Admin\Entity\AdminRole;
use Api\Admin\Factory\AdminCreateCommandFactory;
use Api\Admin\Handler\AdminAccountHandler;
use Api\Admin\Handler\AdminCollectionHandler;
use Api\Admin\Handler\AdminHandler;
use Api\Admin\Handler\AdminRoleCollectionHandler;
use Api\Admin\Handler\AdminRoleHandler;
use Api\Admin\Repository\AdminRepository;
use Api\Admin\Repository\AdminRoleRepository;
Expand Down Expand Up @@ -46,14 +48,16 @@ public function getDependencies(): array
],
],
'factories' => [
AdminHandler::class => AttributedServiceFactory::class,
AdminAccountHandler::class => AttributedServiceFactory::class,
AdminRoleHandler::class => AttributedServiceFactory::class,
AdminService::class => AttributedServiceFactory::class,
AdminRoleService::class => AttributedServiceFactory::class,
AdminCreateCommand::class => AdminCreateCommandFactory::class,
AdminRepository::class => AttributedRepositoryFactory::class,
AdminRoleRepository::class => AttributedRepositoryFactory::class,
AdminHandler::class => AttributedServiceFactory::class,
AdminCollectionHandler::class => AttributedServiceFactory::class,
AdminAccountHandler::class => AttributedServiceFactory::class,
AdminRoleHandler::class => AttributedServiceFactory::class,
AdminRoleCollectionHandler::class => AttributedServiceFactory::class,
AdminService::class => AttributedServiceFactory::class,
AdminRoleService::class => AttributedServiceFactory::class,
AdminCreateCommand::class => AdminCreateCommandFactory::class,
AdminRepository::class => AttributedRepositoryFactory::class,
AdminRoleRepository::class => AttributedRepositoryFactory::class,
],
'aliases' => [
AdminServiceInterface::class => AdminService::class,
Expand Down
2 changes: 0 additions & 2 deletions src/Admin/src/Handler/AdminAccountHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class AdminAccountHandler implements RequestHandlerInterface
HalResponseFactory::class,
ResourceGenerator::class,
AdminServiceInterface::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminServiceInterface $adminService,
protected array $config,
) {
}

Expand Down
40 changes: 40 additions & 0 deletions src/Admin/src/Handler/AdminCollectionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Api\Admin\Handler;

use Api\Admin\Service\AdminServiceInterface;
use Api\App\Exception\BadRequestException;
use Api\App\Handler\HandlerTrait;
use Dot\DependencyInjection\Attribute\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AdminCollectionHandler implements RequestHandlerInterface
{
use HandlerTrait;

#[Inject(
HalResponseFactory::class,
ResourceGenerator::class,
AdminServiceInterface::class,
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminServiceInterface $adminService,
) {
}

/**
* @throws BadRequestException
*/
public function get(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse($request, $this->adminService->getAdmins($request->getQueryParams()));
}
}
10 changes: 0 additions & 10 deletions src/Admin/src/Handler/AdminHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class AdminHandler implements RequestHandlerInterface
HalResponseFactory::class,
ResourceGenerator::class,
AdminServiceInterface::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminServiceInterface $adminService,
protected array $config,
) {
}

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

/**
* @throws BadRequestException
*/
public function getCollection(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse($request, $this->adminService->getAdmins($request->getQueryParams()));
}

/**
* @throws BadRequestException
* @throws ConflictException
Expand Down
40 changes: 40 additions & 0 deletions src/Admin/src/Handler/AdminRoleCollectionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Api\Admin\Handler;

use Api\Admin\Service\AdminRoleServiceInterface;
use Api\App\Exception\BadRequestException;
use Api\App\Handler\HandlerTrait;
use Dot\DependencyInjection\Attribute\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AdminRoleCollectionHandler implements RequestHandlerInterface
{
use HandlerTrait;

#[Inject(
HalResponseFactory::class,
ResourceGenerator::class,
AdminRoleServiceInterface::class,
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminRoleServiceInterface $roleService,
) {
}

/**
* @throws BadRequestException
*/
public function get(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse($request, $this->roleService->getAdminRoles($request->getQueryParams()));
}
}
11 changes: 0 additions & 11 deletions src/Admin/src/Handler/AdminRoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Api\Admin\Handler;

use Api\Admin\Service\AdminRoleServiceInterface;
use Api\App\Exception\BadRequestException;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\HandlerTrait;
use Dot\DependencyInjection\Attribute\Inject;
Expand All @@ -23,13 +22,11 @@ class AdminRoleHandler implements RequestHandlerInterface
HalResponseFactory::class,
ResourceGenerator::class,
AdminRoleServiceInterface::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminRoleServiceInterface $roleService,
protected array $config,
) {
}

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

return $this->createResponse($request, $role);
}

/**
* @throws BadRequestException
*/
public function getCollection(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse($request, $this->roleService->getAdminRoles($request->getQueryParams()));
}
}
6 changes: 4 additions & 2 deletions src/Admin/src/RoutesDelegator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Api\Admin;

use Api\Admin\Handler\AdminAccountHandler;
use Api\Admin\Handler\AdminCollectionHandler;
use Api\Admin\Handler\AdminHandler;
use Api\Admin\Handler\AdminRoleCollectionHandler;
use Api\Admin\Handler\AdminRoleHandler;
use Mezzio\Application;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -44,7 +46,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal
);
$app->get(
'/admin',
AdminHandler::class,
AdminCollectionHandler::class,
'admin.list'
);
$app->patch(
Expand All @@ -60,7 +62,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal

$app->get(
'/admin/role',
AdminRoleHandler::class,
AdminRoleCollectionHandler::class,
'admin.role.list'
);
$app->get(
Expand Down
2 changes: 0 additions & 2 deletions src/App/src/Handler/ErrorReportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class ErrorReportHandler implements RequestHandlerInterface
HalResponseFactory::class,
ResourceGenerator::class,
ErrorReportServiceInterface::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected ErrorReportServiceInterface $errorReportService,
protected array $config,
) {
}

Expand Down
44 changes: 0 additions & 44 deletions src/App/src/Handler/HandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@
use Api\App\Exception\UnauthorizedException;
use Dot\Mail\Exception\MailException;
use Exception;
use Fig\Http\Message\RequestMethodInterface;
use Fig\Http\Message\StatusCodeInterface;
use Mezzio\Hal\Metadata\MetadataMap;
use Mezzio\Hal\Metadata\RouteBasedCollectionMetadata;
use Mezzio\Hal\ResourceGenerator\Exception\OutOfBoundsException;
use Mezzio\Router\RouteResult;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;

use function array_key_exists;
use function assert;
use function is_array;
use function method_exists;
use function sprintf;
use function strtolower;
Expand All @@ -41,10 +34,6 @@ public function handle(ServerRequestInterface $request): 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)
Expand All @@ -70,37 +59,4 @@ public function handle(ServerRequestInterface $request): ResponseInterface
return $this->errorResponse($exception->getMessage());
}
}

/**
* @throws RuntimeException
*/
private function isGetCollectionRequest(ServerRequestInterface $request, array $config): bool
{
if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) {
return false;
}

if (! array_key_exists(MetadataMap::class, $config)) {
throw new RuntimeException(
sprintf('Unable to load %s from config.', MetadataMap::class)
);
}

$routeResult = $request->getAttribute(RouteResult::class);
assert($routeResult instanceof RouteResult);

$routeName = $routeResult->getMatchedRouteName();

$halConfig = null;
foreach ($config[MetadataMap::class] as $cfg) {
if ($cfg['route'] === $routeName) {
$halConfig = $cfg;
break;
}
}

return is_array($halConfig)
&& array_key_exists('__class__', $halConfig)
&& $halConfig['__class__'] === RouteBasedCollectionMetadata::class;
}
}
2 changes: 0 additions & 2 deletions src/App/src/Handler/HomeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ class HomeHandler implements RequestHandlerInterface
#[Inject(
HalResponseFactory::class,
ResourceGenerator::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected array $config,
) {
}

Expand Down
4 changes: 4 additions & 0 deletions src/User/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use Api\User\Handler\AccountResetPasswordHandler;
use Api\User\Handler\UserActivateHandler;
use Api\User\Handler\UserAvatarHandler;
use Api\User\Handler\UserCollectionHandler;
use Api\User\Handler\UserHandler;
use Api\User\Handler\UserRoleCollectionHandler;
use Api\User\Handler\UserRoleHandler;
use Api\User\Repository\UserAvatarRepository;
use Api\User\Repository\UserDetailRepository;
Expand Down Expand Up @@ -67,7 +69,9 @@ public function getDependencies(): array
UserAvatarHandler::class => AttributedServiceFactory::class,
UserAvatarEventListener::class => AttributedServiceFactory::class,
UserHandler::class => AttributedServiceFactory::class,
UserCollectionHandler::class => AttributedServiceFactory::class,
UserRoleHandler::class => AttributedServiceFactory::class,
UserRoleCollectionHandler::class => AttributedServiceFactory::class,
UserService::class => AttributedServiceFactory::class,
UserRoleService::class => AttributedServiceFactory::class,
UserAvatarService::class => AttributedServiceFactory::class,
Expand Down
2 changes: 0 additions & 2 deletions src/User/src/Handler/AccountActivateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ class AccountActivateHandler implements RequestHandlerInterface
HalResponseFactory::class,
ResourceGenerator::class,
UserServiceInterface::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected UserServiceInterface $userService,
protected array $config,
) {
}

Expand Down
2 changes: 0 additions & 2 deletions src/User/src/Handler/AccountAvatarHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class AccountAvatarHandler implements RequestHandlerInterface
HalResponseFactory::class,
ResourceGenerator::class,
UserAvatarServiceInterface::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected UserAvatarServiceInterface $userAvatarService,
protected array $config,
) {
}

Expand Down
2 changes: 0 additions & 2 deletions src/User/src/Handler/AccountHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ class AccountHandler implements RequestHandlerInterface
HalResponseFactory::class,
ResourceGenerator::class,
UserServiceInterface::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected UserServiceInterface $userService,
protected array $config,
) {
}

Expand Down
2 changes: 0 additions & 2 deletions src/User/src/Handler/AccountRecoveryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class AccountRecoveryHandler implements RequestHandlerInterface
HalResponseFactory::class,
ResourceGenerator::class,
UserServiceInterface::class,
"config",
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected UserServiceInterface $userService,
protected array $config,
) {
}

Expand Down
Loading

0 comments on commit 59a9f33

Please sign in to comment.