Skip to content

Commit

Permalink
Issue #330: Move common logic to Core module
Browse files Browse the repository at this point in the history
Signed-off-by: alexmerlin <[email protected]>
  • Loading branch information
alexmerlin committed Dec 1, 2024
1 parent abd7cd5 commit 982d341
Show file tree
Hide file tree
Showing 45 changed files with 108 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/Admin/src/Command/AdminCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
use Api\App\Exception\NotFoundException;
use Api\App\Message;
use Core\Admin\Entity\AdminRole;
use Core\App\Message;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/src/InputFilter/Input/FirstNameInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Api\Admin\InputFilter\Input;

use Api\App\Message;
use Core\App\Message;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\InputFilter\Input;
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/src/InputFilter/Input/IdentityInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Api\Admin\InputFilter\Input;

use Api\App\Message;
use Core\App\Message;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\InputFilter\Input;
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/src/InputFilter/Input/LastNameInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Api\Admin\InputFilter\Input;

use Api\App\Message;
use Core\App\Message;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\InputFilter\Input;
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/src/InputFilter/Input/PasswordConfirmInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Api\Admin\InputFilter\Input;

use Api\App\Message;
use Core\App\Message;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\InputFilter\Input;
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/src/InputFilter/Input/PasswordInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Api\Admin\InputFilter\Input;

use Api\App\Message;
use Core\App\Message;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\InputFilter\Input;
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/src/InputFilter/Input/StatusInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Api\Admin\InputFilter\Input;

use Api\App\Message;
use Core\Admin\Enum\AdminStatusEnum;
use Core\App\Message;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\InputFilter\Input;
Expand Down
7 changes: 5 additions & 2 deletions src/Admin/src/Service/AdminRoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Api\Admin\Collection\AdminRoleCollection;
use Api\App\Exception\BadRequestException;
use Api\App\Exception\NotFoundException;
use Api\App\Message;
use Core\Admin\Entity\AdminRole;
use Core\Admin\Repository\AdminRoleRepository;
use Core\App\Message;
use Dot\DependencyInjection\Attribute\Inject;

use function in_array;
Expand Down Expand Up @@ -54,6 +54,9 @@ public function getAdminRoles(array $params = []): AdminRoleCollection
throw (new BadRequestException())->setMessages([sprintf(Message::INVALID_VALUE, 'order')]);
}

return $this->adminRoleRepository->getAdminRoles($params);
$qb = $this->adminRoleRepository->getAdminRoles($params);
$qb->getQuery()->useQueryCache(true);

return new AdminRoleCollection($qb, false);
}
}
7 changes: 5 additions & 2 deletions src/Admin/src/Service/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
use Api\App\Exception\NotFoundException;
use Api\App\Message;
use Core\Admin\Entity\Admin;
use Core\Admin\Enum\AdminStatusEnum;
use Core\Admin\Repository\AdminRepository;
use Core\App\Message;
use Dot\DependencyInjection\Attribute\Inject;

use function in_array;
Expand Down Expand Up @@ -110,7 +110,10 @@ public function getAdmins(array $params = []): AdminCollection
throw (new BadRequestException())->setMessages([sprintf(Message::INVALID_VALUE, 'order')]);
}

return $this->adminRepository->getAdmins($params);
$qb = $this->adminRepository->getAdmins($params);
$qb->getQuery()->useQueryCache(true);

return new AdminCollection($qb, false);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Attribute/BaseDeprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Api\App\Attribute;

use Api\App\Exception\DeprecationSunsetException;
use Api\App\Message;
use Core\App\Message;
use Laminas\Validator\Date;

use function sprintf;
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Factory/HandlerDelegatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Api\App\Exception\RuntimeException;
use Api\App\Handler\AbstractHandler;
use Api\App\Message;
use Core\App\Message;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Container\ContainerExceptionInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Api\App\Exception\NotFoundException;
use Api\App\Exception\RuntimeException;
use Api\App\Exception\UnauthorizedException;
use Api\App\Message;
use Core\App\Entity\EntityInterface;
use Core\App\Message;
use Dot\Mail\Exception\MailException;
use Exception;
use Fig\Http\Message\StatusCodeInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Handler/ErrorReportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Api\App\Attribute\MethodDeprecation;
use Api\App\Exception\ForbiddenException;
use Api\App\Exception\UnauthorizedException;
use Api\App\Message;
use Api\App\Service\ErrorReportServiceInterface;
use Core\App\Message;
use Dot\DependencyInjection\Attribute\Inject;
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down
26 changes: 0 additions & 26 deletions src/App/src/Helper/PaginationHelper.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/App/src/Middleware/AuthorizationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Api\App\Middleware;

use Api\App\Message;
use Core\Admin\Entity\Admin;
use Core\Admin\Repository\AdminRepository;
use Core\App\Entity\Guest;
use Core\App\Entity\RoleInterface;
use Core\App\Message;
use Core\User\Entity\User;
use Core\User\Repository\UserRepository;
use Core\User\UserIdentity;
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Middleware/DeprecationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Api\App\Attribute\MethodDeprecation;
use Api\App\Attribute\ResourceDeprecation;
use Api\App\Exception\DeprecationConflictException;
use Api\App\Message;
use Core\App\Message;
use Dot\DependencyInjection\Attribute\Inject;
use Laminas\Stratigility\MiddlewarePipe;
use Mezzio\Middleware\LazyLoadingMiddleware;
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Service/ErrorReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Api\App\Exception\ForbiddenException;
use Api\App\Exception\UnauthorizedException;
use Api\App\Message;
use Core\App\Message;
use Dot\DependencyInjection\Attribute\Inject;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
Expand Down
14 changes: 5 additions & 9 deletions src/Core/src/Admin/src/Repository/AdminRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Core\Admin\Repository;

use Api\Admin\Collection\AdminCollection;
use Api\App\Helper\PaginationHelper;
use Core\Admin\Entity\Admin;
use Core\App\Helper\PaginationHelper;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Dot\DependencyInjection\Attribute\Entity;

/**
Expand All @@ -30,21 +30,17 @@ public function saveAdmin(Admin $admin): Admin
return $admin;
}

public function getAdmins(array $filters = []): AdminCollection
public function getAdmins(array $filters = []): QueryBuilder
{
$page = PaginationHelper::getOffsetAndLimit($filters);

$query = $this
return $this
->getEntityManager()
->createQueryBuilder()
->select(['admin'])
->from(Admin::class, 'admin')
->orderBy($filters['order'] ?? 'admin.created', $filters['dir'] ?? 'desc')
->setFirstResult($page['offset'])
->setMaxResults($page['limit'])
->getQuery()
->useQueryCache(true);

return new AdminCollection($query, false);
->setMaxResults($page['limit']);
}
}
14 changes: 5 additions & 9 deletions src/Core/src/Admin/src/Repository/AdminRoleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Core\Admin\Repository;

use Api\Admin\Collection\AdminRoleCollection;
use Api\App\Helper\PaginationHelper;
use Core\Admin\Entity\AdminRole;
use Core\App\Helper\PaginationHelper;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Dot\DependencyInjection\Attribute\Entity;

/**
Expand All @@ -16,21 +16,17 @@
#[Entity(name: AdminRole::class)]
class AdminRoleRepository extends EntityRepository
{
public function getAdminRoles(array $filters = []): AdminRoleCollection
public function getAdminRoles(array $filters = []): QueryBuilder
{
$page = PaginationHelper::getOffsetAndLimit($filters);

$query = $this
return $this
->getEntityManager()
->createQueryBuilder()
->select(['role'])
->from(AdminRole::class, 'role')
->orderBy($filters['order'] ?? 'role.created', $filters['dir'] ?? 'desc')
->setFirstResult($page['offset'])
->setMaxResults($page['limit'])
->getQuery()
->useQueryCache(true);

return new AdminRoleCollection($query, false);
->setMaxResults($page['limit']);
}
}
33 changes: 33 additions & 0 deletions src/Core/src/App/src/Helper/PaginationHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Core\App\Helper;

use function array_key_exists;

class PaginationHelper
{
public const LIMIT = 10;

public static function getOffsetAndLimit(array $filters = []): array
{
$page = (int) ($filters['page'] ?? 1);

if (array_key_exists('all', $filters)) {
$offset = 0;
$limit = 1_000;
} elseif ($page > 0) {
$offset = ($page - 1) * self::LIMIT;
$limit = self::LIMIT;
} else {
$offset = 0;
$limit = self::LIMIT;
}

return [
'offset' => $offset,
'limit' => $limit,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Api\App;
namespace Core\App;

class Message
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/User/src/Entity/UserAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Core\User\Entity;

use Api\User\EventListener\UserAvatarEventListener;
use Core\App\Entity\AbstractEntity;
use Core\App\Entity\TimestampsTrait;
use Core\User\EventListener\UserAvatarEventListener;
use Core\User\Repository\UserAvatarRepository;
use Doctrine\ORM\Mapping as ORM;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Api\User\EventListener;
namespace Core\User\EventListener;

use Core\User\Entity\UserAvatar;
use Dot\DependencyInjection\Attribute\Inject;
Expand Down
12 changes: 5 additions & 7 deletions src/Core/src/User/src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Core\User\Repository;

use Api\App\Helper\PaginationHelper;
use Api\App\Message;
use Api\User\Collection\UserCollection;
use Core\Admin\Entity\Admin;
use Core\App\Entity\OAuthClient;
use Core\App\Helper\PaginationHelper;
use Core\App\Message;
use Core\User\Entity\User;
use Core\User\Enum\UserStatusEnum;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Dot\DependencyInjection\Attribute\Entity;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
Expand All @@ -32,7 +32,7 @@ public function deleteUser(User $user): void
$this->getEntityManager()->flush();
}

public function getUsers(array $filters = []): UserCollection
public function getUsers(array $filters = []): QueryBuilder
{
$page = PaginationHelper::getOffsetAndLimit($filters);

Expand Down Expand Up @@ -78,9 +78,7 @@ public function getUsers(array $filters = []): UserCollection
$qb->andWhere('roles.name = :role')->setParameter('role', $filters['role']);
}

$qb->getQuery()->useQueryCache(true);

return new UserCollection($qb, false);
return $qb;
}

public function saveUser(User $user): User
Expand Down
Loading

0 comments on commit 982d341

Please sign in to comment.