Skip to content

Commit

Permalink
Refactor: Transfer responsibility from handlers to services.
Browse files Browse the repository at this point in the history
Signed-off-by: alexmerlin <[email protected]>
  • Loading branch information
alexmerlin committed Jun 4, 2024
1 parent acfa80f commit a74725f
Show file tree
Hide file tree
Showing 38 changed files with 235 additions and 238 deletions.
5 changes: 0 additions & 5 deletions src/Admin/src/Command/AdminCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function getData(InputInterface $input): array
{
$role = $this->adminRoleService->findOneBy(['name' => AdminRole::ROLE_ADMIN]);
if (! $role instanceof AdminRole) {
throw new NotFoundException(
sprintf(Message::ADMIN_ROLE_MISSING, AdminRole::ROLE_ADMIN)
);
}

return [
'identity' => $input->getOption('identity'),
Expand Down
13 changes: 9 additions & 4 deletions src/Admin/src/Factory/AdminCreateCommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function assert;

class AdminCreateCommandFactory
{
/**
Expand All @@ -19,9 +21,12 @@ class AdminCreateCommandFactory
*/
public function __invoke(ContainerInterface $container): AdminCreateCommand
{
return new AdminCreateCommand(
$container->get(AdminService::class),
$container->get(AdminRoleService::class)
);
$adminService = $container->get(AdminService::class);
assert($adminService instanceof AdminService);

$adminRoleService = $container->get(AdminRoleService::class);
assert($adminRoleService instanceof AdminRoleService);

return new AdminCreateCommand($adminService, $adminRoleService);
}
}
29 changes: 5 additions & 24 deletions src/Admin/src/Handler/AdminHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@

namespace Api\Admin\Handler;

use Api\Admin\Entity\Admin;
use Api\Admin\InputFilter\CreateAdminInputFilter;
use Api\Admin\InputFilter\UpdateAdminInputFilter;
use Api\Admin\Service\AdminServiceInterface;
use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\HandlerTrait;
use Api\App\Message;
use Dot\AnnotatedServices\Annotation\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function sprintf;

class AdminHandler implements RequestHandlerInterface
{
use HandlerTrait;
Expand All @@ -47,12 +43,7 @@ public function __construct(
*/
public function delete(ServerRequestInterface $request): ResponseInterface
{
$uuid = $request->getAttribute('uuid');

$admin = $this->adminService->findOneBy(['uuid' => $uuid]);
if (! $admin instanceof Admin) {
throw new NotFoundException(sprintf(Message::NOT_FOUND_BY_UUID, 'admin', $uuid));
}
$admin = $this->adminService->findOneBy(['uuid' => $request->getAttribute('uuid')]);

$this->adminService->deleteAdmin($admin);

Expand All @@ -64,12 +55,7 @@ public function delete(ServerRequestInterface $request): ResponseInterface
*/
public function get(ServerRequestInterface $request): ResponseInterface
{
$uuid = $request->getAttribute('uuid');

$admin = $this->adminService->findOneBy(['uuid' => $uuid]);
if (! $admin instanceof Admin) {
throw new NotFoundException(sprintf(Message::NOT_FOUND_BY_UUID, 'admin', $uuid));
}
$admin = $this->adminService->findOneBy(['uuid' => $request->getAttribute('uuid')]);

return $this->createResponse($request, $admin);
}
Expand All @@ -91,15 +77,10 @@ public function patch(ServerRequestInterface $request): ResponseInterface
throw (new BadRequestException())->setMessages($inputFilter->getMessages());
}

$uuid = $request->getAttribute('uuid');
$admin = $this->adminService->findOneBy(['uuid' => $uuid]);
if (! $admin instanceof Admin) {
throw new NotFoundException(sprintf(Message::NOT_FOUND_BY_UUID, 'admin', $uuid));
}

$user = $this->adminService->updateAdmin($admin, $inputFilter->getValues());
$admin = $this->adminService->findOneBy(['uuid' => $request->getAttribute('uuid')]);
$this->adminService->updateAdmin($admin, $inputFilter->getValues());

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

/**
Expand Down
10 changes: 1 addition & 9 deletions src/Admin/src/Handler/AdminRoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@

namespace Api\Admin\Handler;

use Api\Admin\Entity\AdminRole;
use Api\Admin\Service\AdminRoleServiceInterface;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\HandlerTrait;
use Api\App\Message;
use Dot\AnnotatedServices\Annotation\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function sprintf;

class AdminRoleHandler implements RequestHandlerInterface
{
use HandlerTrait;
Expand All @@ -43,11 +39,7 @@ public function __construct(
*/
public function get(ServerRequestInterface $request): ResponseInterface
{
$uuid = $request->getAttribute('uuid');
$role = $this->roleService->findOneBy(['uuid' => $uuid]);
if (! $role instanceof AdminRole) {
throw new NotFoundException(sprintf(Message::NOT_FOUND_BY_UUID, 'role', $uuid));
}
$role = $this->roleService->findOneBy(['uuid' => $request->getAttribute('uuid')]);

return $this->createResponse($request, $role);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Admin/src/RoutesDelegator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
use Mezzio\Application;
use Psr\Container\ContainerInterface;

use function assert;

class RoutesDelegator
{
public function __invoke(ContainerInterface $container, string $serviceName, callable $callback): Application
{
/** @var Application $app */
$app = $callback();
assert($app instanceof Application);

$uuid = \Api\App\RoutesDelegator::REGEXP_UUID;

Expand Down
13 changes: 9 additions & 4 deletions src/Admin/src/Service/AdminRoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Api\Admin\Collection\AdminRoleCollection;
use Api\Admin\Entity\AdminRole;
use Api\Admin\Repository\AdminRoleRepository;
use Api\App\Exception\NotFoundException;
use Api\App\Message;
use Dot\AnnotatedServices\Annotation\Inject;

class AdminRoleService implements AdminRoleServiceInterface
Expand All @@ -21,14 +23,17 @@ public function __construct(
) {
}

public function findOneBy(array $params = []): ?AdminRole
/**
* @throws NotFoundException
*/
public function findOneBy(array $params = []): AdminRole
{
$role = $this->adminRoleRepository->findOneBy($params);
if ($role instanceof AdminRole) {
return $role;
if (! $role instanceof AdminRole) {
throw new NotFoundException(Message::ROLE_NOT_FOUND);
}

return null;
return $role;
}

public function getAdminRoles(array $params = []): AdminRoleCollection
Expand Down
6 changes: 5 additions & 1 deletion src/Admin/src/Service/AdminRoleServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

use Api\Admin\Collection\AdminRoleCollection;
use Api\Admin\Entity\AdminRole;
use Api\App\Exception\NotFoundException;

interface AdminRoleServiceInterface
{
public function findOneBy(array $params = []): ?AdminRole;
/**
* @throws NotFoundException
*/
public function findOneBy(array $params = []): AdminRole;

public function getAdminRoles(array $params = []): AdminRoleCollection;
}
47 changes: 21 additions & 26 deletions src/Admin/src/Service/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use Api\App\Message;
use Dot\AnnotatedServices\Annotation\Inject;

use function sprintf;

class AdminService implements AdminServiceInterface
{
/**
Expand Down Expand Up @@ -48,22 +46,14 @@ public function createAdmin(array $data = []): Admin

if (! empty($data['roles'])) {
foreach ($data['roles'] as $roleData) {
$role = $this->adminRoleService->findOneBy(['uuid' => $roleData['uuid']]);
if (! $role instanceof AdminRole) {
throw new NotFoundException(
sprintf(Message::NOT_FOUND_BY_UUID, 'role', $roleData['uuid'])
);
}
$admin->addRole($role);
}
} else {
$role = $this->adminRoleService->findOneBy(['name' => AdminRole::ROLE_ADMIN]);
if (! $role instanceof AdminRole) {
throw new NotFoundException(
sprintf(Message::NOT_FOUND_BY_NAME, 'role', AdminRole::ROLE_ADMIN)
$admin->addRole(
$this->adminRoleService->findOneBy(['uuid' => $roleData['uuid']])
);
}
$admin->addRole($role);
} else {
$admin->addRole(
$this->adminRoleService->findOneBy(['name' => AdminRole::ROLE_ADMIN])
);
}

return $this->adminRepository->saveAdmin($admin);
Expand All @@ -78,17 +68,24 @@ public function deleteAdmin(Admin $admin): void

public function exists(string $identity = ''): bool
{
return $this->findOneBy(['identity' => $identity]) instanceof Admin;
try {
return $this->findOneBy(['identity' => $identity]) instanceof Admin;
} catch (NotFoundException) {
return false;
}
}

public function findOneBy(array $params = []): ?Admin
/**
* @throws NotFoundException
*/
public function findOneBy(array $params = []): Admin
{
$admin = $this->adminRepository->findOneBy($params);
if ($admin instanceof Admin) {
return $admin;
if (! $admin instanceof Admin) {
throw new NotFoundException(Message::ADMIN_NOT_FOUND);
}

return null;
return $admin;
}

public function getAdmins(array $params = []): AdminCollection
Expand Down Expand Up @@ -121,11 +118,9 @@ public function updateAdmin(Admin $admin, array $data = []): Admin
if (! empty($data['roles'])) {
$admin->resetRoles();
foreach ($data['roles'] as $roleData) {
$role = $this->adminRoleService->findOneBy(['uuid' => $roleData['uuid']]);
if (! $role instanceof AdminRole) {
throw new NotFoundException(sprintf(Message::NOT_FOUND_BY_UUID, 'role', $roleData['uuid']));
}
$admin->addRole($role);
$admin->addRole(
$this->adminRoleService->findOneBy(['uuid' => $roleData['uuid']])
);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Admin/src/Service/AdminServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public function deleteAdmin(Admin $admin): void;

public function exists(string $identity = ''): bool;

public function findOneBy(array $params = []): ?Admin;
/**
* @throws NotFoundException
*/
public function findOneBy(array $params = []): Admin;

public function getAdmins(array $params = []): AdminCollection;

Expand Down
4 changes: 3 additions & 1 deletion src/App/src/Entity/OAuthAuthCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Entities\Traits\AuthCodeTrait;

use function assert;

#[ORM\Entity(repositoryClass: OAuthAuthCodeRepository::class)]
#[ORM\Table(name: "oauth_auth_codes")]
class OAuthAuthCode implements AuthCodeEntityInterface
Expand Down Expand Up @@ -97,8 +99,8 @@ public function setUserIdentifier($identifier): self

public function getUserIdentifier(): ?string
{
/** @var OAuthClient $client */
$client = $this->getClient();
assert($client instanceof OAuthClient);

if (null === $user = $client->getUser()) {
return null;
Expand Down
9 changes: 6 additions & 3 deletions src/App/src/Factory/AuthenticationMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function assert;

class AuthenticationMiddlewareFactory
{
/**
Expand All @@ -23,8 +25,9 @@ public function __invoke(ContainerInterface $container): AuthenticationMiddlewar
throw new InvalidConfigException('AuthenticationInterface service is missing');
}

return new AuthenticationMiddleware(
$container->get(AuthenticationInterface::class)
);
$authentication = $container->get(AuthenticationInterface::class);
assert($authentication instanceof AuthenticationInterface);

return new AuthenticationMiddleware($authentication);
}
}
4 changes: 4 additions & 0 deletions src/App/src/Factory/ErrorResponseGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function assert;
use function is_array;

class ErrorResponseGeneratorFactory
{
/**
Expand All @@ -18,6 +21,7 @@ class ErrorResponseGeneratorFactory
public function __invoke(ContainerInterface $container): ErrorResponseGenerator
{
$config = $container->has('config') ? $container->get('config') : [];
assert(is_array($config));

return new ErrorResponseGenerator($config['debug'] ?? false);
}
Expand Down
4 changes: 3 additions & 1 deletion src/App/src/Factory/OAuthAccessTokenRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function assert;

class OAuthAccessTokenRepositoryFactory
{
/**
Expand All @@ -19,8 +21,8 @@ class OAuthAccessTokenRepositoryFactory
*/
public function __invoke(ContainerInterface $container): ObjectRepository
{
/** @var EntityManagerInterface $entityManager */
$entityManager = $container->get(EntityManagerInterface::class);
assert($entityManager instanceof EntityManagerInterface);

return $entityManager->getRepository(OAuthAccessToken::class);
}
Expand Down
4 changes: 3 additions & 1 deletion src/App/src/Factory/OAuthAuthCodeRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

use function assert;

class OAuthAuthCodeRepositoryFactory
{
/**
Expand All @@ -19,8 +21,8 @@ class OAuthAuthCodeRepositoryFactory
*/
public function __invoke(ContainerInterface $container): ObjectRepository
{
/** @var EntityManagerInterface $entityManager */
$entityManager = $container->get(EntityManagerInterface::class);
assert($entityManager instanceof EntityManagerInterface);

return $entityManager->getRepository(OAuthAuthCode::class);
}
Expand Down
Loading

0 comments on commit a74725f

Please sign in to comment.