Complex classes like MySQLData 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MySQLData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class MySQLData extends AbstractData |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Holds the Doctrine DBAL instance. |
||
26 | * @var Connection |
||
27 | */ |
||
28 | protected $database; |
||
29 | |||
30 | /** |
||
31 | * Flag whether to use UUIDs as primary key. |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $useUUIDs; |
||
35 | |||
36 | /** |
||
37 | * Adds the soft deletion parameters if activated. |
||
38 | * |
||
39 | * @param EntityDefinition $definition |
||
40 | * the entity definition which might have soft deletion activated |
||
41 | * @param QueryBuilder $queryBuilder |
||
42 | * the query builder to add the deletion condition to |
||
43 | * @param string $fieldPrefix |
||
44 | * the prefix to add before the deleted_at field like an table alias |
||
45 | * @param string $method |
||
46 | * the method to use of the query builder, "where" or "andWhere" |
||
47 | */ |
||
48 | 35 | protected function addSoftDeletionToQuery(EntityDefinition $definition, QueryBuilder $queryBuilder, $fieldPrefix = '', $method = 'andWhere') |
|
49 | { |
||
50 | 35 | if (!$definition->isHardDeletion()) { |
|
51 | 35 | $queryBuilder->$method($fieldPrefix.'deleted_at IS NULL'); |
|
52 | } |
||
53 | 35 | } |
|
54 | |||
55 | /** |
||
56 | * Sets the values and parameters of the upcoming given query according |
||
57 | * to the entity. |
||
58 | * |
||
59 | * @param Entity $entity |
||
60 | * the entity with its fields and values |
||
61 | * @param QueryBuilder $queryBuilder |
||
62 | * the upcoming query |
||
63 | * @param string $setMethod |
||
64 | * what method to use on the QueryBuilder: 'setValue' or 'set' |
||
65 | */ |
||
66 | 34 | protected function setValuesAndParameters(Entity $entity, QueryBuilder $queryBuilder, $setMethod) |
|
67 | { |
||
68 | 34 | $formFields = $this->getFormFields(); |
|
69 | 34 | $count = count($formFields); |
|
70 | 34 | for ($i = 0; $i < $count; ++$i) { |
|
71 | 34 | $type = $this->definition->getType($formFields[$i]); |
|
72 | 34 | $value = $entity->get($formFields[$i]); |
|
73 | 34 | if ($type == 'boolean') { |
|
74 | 34 | $value = $value ? 1 : 0; |
|
75 | } |
||
76 | 34 | if ($type == 'reference' && is_array($value)) { |
|
77 | 4 | $value = $value['id']; |
|
78 | } |
||
79 | 34 | $queryBuilder->$setMethod('`'.$formFields[$i].'`', '?'); |
|
80 | 34 | $queryBuilder->setParameter($i, $value); |
|
81 | } |
||
82 | 34 | } |
|
83 | |||
84 | /** |
||
85 | * Checks whether the by id given entity still has children referencing it. |
||
86 | * |
||
87 | * @param integer $id |
||
88 | * the current entities id |
||
89 | * |
||
90 | * @return boolean |
||
91 | * true if the entity still has children |
||
92 | */ |
||
93 | 2 | protected function hasChildren($id) |
|
94 | { |
||
95 | 2 | foreach ($this->definition->getChildren() as $child) { |
|
96 | 2 | $queryBuilder = $this->database->createQueryBuilder(); |
|
97 | $queryBuilder |
||
98 | 2 | ->select('COUNT(id)') |
|
99 | 2 | ->from('`'.$child[0].'`', '`'.$child[0].'`') |
|
100 | 2 | ->where('`'.$child[1].'` = ?') |
|
101 | 2 | ->setParameter(0, $id) |
|
102 | ; |
||
103 | 2 | $this->addSoftDeletionToQuery($this->getDefinition()->getServiceProvider()->getData($child[2])->getDefinition(), $queryBuilder); |
|
104 | 2 | $queryResult = $queryBuilder->execute(); |
|
105 | 2 | $result = $queryResult->fetch(\PDO::FETCH_NUM); |
|
106 | 2 | if ($result[0] > 0) { |
|
107 | 2 | return true; |
|
108 | } |
||
109 | } |
||
110 | 2 | return false; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Deletes any many to many references pointing to the given entity. |
||
115 | * |
||
116 | * @param Entity $entity |
||
117 | * the referenced entity |
||
118 | */ |
||
119 | 4 | protected function deleteManyToManyReferences(Entity $entity) |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 4 | protected function doDelete(Entity $entity, $deleteCascade) |
|
175 | |||
176 | /** |
||
177 | * Gets all possible many-to-many ids existing for this definition. |
||
178 | * |
||
179 | * @param array $fields |
||
180 | * the many field names to fetch for |
||
181 | * @param $params |
||
182 | * the parameters the possible many field values to fetch for |
||
183 | * @return array |
||
184 | * an array of this many-to-many ids |
||
185 | */ |
||
186 | 35 | protected function getManyIds(array $fields, array $params) |
|
207 | |||
208 | /** |
||
209 | * Adds sorting parameters to the query. |
||
210 | * |
||
211 | * @param QueryBuilder $queryBuilder |
||
212 | * the query |
||
213 | * @param $filter |
||
214 | * the filter all resulting entities must fulfill, the keys as field names |
||
215 | * @param $filterOperators |
||
216 | * the operators of the filter like "=" defining the full condition of the field |
||
217 | */ |
||
218 | 35 | protected function addFilter(QueryBuilder $queryBuilder, array $filter, array $filterOperators) |
|
245 | |||
246 | /** |
||
247 | * Adds pagination parameters to the query. |
||
248 | * |
||
249 | * @param QueryBuilder $queryBuilder |
||
250 | * the query |
||
251 | * @param integer|null $skip |
||
252 | * the rows to skip |
||
253 | * @param integer|null $amount |
||
254 | * the maximum amount of rows |
||
255 | */ |
||
256 | 35 | protected function addPagination(QueryBuilder $queryBuilder, $skip, $amount) |
|
266 | |||
267 | /** |
||
268 | * Adds sorting parameters to the query. |
||
269 | * |
||
270 | * @param QueryBuilder $queryBuilder |
||
271 | * the query |
||
272 | * @param string|null $sortField |
||
273 | * the sort field |
||
274 | * @param boolean|null $sortAscending |
||
275 | * true if sort ascending, false if descending |
||
276 | */ |
||
277 | 35 | protected function addSort(QueryBuilder $queryBuilder, $sortField, $sortAscending) |
|
290 | |||
291 | /** |
||
292 | * Adds the id and name of referenced entities to the given entities. The |
||
293 | * reference field is before the raw id of the referenced entity and after |
||
294 | * the fetch, it's an array with the keys id and name. |
||
295 | * |
||
296 | * @param Entity[] &$entities |
||
297 | * the entities to fetch the references for |
||
298 | * @param string $field |
||
299 | * the reference field |
||
300 | */ |
||
301 | 21 | protected function fetchReferencesForField(array &$entities, $field) |
|
338 | |||
339 | /** |
||
340 | * Generates a new UUID. |
||
341 | * |
||
342 | * @return string|null |
||
343 | * the new UUID or null if this instance isn't configured to do so |
||
344 | */ |
||
345 | 34 | protected function generateUUID() |
|
355 | |||
356 | /** |
||
357 | * Enriches the given mapping of entity id to raw entity data with some many-to-many data. |
||
358 | * |
||
359 | * @param array $idToData |
||
360 | * a reference to the map entity id to raw entity data |
||
361 | * @param $manyField |
||
362 | * the many field to enrich data with |
||
363 | */ |
||
364 | 34 | protected function enrichWithManyField(&$idToData, $manyField) |
|
390 | |||
391 | /** |
||
392 | * Fetches to the rows belonging many-to-many entries and adds them to the rows. |
||
393 | * |
||
394 | * @param array $rows |
||
395 | * the rows to enrich |
||
396 | * @return array |
||
397 | * the enriched rows |
||
398 | */ |
||
399 | 35 | protected function enrichWithMany(array $rows) |
|
414 | |||
415 | /** |
||
416 | * First, deletes all to the given entity related many-to-many entries from the DB |
||
417 | * and then writes them again. |
||
418 | * |
||
419 | * @param Entity $entity |
||
420 | * the entity to save the many-to-many entries of |
||
421 | */ |
||
422 | 34 | protected function saveMany(Entity $entity) |
|
439 | |||
440 | /** |
||
441 | * Adds the id and name of referenced entities to the given entities. Each |
||
442 | * reference field is before the raw id of the referenced entity and after |
||
443 | * the fetch, it's an array with the keys id and name. |
||
444 | * |
||
445 | * @param Entity[] &$entities |
||
446 | * the entities to fetch the references for |
||
447 | * |
||
448 | * @return void |
||
449 | */ |
||
450 | 35 | protected function enrichWithReference(array &$entities) |
|
462 | |||
463 | /** |
||
464 | * {@inheritdoc} |
||
465 | */ |
||
466 | 34 | protected function doCreate(Entity $entity) |
|
500 | |||
501 | /** |
||
502 | * {@inheritdoc} |
||
503 | */ |
||
504 | 13 | protected function doUpdate(Entity $entity) |
|
523 | |||
524 | /** |
||
525 | * Constructor. |
||
526 | * |
||
527 | * @param EntityDefinition $definition |
||
528 | * the entity definition |
||
529 | * @param FilesystemInterface $filesystem |
||
530 | * the filesystem to use |
||
531 | * @param Connection $database |
||
532 | * the Doctrine DBAL instance to use |
||
533 | * @param boolean $useUUIDs |
||
534 | * flag whether to use UUIDs as primary key |
||
535 | */ |
||
536 | 76 | public function __construct(EntityDefinition $definition, FilesystemInterface $filesystem, Connection $database, $useUUIDs) |
|
544 | |||
545 | /** |
||
546 | * {@inheritdoc} |
||
547 | */ |
||
548 | 34 | public function get($id) |
|
556 | |||
557 | /** |
||
558 | * {@inheritdoc} |
||
559 | */ |
||
560 | 35 | public function listEntries(array $filter = [], array $filterOperators = [], $skip = null, $amount = null, $sortField = null, $sortAscending = null) |
|
586 | |||
587 | /** |
||
588 | * {@inheritdoc} |
||
589 | */ |
||
590 | 8 | public function getIdToNameMap($entity, $nameField) |
|
612 | |||
613 | /** |
||
614 | * {@inheritdoc} |
||
615 | */ |
||
616 | 12 | public function countBy($table, array $params, array $paramsOperators, $excludeDeleted) |
|
657 | |||
658 | /** |
||
659 | * {@inheritdoc} |
||
660 | */ |
||
661 | 2 | public function hasManySet($field, array $thatIds, $excludeId = null) |
|
690 | |||
691 | } |
||
692 |