Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
75 / 75 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| EntityQuery | |
100.00% |
75 / 75 |
|
100.00% |
7 / 7 |
23 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
12 | |||
| getWithoutPagination | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| generateJoins | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| generateWhere | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| generateOrderBy | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
4 | |||
| generateOffset | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\DoctrineEntityDatalayer\Query; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextId; |
| 5 | use Apie\Core\Datalayers\Search\QuerySearch; |
| 6 | use Apie\Core\Entities\EntityInterface; |
| 7 | use Apie\Core\IdentifierUtils; |
| 8 | use Apie\DoctrineEntityDatalayer\Enums\SortingOrder; |
| 9 | use Doctrine\ORM\EntityManagerInterface; |
| 10 | use ReflectionClass; |
| 11 | use Stringable; |
| 12 | |
| 13 | final class EntityQuery implements Stringable |
| 14 | { |
| 15 | /** @var array<int, EntityQueryFilterInterface> */ |
| 16 | private array $filters = []; |
| 17 | /** @var array<int, EntityQueryFilterInterface&AddsJoinFilterInterface> */ |
| 18 | private array $joinFilters = []; |
| 19 | /** @var array<int, OrderByFilterInterface> */ |
| 20 | private array $orderByFilters = []; |
| 21 | |
| 22 | /** |
| 23 | * @param ReflectionClass<EntityInterface> $entityClass |
| 24 | */ |
| 25 | public function __construct( |
| 26 | private readonly EntityManagerInterface $entityManager, |
| 27 | private readonly ReflectionClass $entityClass, |
| 28 | private readonly BoundedContextId $boundedContextId, |
| 29 | private readonly QuerySearch $querySearch, |
| 30 | EntityQueryFilterInterface... $filters |
| 31 | ) { |
| 32 | $searches = $querySearch->getSearches(); |
| 33 | $orderBy = $this->querySearch->getOrderBy(); |
| 34 | foreach ($filters as $filter) { |
| 35 | $name = is_callable([$filter, 'getFilterName']) ? $filter->getFilterName() : ''; |
| 36 | if ($filter instanceof TextSearchFilterInterface && $querySearch->getTextSearch()) { |
| 37 | } elseif ($filter instanceof FieldSearchFilterInterface && !empty($searches[$name])) { |
| 38 | } elseif ($filter instanceof OrderByFilterInterface && !empty($orderBy[$name])) { |
| 39 | } elseif ($filter instanceof RequiresPermissionFilter) { |
| 40 | } else { |
| 41 | continue; |
| 42 | } |
| 43 | $this->filters[] = $filter; |
| 44 | if ($filter instanceof AddsJoinFilterInterface) { |
| 45 | $this->joinFilters[] = $filter; |
| 46 | } |
| 47 | if ($filter instanceof OrderByFilterInterface) { |
| 48 | $this->orderByFilters[] = $filter; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public function getWithoutPagination(): string |
| 54 | { |
| 55 | return sprintf( |
| 56 | "SELECT DISTINCT entity.* |
| 57 | FROM apie_resource__%s_%s entity%s |
| 58 | %s |
| 59 | GROUP BY entity.id |
| 60 | ORDER BY %s", |
| 61 | $this->boundedContextId, |
| 62 | IdentifierUtils::classNameToUnderscore($this->entityClass), |
| 63 | $this->generateJoins(), |
| 64 | $this->generateWhere(), |
| 65 | $this->generateOrderBy(), |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | public function __toString(): string |
| 70 | { |
| 71 | return $this->getWithoutPagination() . PHP_EOL . $this->generateOffset(); |
| 72 | } |
| 73 | private function generateJoins(): string |
| 74 | { |
| 75 | if (empty($this->joinFilters)) { |
| 76 | return ''; |
| 77 | } |
| 78 | $connection = $this->entityManager->getConnection(); |
| 79 | $joinSql = implode( |
| 80 | PHP_EOL, |
| 81 | array_filter( |
| 82 | array_map( |
| 83 | function (EntityQueryFilterInterface&AddsJoinFilterInterface $filter) use ($connection) { |
| 84 | return $filter->createJoinQuery($this->querySearch, $connection); |
| 85 | }, |
| 86 | $this->joinFilters |
| 87 | ) |
| 88 | ) |
| 89 | ); |
| 90 | return PHP_EOL . $joinSql; |
| 91 | } |
| 92 | |
| 93 | private function generateWhere(): string |
| 94 | { |
| 95 | if (empty($this->filters)) { |
| 96 | return ''; |
| 97 | } |
| 98 | $connection = $this->entityManager->getConnection(); |
| 99 | $whereSql = implode( |
| 100 | ')' . PHP_EOL . 'AND (', |
| 101 | array_map( |
| 102 | function (EntityQueryFilterInterface $filter) use ($connection) { |
| 103 | return $filter->getWhereCondition($this->querySearch, $connection); |
| 104 | }, |
| 105 | $this->filters |
| 106 | ) |
| 107 | ); |
| 108 | return 'WHERE (' . $whereSql . ')'; |
| 109 | } |
| 110 | |
| 111 | private function generateOrderBy(): string |
| 112 | { |
| 113 | $orderBy = $this->querySearch->getOrderBy()->toArray(); |
| 114 | $query = implode( |
| 115 | ', ', |
| 116 | array_filter( |
| 117 | array_map( |
| 118 | function (OrderByFilterInterface $filter) use ($orderBy) { |
| 119 | if (is_callable([$filter, 'getFilterName'])) { |
| 120 | $filterName = $filter->getFilterName(); |
| 121 | if (isset($orderBy[$filterName])) { |
| 122 | return $filter->getOrderByCode(SortingOrder::from($orderBy[$filterName])); |
| 123 | } |
| 124 | } |
| 125 | return $filter->getOrderByCode(SortingOrder::DESCENDING); |
| 126 | }, |
| 127 | $this->orderByFilters |
| 128 | ) |
| 129 | ) |
| 130 | ); |
| 131 | |
| 132 | return empty($query) ? 'entity.created_at DESC' : $query; |
| 133 | } |
| 134 | |
| 135 | private function generateOffset(): string |
| 136 | { |
| 137 | $platform = $this->entityManager->getConnection()->getDatabasePlatform(); |
| 138 | |
| 139 | return $platform->modifyLimitQuery( |
| 140 | '', |
| 141 | $this->querySearch->getItemsPerPage(), |
| 142 | ($this->querySearch->getPageIndex() * $this->querySearch->getItemsPerPage()), |
| 143 | ); |
| 144 | } |
| 145 | } |