diff --git a/src/Admin/src/ConfigProvider.php b/src/Admin/src/ConfigProvider.php index df431a8..12d9a01 100644 --- a/src/Admin/src/ConfigProvider.php +++ b/src/Admin/src/ConfigProvider.php @@ -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; @@ -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, diff --git a/src/Admin/src/Handler/AdminAccountHandler.php b/src/Admin/src/Handler/AdminAccountHandler.php index 06a0a9c..fd8f76f 100644 --- a/src/Admin/src/Handler/AdminAccountHandler.php +++ b/src/Admin/src/Handler/AdminAccountHandler.php @@ -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, ) { } diff --git a/src/Admin/src/Handler/AdminCollectionHandler.php b/src/Admin/src/Handler/AdminCollectionHandler.php new file mode 100644 index 0000000..8d8b487 --- /dev/null +++ b/src/Admin/src/Handler/AdminCollectionHandler.php @@ -0,0 +1,40 @@ +createResponse($request, $this->adminService->getAdmins($request->getQueryParams())); + } +} diff --git a/src/Admin/src/Handler/AdminHandler.php b/src/Admin/src/Handler/AdminHandler.php index 18a169c..2a57133 100644 --- a/src/Admin/src/Handler/AdminHandler.php +++ b/src/Admin/src/Handler/AdminHandler.php @@ -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, ) { } @@ -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 diff --git a/src/Admin/src/Handler/AdminRoleCollectionHandler.php b/src/Admin/src/Handler/AdminRoleCollectionHandler.php new file mode 100644 index 0000000..d4e5536 --- /dev/null +++ b/src/Admin/src/Handler/AdminRoleCollectionHandler.php @@ -0,0 +1,40 @@ +createResponse($request, $this->roleService->getAdminRoles($request->getQueryParams())); + } +} diff --git a/src/Admin/src/Handler/AdminRoleHandler.php b/src/Admin/src/Handler/AdminRoleHandler.php index fdc2908..18b377a 100644 --- a/src/Admin/src/Handler/AdminRoleHandler.php +++ b/src/Admin/src/Handler/AdminRoleHandler.php @@ -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; @@ -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, ) { } @@ -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())); - } } diff --git a/src/Admin/src/RoutesDelegator.php b/src/Admin/src/RoutesDelegator.php index 0239f78..06090a5 100644 --- a/src/Admin/src/RoutesDelegator.php +++ b/src/Admin/src/RoutesDelegator.php @@ -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; @@ -44,7 +46,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal ); $app->get( '/admin', - AdminHandler::class, + AdminCollectionHandler::class, 'admin.list' ); $app->patch( @@ -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( diff --git a/src/App/src/Handler/ErrorReportHandler.php b/src/App/src/Handler/ErrorReportHandler.php index 5f2e4b2..a687139 100644 --- a/src/App/src/Handler/ErrorReportHandler.php +++ b/src/App/src/Handler/ErrorReportHandler.php @@ -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, ) { } diff --git a/src/App/src/Handler/HandlerTrait.php b/src/App/src/Handler/HandlerTrait.php index 08c1ae3..7e1b5f4 100644 --- a/src/App/src/Handler/HandlerTrait.php +++ b/src/App/src/Handler/HandlerTrait.php @@ -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; @@ -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) @@ -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; - } } diff --git a/src/App/src/Handler/HomeHandler.php b/src/App/src/Handler/HomeHandler.php index bcd8738..ace153a 100644 --- a/src/App/src/Handler/HomeHandler.php +++ b/src/App/src/Handler/HomeHandler.php @@ -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, ) { } diff --git a/src/User/src/ConfigProvider.php b/src/User/src/ConfigProvider.php index 30589f9..976326b 100644 --- a/src/User/src/ConfigProvider.php +++ b/src/User/src/ConfigProvider.php @@ -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; @@ -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, diff --git a/src/User/src/Handler/AccountActivateHandler.php b/src/User/src/Handler/AccountActivateHandler.php index 8024f67..cdd1275 100644 --- a/src/User/src/Handler/AccountActivateHandler.php +++ b/src/User/src/Handler/AccountActivateHandler.php @@ -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, ) { } diff --git a/src/User/src/Handler/AccountAvatarHandler.php b/src/User/src/Handler/AccountAvatarHandler.php index 3ba78e8..1bfa700 100644 --- a/src/User/src/Handler/AccountAvatarHandler.php +++ b/src/User/src/Handler/AccountAvatarHandler.php @@ -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, ) { } diff --git a/src/User/src/Handler/AccountHandler.php b/src/User/src/Handler/AccountHandler.php index 9713299..89567b9 100644 --- a/src/User/src/Handler/AccountHandler.php +++ b/src/User/src/Handler/AccountHandler.php @@ -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, ) { } diff --git a/src/User/src/Handler/AccountRecoveryHandler.php b/src/User/src/Handler/AccountRecoveryHandler.php index 98d0393..fef7d3c 100644 --- a/src/User/src/Handler/AccountRecoveryHandler.php +++ b/src/User/src/Handler/AccountRecoveryHandler.php @@ -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, ) { } diff --git a/src/User/src/Handler/AccountResetPasswordHandler.php b/src/User/src/Handler/AccountResetPasswordHandler.php index 52977ce..f3af021 100644 --- a/src/User/src/Handler/AccountResetPasswordHandler.php +++ b/src/User/src/Handler/AccountResetPasswordHandler.php @@ -33,13 +33,11 @@ class AccountResetPasswordHandler implements RequestHandlerInterface HalResponseFactory::class, ResourceGenerator::class, UserServiceInterface::class, - "config", )] public function __construct( protected HalResponseFactory $responseFactory, protected ResourceGenerator $resourceGenerator, protected UserServiceInterface $userService, - protected array $config, ) { } diff --git a/src/User/src/Handler/UserActivateHandler.php b/src/User/src/Handler/UserActivateHandler.php index b837a17..27e67f1 100644 --- a/src/User/src/Handler/UserActivateHandler.php +++ b/src/User/src/Handler/UserActivateHandler.php @@ -25,13 +25,11 @@ class UserActivateHandler implements RequestHandlerInterface HalResponseFactory::class, ResourceGenerator::class, UserServiceInterface::class, - "config", )] public function __construct( protected HalResponseFactory $responseFactory, protected ResourceGenerator $resourceGenerator, protected UserServiceInterface $userService, - protected array $config, ) { } diff --git a/src/User/src/Handler/UserAvatarHandler.php b/src/User/src/Handler/UserAvatarHandler.php index bd22d60..0c2cb1e 100644 --- a/src/User/src/Handler/UserAvatarHandler.php +++ b/src/User/src/Handler/UserAvatarHandler.php @@ -27,14 +27,12 @@ class UserAvatarHandler implements RequestHandlerInterface ResourceGenerator::class, UserServiceInterface::class, UserAvatarServiceInterface::class, - "config", )] public function __construct( protected HalResponseFactory $responseFactory, protected ResourceGenerator $resourceGenerator, protected UserServiceInterface $userService, protected UserAvatarServiceInterface $userAvatarService, - protected array $config, ) { } diff --git a/src/User/src/Handler/UserCollectionHandler.php b/src/User/src/Handler/UserCollectionHandler.php new file mode 100644 index 0000000..dfa94f4 --- /dev/null +++ b/src/User/src/Handler/UserCollectionHandler.php @@ -0,0 +1,40 @@ +createResponse($request, $this->userService->getUsers($request->getQueryParams())); + } +} diff --git a/src/User/src/Handler/UserHandler.php b/src/User/src/Handler/UserHandler.php index f6b4755..490dcb4 100644 --- a/src/User/src/Handler/UserHandler.php +++ b/src/User/src/Handler/UserHandler.php @@ -28,13 +28,11 @@ class UserHandler implements RequestHandlerInterface HalResponseFactory::class, ResourceGenerator::class, UserServiceInterface::class, - "config", )] public function __construct( protected HalResponseFactory $responseFactory, protected ResourceGenerator $resourceGenerator, protected UserServiceInterface $userService, - protected array $config, ) { } @@ -61,14 +59,6 @@ public function get(ServerRequestInterface $request): ResponseInterface return $this->createResponse($request, $user); } - /** - * @throws BadRequestException - */ - public function getCollection(ServerRequestInterface $request): ResponseInterface - { - return $this->createResponse($request, $this->userService->getUsers($request->getQueryParams())); - } - /** * @throws BadRequestException * @throws ConflictException diff --git a/src/User/src/Handler/UserRoleCollectionHandler.php b/src/User/src/Handler/UserRoleCollectionHandler.php new file mode 100644 index 0000000..10df222 --- /dev/null +++ b/src/User/src/Handler/UserRoleCollectionHandler.php @@ -0,0 +1,40 @@ +createResponse($request, $this->roleService->getRoles($request->getQueryParams())); + } +} diff --git a/src/User/src/Handler/UserRoleHandler.php b/src/User/src/Handler/UserRoleHandler.php index 70ddefe..d7599b9 100644 --- a/src/User/src/Handler/UserRoleHandler.php +++ b/src/User/src/Handler/UserRoleHandler.php @@ -4,7 +4,6 @@ namespace Api\User\Handler; -use Api\App\Exception\BadRequestException; use Api\App\Exception\NotFoundException; use Api\App\Handler\HandlerTrait; use Api\User\Service\UserRoleServiceInterface; @@ -23,13 +22,11 @@ class UserRoleHandler implements RequestHandlerInterface HalResponseFactory::class, ResourceGenerator::class, UserRoleServiceInterface::class, - "config", )] public function __construct( protected HalResponseFactory $responseFactory, protected ResourceGenerator $resourceGenerator, protected UserRoleServiceInterface $roleService, - protected array $config, ) { } @@ -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->getRoles($request->getQueryParams())); - } } diff --git a/src/User/src/RoutesDelegator.php b/src/User/src/RoutesDelegator.php index 2769d0f..6dd2e50 100644 --- a/src/User/src/RoutesDelegator.php +++ b/src/User/src/RoutesDelegator.php @@ -11,7 +11,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 Mezzio\Application; use Psr\Container\ContainerInterface; @@ -43,7 +45,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal ); $app->get( '/user', - UserHandler::class, + UserCollectionHandler::class, 'user.list' ); $app->patch( @@ -81,7 +83,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal $app->get( '/user/role', - UserRoleHandler::class, + UserRoleCollectionHandler::class, 'user.role.list' ); $app->get(