Total Complexity | 57 |
Total Lines | 415 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractRepository 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 AbstractRepository, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
17 | abstract class AbstractRepository extends BaseAbstractRepository |
||
18 | { |
||
19 | /** |
||
20 | * @param QueryBuilderInterface $queryBuilder |
||
21 | * @param bool $toString |
||
22 | * |
||
23 | * @return string|EntityInterface |
||
24 | * @throws RepositoryException|\Exception |
||
25 | */ |
||
26 | protected function insert(QueryBuilderInterface $queryBuilder, bool $toString = false) |
||
27 | { |
||
28 | if (!($entity = $queryBuilder->getEntity())) { |
||
29 | throw new RepositoryException( |
||
30 | 'Cannot perform insert on query without entity! Please create query builder with entity.' |
||
31 | ); |
||
32 | } |
||
33 | |||
34 | $this->addEntityInsertQuery($queryBuilder, $entity); |
||
35 | |||
36 | if (true === $toString) { |
||
37 | return $queryBuilder->toString(); |
||
38 | } |
||
39 | |||
40 | |||
41 | $this->beginTransaction($connection = $this->getConnection()); |
||
42 | |||
43 | try { |
||
44 | $this->prepareAndExecute($queryBuilder, $queryBuilder->bindValueData(), $connection); |
||
45 | } catch (\Exception $e) { |
||
46 | $this->rollBack($connection); |
||
47 | |||
48 | throw $e; |
||
49 | } |
||
50 | |||
51 | if (!empty($primaryKeysAutoIncrement = $entity->primaryKeysAutoIncrement())) { |
||
52 | $entityData = &$entity->data(); |
||
53 | |||
54 | foreach ($primaryKeysAutoIncrement as $primaryKeyAutoIncrement) { |
||
55 | $entityData[$primaryKeyAutoIncrement] = (int) $connection->lastInsertId($entity::TABLE_NAME); |
||
|
|||
56 | } |
||
57 | } |
||
58 | |||
59 | $this->commit($connection); |
||
60 | |||
61 | return $entity; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param QueryBuilderInterface $queryBuilder |
||
66 | * @param bool $toString |
||
67 | * |
||
68 | * @return string|EntityInterface |
||
69 | */ |
||
70 | protected function insertIgnore(QueryBuilderInterface $queryBuilder, bool $toString = false) |
||
71 | { |
||
72 | return $this->insert($queryBuilder, $toString); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param QueryBuilderInterface $queryBuilder |
||
77 | * @param bool $toString |
||
78 | * |
||
79 | * @return string|EntityInterface |
||
80 | */ |
||
81 | protected function replace(QueryBuilderInterface $queryBuilder, bool $toString = false) |
||
82 | { |
||
83 | return $this->insert($queryBuilder, $toString); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param QueryBuilderInterface $queryBuilder |
||
88 | * @param bool $toString |
||
89 | * |
||
90 | * @return null|string |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | protected function findOne(QueryBuilderInterface $queryBuilder, bool $toString = false) |
||
94 | { |
||
95 | if (true === $toString) { |
||
96 | return $queryBuilder->toString(); |
||
97 | } |
||
98 | |||
99 | try { |
||
100 | $statement = $this->prepareAndExecute($queryBuilder, $queryBuilder->bindData()); |
||
101 | $statement->setFetchMode( |
||
102 | \PDO::FETCH_CLASS, |
||
103 | ($entity = $queryBuilder->getEntity()) ? \get_class($entity) : $this->getEntityClass(), |
||
104 | [ |
||
105 | false, |
||
106 | ] |
||
107 | ); |
||
108 | } catch (\Exception $e) { |
||
109 | $this->rollback(); |
||
110 | |||
111 | throw $e; |
||
112 | } |
||
113 | |||
114 | return $statement->fetch() ?: null; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param QueryBuilderInterface $queryBuilder |
||
119 | * @param bool $toString |
||
120 | * |
||
121 | * @return array|string |
||
122 | * @throws \Exception |
||
123 | */ |
||
124 | protected function find(QueryBuilderInterface $queryBuilder, bool $toString = false) |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param QueryBuilderInterface $queryBuilder |
||
150 | * @param bool $toString |
||
151 | * |
||
152 | * @return null|bool|EntityInterface|string |
||
153 | * @throws \Exception |
||
154 | */ |
||
155 | protected function update(QueryBuilderInterface $queryBuilder, bool $toString = false) |
||
156 | { |
||
157 | if (($entity = $queryBuilder->getEntity()) && !$this->addEntityUpdateQuery($queryBuilder, $entity, $toString)) { |
||
158 | return $entity; |
||
159 | } |
||
160 | |||
161 | if (true === $toString) { |
||
162 | return $queryBuilder->toString(); |
||
163 | } |
||
164 | |||
165 | $this->beginTransaction($connection = $this->getConnection()); |
||
166 | |||
167 | try { |
||
168 | $connection->setSqlSafeUpdates(); |
||
169 | |||
170 | $this->prepareAndExecute($queryBuilder, $queryBuilder->bindData(), $connection); |
||
171 | |||
172 | $connection->unsetSqlSafeUpdates(); |
||
173 | } catch (\Exception $e) { |
||
174 | $this->rollBack($connection); |
||
175 | $connection->unsetSqlSafeUpdates(); |
||
176 | |||
177 | throw $e; |
||
178 | } |
||
179 | |||
180 | return $this->commit($connection) ? ($entity ?: true) : false; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param QueryBuilderInterface $queryBuilder |
||
185 | * @param bool $toString |
||
186 | * |
||
187 | * @return null|string|bool|EntityInterface |
||
188 | */ |
||
189 | protected function updateIgnore(QueryBuilderInterface $queryBuilder, bool $toString = false) |
||
190 | { |
||
191 | return $this->update($queryBuilder, $toString); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param QueryBuilderInterface $queryBuilder |
||
196 | * @param bool $toString |
||
197 | * |
||
198 | * @return bool|string |
||
199 | * @throws \Exception |
||
200 | */ |
||
201 | protected function delete(QueryBuilderInterface $queryBuilder, bool $toString = false) |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param QueryBuilderInterface $queryBuilder |
||
231 | * @param bool $toString |
||
232 | * |
||
233 | * @return int |
||
234 | * @throws RepositoryException|\Exception |
||
235 | */ |
||
236 | protected function count(BaseQueryBuilderInterface $queryBuilder, bool $toString = false) |
||
237 | { |
||
238 | if (CommandEnum::SELECT !== $queryBuilder->commandData()) { |
||
239 | throw new RepositoryException(\sprintf( |
||
240 | 'Command "%s" is not a valid command for count query! Use "%s" command to execute count query.', |
||
241 | $queryBuilder->commandData(), |
||
242 | CommandEnum::SELECT |
||
243 | )); |
||
244 | } |
||
245 | |||
246 | $queryBuilderCount = $this |
||
247 | ->createQueryBuilder() |
||
248 | ->command($queryBuilder->commandData()) |
||
249 | ->column('COUNT(*)', true) |
||
250 | ->table(\sprintf('(%s) AS total_count', $queryBuilder->buildQuery()), true) |
||
251 | ->bind($queryBuilder->bindData()) |
||
252 | ; |
||
253 | |||
254 | if ($toString === true) { |
||
255 | return $queryBuilderCount->toString(); |
||
256 | } |
||
257 | |||
258 | $connection = $this->getConnection(); |
||
259 | |||
260 | try { |
||
261 | $statement = $this->prepareAndExecute($queryBuilderCount, $queryBuilderCount->bindData(), $connection); |
||
262 | } catch (\Exception $e) { |
||
263 | $this->rollBack($connection); |
||
264 | |||
265 | throw $e; |
||
266 | } |
||
267 | |||
268 | return $statement->fetchColumn(0); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @param EntityInterface|null $entity |
||
273 | * |
||
274 | * @return QueryBuilderInterface |
||
275 | */ |
||
276 | protected function createQueryBuilder(?EntityInterface $entity = null): BaseQueryBuilderInterface |
||
277 | { |
||
278 | return (new QueryBuilder($this->createRepositoryCallClosure(), $entity)) |
||
279 | ->column(\sprintf('%s.*', $this->getEntityClassConstant(WriterInterface::CLASS_CONSTANT_TABLE_NAME))) |
||
280 | ->table($this->getEntityClassConstant(WriterInterface::CLASS_CONSTANT_TABLE_NAME)) |
||
281 | ; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param QueryBuilderInterface $queryBuilder |
||
286 | * @param EntityInterface $entity |
||
287 | * |
||
288 | * @return $this |
||
289 | */ |
||
290 | protected function addEntityInsertQuery(QueryBuilderInterface $queryBuilder, EntityInterface $entity) |
||
291 | { |
||
292 | $entityColumns = $entity->columns(); |
||
293 | $entityData = &$entity->data(); |
||
294 | |||
295 | foreach ($entityColumns as $column) { |
||
296 | if (\array_key_exists($column, $entityData)) { |
||
297 | $queryBuilder->value($column, $entityData[$column]); |
||
298 | } |
||
299 | } |
||
300 | |||
301 | return $this; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param QueryBuilderInterface $queryBuilder |
||
306 | * @param EntityInterface $entity |
||
307 | * @param bool $toString |
||
308 | * |
||
309 | * @return bool |
||
310 | */ |
||
311 | protected function addEntityUpdateQuery( |
||
312 | QueryBuilderInterface $queryBuilder, |
||
313 | EntityInterface $entity, |
||
314 | bool $toString |
||
315 | ) { |
||
316 | $performUpdate = false; |
||
317 | $entityData = &$entity->data(); |
||
318 | $entityDataOriginal = &$entity->dataOriginal(); |
||
319 | |||
320 | if (false === $entity->isNew() || true === $entity->isSaved()) { |
||
321 | foreach ($entity->primaryKeys() as $primaryKey) { |
||
322 | if (isset($entityDataOriginal[$primaryKey])) { |
||
323 | $queryBuilder->where( |
||
324 | \sprintf('%s.%s = :%s_WhereUpdate', $entity::TABLE_NAME, $primaryKey, $primaryKey), |
||
325 | [ |
||
326 | \sprintf('%s_WhereUpdate', $primaryKey) => $entityDataOriginal[$primaryKey], |
||
327 | ] |
||
328 | ); |
||
329 | } |
||
330 | } |
||
331 | } else { |
||
332 | return $performUpdate; |
||
333 | } |
||
334 | |||
335 | foreach ($entity->columns() as $column) { |
||
336 | /** Updating only what is needed to update, for faster queries, skipping equal values */ |
||
337 | if (!\array_key_exists($column, $entityDataOriginal) |
||
338 | || (isset($entityDataOriginal[$column]) && $entityDataOriginal[$column] == $entityData[$column]) |
||
339 | ) { |
||
340 | continue; |
||
341 | } elseif (false === $performUpdate) { |
||
342 | $performUpdate = true; |
||
343 | } |
||
344 | |||
345 | $queryBuilder->set(\sprintf('%s.%s', $entity::TABLE_NAME, $column), $entityData[$column]); |
||
346 | |||
347 | /** |
||
348 | * If we want to get query as astring, we don't perform actions on original data as update won't be |
||
349 | * performed! |
||
350 | */ |
||
351 | if (false === $toString) { |
||
352 | $entityDataOriginal[$column] = $entityData[$column]; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return $performUpdate; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param QueryBuilderInterface $queryBuilder |
||
361 | * @param EntityInterface $entity |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | protected function addEntityDeleteQuery(QueryBuilderInterface $queryBuilder, EntityInterface $entity) |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @param QueryBuilderInterface $queryBuilder |
||
408 | * @param int $pageSize |
||
409 | * @param int $currentPage |
||
410 | * |
||
411 | * @return $this |
||
412 | */ |
||
413 | protected function addPaginateQuery( |
||
414 | BaseQueryBuilderInterface $queryBuilder, |
||
415 | int $pageSize, |
||
416 | int $currentPage |
||
417 | ): BaseAbstractRepository { |
||
418 | $queryBuilder->limitWithOffset($pageSize, $pageSize * $currentPage - $pageSize); |
||
419 | |||
420 | return $this; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * @param QueryBuilderInterface $queryBuilder |
||
425 | * @param bool $toString |
||
426 | * |
||
427 | * @return EntityInterface[] |
||
428 | */ |
||
429 | protected function getPaginateResult(BaseQueryBuilderInterface $queryBuilder, bool $toString = false): array |
||
432 | } |
||
433 | } |
||
434 |