1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Paysera\Pagination\Entity\ODM; |
5
|
|
|
|
6
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder as QueryBuilder; |
7
|
|
|
use Paysera\Pagination\Entity\OrderingConfiguration; |
8
|
|
|
use Paysera\Pagination\Exception\InvalidOrderByException; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
View Code Duplication |
class ConfiguredQuery |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var QueryBuilder |
16
|
|
|
*/ |
17
|
|
|
private $queryBuilder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array|OrderingConfiguration[] associative, keys are strings for ordering fields |
21
|
|
|
*/ |
22
|
|
|
private $orderingConfigurations; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $totalCountNeeded; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int|null |
31
|
|
|
*/ |
32
|
|
|
private $maximumOffset; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var callable |
36
|
|
|
*/ |
37
|
|
|
private $itemTransformer; |
38
|
|
|
|
39
|
|
|
public function __construct(QueryBuilder $queryBuilder) |
40
|
|
|
{ |
41
|
|
|
$this->queryBuilder = $queryBuilder; |
42
|
|
|
$this->orderingConfigurations = []; |
43
|
|
|
$this->totalCountNeeded = false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function addOrderingConfiguration(string $orderBy, OrderingConfiguration $configuration): self |
47
|
|
|
{ |
48
|
|
|
if ($configuration->getAccessorPath() === null && $configuration->getAccessorClosure() === null) { |
49
|
|
|
throw new InvalidArgumentException( |
50
|
|
|
'Must set either accessorPath or accessorClosure for every OrderingConfiguration' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
$this->orderingConfigurations[$orderBy] = $configuration; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array|OrderingConfiguration[] $orderingConfigurations array of `orderBy => OrderingConfiguration` pairs |
59
|
|
|
* @return ConfiguredQuery |
60
|
|
|
*/ |
61
|
|
|
public function addOrderingConfigurations(array $orderingConfigurations): self |
62
|
|
|
{ |
63
|
|
|
foreach ($orderingConfigurations as $orderBy => $configuration) { |
64
|
|
|
$this->addOrderingConfiguration($orderBy, $configuration); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getOrderingConfigurationFor(string $orderBy): OrderingConfiguration |
71
|
|
|
{ |
72
|
|
|
if (!isset($this->orderingConfigurations[$orderBy])) { |
73
|
|
|
throw new InvalidOrderByException($orderBy); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->orderingConfigurations[$orderBy]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return QueryBuilder |
81
|
|
|
*/ |
82
|
|
|
public function getQueryBuilder(): QueryBuilder |
83
|
|
|
{ |
84
|
|
|
return $this->queryBuilder; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function isTotalCountNeeded(): bool |
91
|
|
|
{ |
92
|
|
|
return $this->totalCountNeeded; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param bool $totalCountNeeded |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function setTotalCountNeeded(bool $totalCountNeeded): self |
100
|
|
|
{ |
101
|
|
|
$this->totalCountNeeded = $totalCountNeeded; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int $maximumOffset |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function setMaximumOffset(int $maximumOffset): self |
110
|
|
|
{ |
111
|
|
|
$this->maximumOffset = $maximumOffset; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function hasMaximumOffset(): bool |
116
|
|
|
{ |
117
|
|
|
return $this->maximumOffset !== null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return int |
122
|
|
|
* @throws RuntimeException if maximum offset was not set. Check with hasMaximumOffset beforehand |
123
|
|
|
*/ |
124
|
|
|
public function getMaximumOffset(): int |
125
|
|
|
{ |
126
|
|
|
if ($this->maximumOffset === null) { |
127
|
|
|
throw new RuntimeException('Maximum offset was not set'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->maximumOffset; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return callable|null |
135
|
|
|
*/ |
136
|
|
|
public function getItemTransformer() |
137
|
|
|
{ |
138
|
|
|
return $this->itemTransformer; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function setItemTransformer(callable $itemTransformer): self |
142
|
|
|
{ |
143
|
|
|
$this->itemTransformer = $itemTransformer; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.