Total Complexity | 56 |
Total Lines | 422 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 0 |
Complex classes like Repository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Repository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class Repository implements RepositoryInterface |
||
69 | { |
||
70 | /** |
||
71 | * The list of relation to load with the query |
||
72 | * @var array<int, string>|array<string, Closure> |
||
73 | */ |
||
74 | protected array $with = []; |
||
75 | |||
76 | /** |
||
77 | * Whether need load relation data immediately |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected bool $immediate = false; |
||
81 | |||
82 | /** |
||
83 | * The order by column(s) |
||
84 | * @var string|Closure|Expression|string[]|Expression[]|Closure[] |
||
85 | */ |
||
86 | protected string|Closure|Expression|array $orderColumns = ''; |
||
87 | |||
88 | /** |
||
89 | * The order direction |
||
90 | * @var string |
||
91 | */ |
||
92 | protected string $orderDir = 'ASC'; |
||
93 | |||
94 | /** |
||
95 | * The offset to use |
||
96 | * @var int |
||
97 | */ |
||
98 | protected int $offset = -1; |
||
99 | |||
100 | /** |
||
101 | * The limit to use |
||
102 | * @var int |
||
103 | */ |
||
104 | protected int $limit = 0; |
||
105 | |||
106 | /** |
||
107 | * The filters list |
||
108 | * @var array<string, mixed> |
||
109 | */ |
||
110 | protected array $filters = []; |
||
111 | |||
112 | /** |
||
113 | * Create new instance |
||
114 | * @param EntityManager<TEntity> $manager |
||
115 | * @param class-string<TEntity> $entityClass |
||
|
|||
116 | */ |
||
117 | public function __construct( |
||
118 | protected EntityManager $manager, |
||
119 | protected string $entityClass |
||
120 | ) { |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritedoc} |
||
125 | */ |
||
126 | public function query(string|array $with = [], bool $immediate = false): EntityQuery |
||
127 | { |
||
128 | if (is_string($with)) { |
||
129 | $with = [$with]; |
||
130 | } |
||
131 | |||
132 | if (empty($with) && count($this->with) > 0) { |
||
133 | $with = $this->with; |
||
134 | |||
135 | $this->with = []; |
||
136 | } |
||
137 | $this->immediate = false; |
||
138 | |||
139 | $query = $this->manager->query($this->entityClass); |
||
140 | $query->with($with, $immediate); |
||
141 | |||
142 | if (!empty($this->orderColumns)) { |
||
143 | $query->orderBy($this->orderColumns, $this->orderDir); |
||
144 | |||
145 | $this->orderColumns = ''; |
||
146 | $this->orderDir = 'ASC'; |
||
147 | } |
||
148 | |||
149 | if ($this->offset >= 0 && $this->limit >= 0) { |
||
150 | $query->offset($this->offset) |
||
151 | ->limit($this->limit); |
||
152 | |||
153 | $this->offset = -1; |
||
154 | $this->limit = 0; |
||
155 | } |
||
156 | |||
157 | $this->setFilters($query); |
||
158 | |||
159 | return $query; |
||
160 | } |
||
161 | |||
162 | |||
163 | /** |
||
164 | * {@inheritedoc} |
||
165 | * @return self<TEntity> |
||
166 | */ |
||
167 | public function with(string|array $with, bool $immediate = false): self |
||
168 | { |
||
169 | if (!is_array($with)) { |
||
170 | $with = [$with]; |
||
171 | } |
||
172 | $this->with = $with; |
||
173 | $this->immediate = $immediate; |
||
174 | |||
175 | return $this; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * {@inheritedoc} |
||
180 | * @return self<TEntity> |
||
181 | */ |
||
182 | public function orderBy( |
||
183 | string|Closure|Expression|array $columns, |
||
184 | string $order = 'ASC' |
||
185 | ): self { |
||
186 | $this->orderColumns = $columns; |
||
187 | $this->orderDir = $order; |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * {@inheritedoc} |
||
194 | * @return self<TEntity> |
||
195 | */ |
||
196 | public function limit(int $offset, int $limit): self |
||
197 | { |
||
198 | $this->offset = $offset; |
||
199 | $this->limit = $limit; |
||
200 | |||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * {@inheritedoc} |
||
206 | * @return self<TEntity> |
||
207 | */ |
||
208 | public function filters(string|array $filters = []): self |
||
209 | { |
||
210 | if (is_string($filters)) { |
||
211 | $filters = [$filters => true]; |
||
212 | } |
||
213 | |||
214 | $this->filters = $filters; |
||
215 | |||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * {@inheritedoc} |
||
221 | */ |
||
222 | public function all(array $columns = []): array |
||
223 | { |
||
224 | return $this->query()->all($columns); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * {@inheritedoc} |
||
229 | */ |
||
230 | public function create(array $columns = []): Entity |
||
231 | { |
||
232 | $mapper = $this->manager->getEntityMapper($this->entityClass); |
||
233 | |||
234 | return new $this->entityClass( |
||
235 | $this->manager, |
||
236 | $mapper, |
||
237 | $columns, |
||
238 | [], |
||
239 | false, |
||
240 | true |
||
241 | ); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * {@inheritedoc} |
||
246 | */ |
||
247 | public function find(array|string|int|float $id): ?Entity |
||
248 | { |
||
249 | return $this->query()->find($id); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * {@inheritedoc} |
||
254 | */ |
||
255 | public function findBy(array $conditions): ?Entity |
||
256 | { |
||
257 | $query = $this->query(); |
||
258 | foreach ($conditions as $name => $value) { |
||
259 | $query->where($name)->is($value); |
||
260 | } |
||
261 | return $query->get(); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * {@inheritedoc} |
||
266 | */ |
||
267 | public function findAll(mixed ...$ids): array |
||
268 | { |
||
269 | return $this->query()->findAll(...$ids); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * {@inheritedoc} |
||
274 | */ |
||
275 | public function findAllBy(array $conditions): array |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * {@inheritedoc} |
||
286 | */ |
||
287 | public function save(Entity $entity): array|string|int|float|bool|null |
||
288 | { |
||
289 | $data = Proxy::instance()->getEntityDataMapper($entity); |
||
290 | |||
291 | if ($data->isNew()) { |
||
292 | return (bool) $this->insert($entity); |
||
293 | } |
||
294 | |||
295 | return $this->update($entity); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * {@inheritedoc} |
||
300 | */ |
||
301 | public function insert(Entity $entity): array|string|int|float|false|null |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * {@inheritedoc} |
||
364 | */ |
||
365 | public function update(Entity $entity): bool |
||
366 | { |
||
367 | $data = Proxy::instance()->getEntityDataMapper($entity); |
||
368 | $mapper = $data->getEntityMapper(); |
||
369 | $eventsHandlers = $mapper->getEventHandlers(); |
||
370 | |||
371 | if ($data->isDeleted()) { |
||
372 | throw new EntityStateException('The record was deleted'); |
||
373 | } |
||
374 | |||
375 | if ($data->isNew()) { |
||
376 | throw new EntityStateException('Can\'t update an unsaved entity'); |
||
377 | } |
||
378 | |||
379 | if (!$data->wasModified()) { |
||
380 | return true; |
||
381 | } |
||
382 | |||
383 | $modified = $data->getModifiedColumns(); |
||
384 | if (!empty($modified)) { |
||
385 | $connection = $this->manager->getConnection(); |
||
386 | $result = $connection->transaction(function (Connection $connection) use ($data, $mapper, $modified) { |
||
387 | $columns = array_intersect_key($data->getRawColumns(), array_flip($modified)); |
||
388 | |||
389 | $updatedAt = null; |
||
390 | |||
391 | if ($mapper->hasTimestamp()) { |
||
392 | list(, $updatedAtCol) = $mapper->getTimestampColumns(); |
||
393 | $columns[$updatedAtCol] = $updatedAt = date($this->manager->getDateFormat()); |
||
394 | } |
||
395 | $data->markAsUpdated($updatedAt); |
||
396 | |||
397 | $update = new Update($connection, $mapper->getTable()); |
||
398 | |||
399 | $primaryKeys = $mapper->getPrimaryKey()->getValue($data->getRawColumns(), true); |
||
400 | if (is_array($primaryKeys)) { |
||
401 | foreach ($primaryKeys as $pkColumn => $pkValue) { |
||
402 | $update->where($pkColumn)->is($pkValue); |
||
403 | } |
||
404 | } |
||
405 | |||
406 | return $update->set($columns) >= 0; |
||
407 | }); |
||
408 | |||
409 | if ($result === false) { |
||
410 | return false; |
||
411 | } |
||
412 | |||
413 | if (isset($eventsHandlers['update'])) { |
||
414 | foreach ($eventsHandlers['update'] as $callback) { |
||
415 | $callback($entity, $data); |
||
416 | } |
||
417 | } |
||
418 | |||
419 | return true; |
||
420 | } |
||
421 | |||
422 | $connection = $this->manager->getConnection(); |
||
423 | return $connection->transaction(function (Connection $connection) use ($data) { |
||
424 | $data->executePendingLinkage(); |
||
425 | |||
426 | return true; |
||
427 | }); |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * {@inheritedoc} |
||
432 | */ |
||
433 | public function delete(Entity $entity, bool $force = false): bool |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Set the filters |
||
478 | * @param EntityQuery<TEntity> $query |
||
479 | * @return $this |
||
480 | */ |
||
481 | protected function setFilters(EntityQuery $query): self |
||
490 | } |
||
491 | } |
||
492 |