-
Notifications
You must be signed in to change notification settings - Fork 50
/
DoctrineRepository.php
119 lines (94 loc) · 2.88 KB
/
DoctrineRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace App\Shared\Infrastructure\Doctrine;
use App\Shared\Domain\Repository\PaginatorInterface;
use App\Shared\Domain\Repository\RepositoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Webmozart\Assert\Assert;
/**
* @template T of object
*
* @implements RepositoryInterface<T>
*/
abstract class DoctrineRepository implements RepositoryInterface
{
private ?int $page = null;
private ?int $itemsPerPage = null;
private QueryBuilder $queryBuilder;
public function __construct(
protected EntityManagerInterface $em,
string $entityClass,
string $alias,
) {
$this->queryBuilder = $this->em->createQueryBuilder()
->select($alias)
->from($entityClass, $alias);
}
public function getIterator(): \Iterator
{
if (null !== $paginator = $this->paginator()) {
yield from $paginator;
return;
}
yield from $this->queryBuilder->getQuery()->getResult();
}
public function count(): int
{
if (null !== $paginator = $this->paginator()) {
return count($paginator);
}
return (int) (clone $this->queryBuilder)
->select('count(1)')
->getQuery()
->getSingleScalarResult();
}
public function paginator(): ?PaginatorInterface
{
if (null === $this->page || null === $this->itemsPerPage) {
return null;
}
$firstResult = ($this->page - 1) * $this->itemsPerPage;
$maxResults = $this->itemsPerPage;
$repository = $this->filter(static function (QueryBuilder $qb) use ($firstResult, $maxResults) {
$qb->setFirstResult($firstResult)->setMaxResults($maxResults);
});
/** @var Paginator<T> $paginator */
$paginator = new Paginator($repository->queryBuilder->getQuery());
return new DoctrinePaginator($paginator);
}
public function withoutPagination(): static
{
$cloned = clone $this;
$cloned->page = null;
$cloned->itemsPerPage = null;
return $cloned;
}
public function withPagination(int $page, int $itemsPerPage): static
{
Assert::positiveInteger($page);
Assert::positiveInteger($itemsPerPage);
$cloned = clone $this;
$cloned->page = $page;
$cloned->itemsPerPage = $itemsPerPage;
return $cloned;
}
/**
* @return static<T>
*/
protected function filter(callable $filter): static
{
$cloned = clone $this;
$filter($cloned->queryBuilder);
return $cloned;
}
protected function query(): QueryBuilder
{
return clone $this->queryBuilder;
}
protected function __clone()
{
$this->queryBuilder = clone $this->queryBuilder;
}
}