Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| CreatePaginatedResultRuntime | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
7 | |
100.00% |
1 / 1 |
| getIterator | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| toPaginatedResult | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Datalayers\Concerns; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextId; |
| 5 | use Apie\Core\Datalayers\Lists\PaginatedResult; |
| 6 | use Apie\Core\Datalayers\Search\LazyLoadedListFilterer; |
| 7 | use Apie\Core\Datalayers\Search\QuerySearch; |
| 8 | use Apie\Core\Datalayers\ValueObjects\LazyLoadedListIdentifier; |
| 9 | use Apie\Core\Entities\EntityInterface; |
| 10 | use Apie\Core\Lists\ItemList; |
| 11 | use Iterator; |
| 12 | use ReflectionClass; |
| 13 | |
| 14 | /** |
| 15 | * @property LazyLoadedListFilterer $filterer |
| 16 | * @property BoundedContextId $boundedContextId |
| 17 | * @property ReflectionClass<EntityInterface> $class |
| 18 | */ |
| 19 | trait CreatePaginatedResultRuntime |
| 20 | { |
| 21 | abstract public function getIterator(): Iterator; |
| 22 | |
| 23 | public function toPaginatedResult(QuerySearch $search): PaginatedResult |
| 24 | { |
| 25 | $pageIndex = $search->getPageIndex(); |
| 26 | $itemsPerPage = $search->getItemsPerPage(); |
| 27 | $offset = 1+ $pageIndex * $itemsPerPage; |
| 28 | $endOffset = $offset + $itemsPerPage; |
| 29 | $filteredList = []; |
| 30 | $found = 0; |
| 31 | $totalCount = 0; |
| 32 | foreach ($this->getIterator() as $entity) { |
| 33 | assert($entity instanceof EntityInterface); |
| 34 | if ($this->filterer->appliesPermissions($entity, $search)) { |
| 35 | $added = $found < $endOffset && $this->filterer->appliesFiltering($entity, $search); |
| 36 | $totalCount++; |
| 37 | if ($added) { |
| 38 | $found++; |
| 39 | if ($found >= $offset && $found < $endOffset) { |
| 40 | $filteredList[] = $entity; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return new PaginatedResult( |
| 46 | LazyLoadedListIdentifier::createFrom($this->boundedContextId, $this->class), |
| 47 | $totalCount, |
| 48 | $found, |
| 49 | new ItemList($filteredList), |
| 50 | $pageIndex, |
| 51 | $itemsPerPage, |
| 52 | $search |
| 53 | ); |
| 54 | } |
| 55 | } |