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 |
||
20 | class MySQLData extends AbstractData { |
||
21 | |||
22 | /** |
||
23 | * Holds the Doctrine DBAL instance. |
||
24 | */ |
||
25 | protected $database; |
||
26 | |||
27 | /** |
||
28 | * Flag whether to use UUIDs as primary key. |
||
29 | */ |
||
30 | protected $useUUIDs; |
||
31 | |||
32 | /** |
||
33 | * Gets the many-to-many fields. |
||
34 | * |
||
35 | * @return array|\string[] |
||
36 | * the many-to-many fields |
||
37 | */ |
||
38 | protected function getManyFields() { |
||
44 | |||
45 | /** |
||
46 | * Gets all form fields including the many-to-many-ones. |
||
47 | * |
||
48 | * @return array |
||
49 | * all form fields |
||
50 | */ |
||
51 | protected function getFormFields() { |
||
61 | |||
62 | /** |
||
63 | * Sets the values and parameters of the upcoming given query according |
||
64 | * to the entity. |
||
65 | * |
||
66 | * @param Entity $entity |
||
67 | * the entity with its fields and values |
||
68 | * @param QueryBuilder $queryBuilder |
||
69 | * the upcoming query |
||
70 | * @param string $setMethod |
||
71 | * what method to use on the QueryBuilder: 'setValue' or 'set' |
||
72 | */ |
||
73 | protected function setValuesAndParameters(Entity $entity, QueryBuilder $queryBuilder, $setMethod) { |
||
74 | $formFields = $this->getFormFields(); |
||
75 | $count = count($formFields); |
||
76 | for ($i = 0; $i < $count; ++$i) { |
||
77 | $type = $this->definition->getType($formFields[$i]); |
||
78 | $value = $entity->get($formFields[$i]); |
||
79 | if ($type == 'boolean') { |
||
80 | $value = $value ? 1 : 0; |
||
81 | } |
||
82 | if ($type == 'reference' && is_array($value)) { |
||
83 | $value = $value['id']; |
||
84 | } |
||
85 | $queryBuilder->$setMethod('`'.$formFields[$i].'`', '?'); |
||
86 | $queryBuilder->setParameter($i, $value); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Performs the cascading children deletion. |
||
92 | * |
||
93 | * @param integer $id |
||
94 | * the current entities id |
||
95 | * @param boolean $deleteCascade |
||
96 | * whether to delete children and sub children |
||
97 | */ |
||
98 | protected function deleteChildren($id, $deleteCascade) { |
||
99 | foreach ($this->definition->getChildren() as $childArray) { |
||
100 | $childData = $this->definition->getServiceProvider()->getData($childArray[2]); |
||
101 | $children = $childData->listEntries([$childArray[1] => $id]); |
||
102 | foreach ($children as $child) { |
||
103 | $childData->doDelete($child, $deleteCascade); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Checks whether the by id given entity still has children referencing it. |
||
110 | * |
||
111 | * @param integer $id |
||
112 | * the current entities id |
||
113 | * |
||
114 | * @return boolean |
||
115 | * true if the entity still has children |
||
116 | */ |
||
117 | protected function hasChildren($id) { |
||
118 | foreach ($this->definition->getChildren() as $child) { |
||
119 | $queryBuilder = $this->database->createQueryBuilder(); |
||
120 | $queryBuilder |
||
121 | ->select('COUNT(id)') |
||
122 | ->from('`'.$child[0].'`', '`'.$child[0].'`') |
||
123 | ->where('`'.$child[1].'` = ?') |
||
124 | ->andWhere('deleted_at IS NULL') |
||
125 | ->setParameter(0, $id); |
||
126 | $queryResult = $queryBuilder->execute(); |
||
127 | $result = $queryResult->fetch(\PDO::FETCH_NUM); |
||
128 | if ($result[0] > 0) { |
||
129 | return true; |
||
130 | } |
||
131 | } |
||
132 | return false; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | protected function doDelete(Entity $entity, $deleteCascade) { |
||
139 | $result = $this->shouldExecuteEvents($entity, 'before', 'delete'); |
||
140 | if (!$result) { |
||
141 | return static::DELETION_FAILED_EVENT; |
||
142 | } |
||
143 | $id = $entity->get('id'); |
||
144 | if ($deleteCascade) { |
||
145 | $this->deleteChildren($id, $deleteCascade); |
||
146 | } elseif ($this->hasChildren($id)) { |
||
147 | return static::DELETION_FAILED_STILL_REFERENCED; |
||
148 | } |
||
149 | |||
150 | $query = $this->database->createQueryBuilder(); |
||
151 | $query |
||
152 | ->update('`'.$this->definition->getTable().'`') |
||
153 | ->set('deleted_at', 'UTC_TIMESTAMP()') |
||
154 | ->where('id = ?') |
||
155 | ->setParameter(0, $id); |
||
156 | |||
157 | $query->execute(); |
||
158 | $this->shouldExecuteEvents($entity, 'after', 'delete'); |
||
159 | return static::DELETION_SUCCESS; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Gets all possible many-to-many ids existing for this definition. |
||
164 | * |
||
165 | * @param array $fields |
||
166 | * the many field names to fetch for |
||
167 | * @param $params |
||
168 | * the parameters the possible many field values to fetch for |
||
169 | * @return array |
||
170 | * an array of this many-to-many ids |
||
171 | */ |
||
172 | protected function getManyIds(array $fields, array $params) { |
||
173 | $manyIds = []; |
||
174 | foreach ($fields as $field) { |
||
175 | $thisField = $this->definition->getSubTypeField($field, 'many', 'thisField'); |
||
176 | $thatField = $this->definition->getSubTypeField($field, 'many', 'thatField'); |
||
177 | $queryBuilder = $this->database->createQueryBuilder(); |
||
178 | $queryBuilder |
||
179 | ->select('`'.$thisField.'`') |
||
180 | ->from($field) |
||
181 | ->where('`'.$thatField.'` IN (?)') |
||
182 | ->setParameter(0, array_column($params[$field], 'id'), Connection::PARAM_STR_ARRAY) |
||
183 | ->groupBy('`'.$thisField.'`') |
||
184 | ; |
||
185 | $queryResult = $queryBuilder->execute(); |
||
186 | $manyResults = $queryResult->fetchAll(\PDO::FETCH_ASSOC); |
||
187 | $manyIds = array_merge($manyIds, array_column($manyResults, $thisField)); |
||
188 | |||
189 | } |
||
190 | return $manyIds; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Adds sorting parameters to the query. |
||
195 | * |
||
196 | * @param QueryBuilder $queryBuilder |
||
197 | * the query |
||
198 | * @param $filter |
||
199 | * the filter all resulting entities must fulfill, the keys as field names |
||
200 | * @param $filterOperators |
||
201 | * the operators of the filter like "=" defining the full condition of the field |
||
202 | */ |
||
203 | protected function addFilter(QueryBuilder $queryBuilder, array $filter, array $filterOperators) { |
||
229 | |||
230 | /** |
||
231 | * Adds pagination parameters to the query. |
||
232 | * |
||
233 | * @param QueryBuilder $queryBuilder |
||
234 | * the query |
||
235 | * @param integer|null $skip |
||
236 | * the rows to skip |
||
237 | * @param integer|null $amount |
||
238 | * the maximum amount of rows |
||
239 | */ |
||
240 | protected function addPagination(QueryBuilder $queryBuilder, $skip, $amount) { |
||
249 | |||
250 | /** |
||
251 | * Adds sorting parameters to the query. |
||
252 | * |
||
253 | * @param QueryBuilder $queryBuilder |
||
254 | * the query |
||
255 | * @param string|null $sortField |
||
256 | * the sort field |
||
257 | * @param boolean|null $sortAscending |
||
258 | * true if sort ascending, false if descending |
||
259 | */ |
||
260 | protected function addSort(QueryBuilder $queryBuilder, $sortField, $sortAscending) { |
||
272 | |||
273 | |||
274 | /** |
||
275 | * Gets an array of reference ids for the given entities. |
||
276 | * |
||
277 | * @param array $entities |
||
278 | * the entities to extract the ids |
||
279 | * @param $field |
||
280 | * the reference field |
||
281 | * |
||
282 | * @return array |
||
283 | * the extracted ids |
||
284 | */ |
||
285 | protected function getReferenceIds(array $entities, $field) { |
||
292 | |||
293 | /** |
||
294 | * Adds the id and name of referenced entities to the given entities. The |
||
295 | * reference field is before the raw id of the referenced entity and after |
||
296 | * the fetch, it's an array with the keys id and name. |
||
297 | * |
||
298 | * @param Entity[] &$entities |
||
299 | * the entities to fetch the references for |
||
300 | * @param string $field |
||
301 | * the reference field |
||
302 | */ |
||
303 | 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 | protected function generateUUID() { |
||
354 | |||
355 | /** |
||
356 | * Enriches the given mapping of entity id to raw entity data with some many-to-many data. |
||
357 | * |
||
358 | * @param array $idToData |
||
359 | * a reference to the map entity id to raw entity data |
||
360 | * @param $manyField |
||
361 | * the many field to enrich data with |
||
362 | */ |
||
363 | protected function enrichWithManyField(&$idToData, $manyField) { |
||
386 | |||
387 | /** |
||
388 | * Fetches to the rows belonging many-to-many entries and adds them to the rows. |
||
389 | * |
||
390 | * @param array $rows |
||
391 | * the rows to enrich |
||
392 | * @return array |
||
393 | * the enriched rows |
||
394 | */ |
||
395 | protected function enrichWithMany(array $rows) { |
||
409 | |||
410 | /** |
||
411 | * First, deletes all to the given entity related many-to-many entries from the DB |
||
412 | * and then writes them again. |
||
413 | * |
||
414 | * @param Entity $entity |
||
415 | * the entity to save the many-to-many entries of |
||
416 | */ |
||
417 | protected function saveMany(Entity $entity) { |
||
433 | |||
434 | /** |
||
435 | * Adds the id and name of referenced entities to the given entities. Each |
||
436 | * reference field is before the raw id of the referenced entity and after |
||
437 | * the fetch, it's an array with the keys id and name. |
||
438 | * |
||
439 | * @param Entity[] &$entities |
||
440 | * the entities to fetch the references for |
||
441 | * |
||
442 | * @return void |
||
443 | */ |
||
444 | protected function enrichWithReference(array &$entities) { |
||
455 | |||
456 | /** |
||
457 | * Constructor. |
||
458 | * |
||
459 | * @param EntityDefinition $definition |
||
460 | * the entity definition |
||
461 | * @param FileProcessorInterface $fileProcessor |
||
462 | * the file processor to use |
||
463 | * @param $database |
||
464 | * the Doctrine DBAL instance to use |
||
465 | * @param boolean $useUUIDs |
||
466 | * flag whether to use UUIDs as primary key |
||
467 | */ |
||
468 | public function __construct(EntityDefinition $definition, FileProcessorInterface $fileProcessor, $database, $useUUIDs) { |
||
474 | |||
475 | /** |
||
476 | * {@inheritdoc} |
||
477 | */ |
||
478 | public function get($id) { |
||
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | public function listEntries(array $filter = [], array $filterOperators = [], $skip = null, $amount = null, $sortField = null, $sortAscending = null) { |
||
513 | |||
514 | /** |
||
515 | * {@inheritdoc} |
||
516 | */ |
||
517 | public function create(Entity $entity) { |
||
556 | |||
557 | /** |
||
558 | * {@inheritdoc} |
||
559 | */ |
||
560 | public function update(Entity $entity) { |
||
584 | |||
585 | /** |
||
586 | * {@inheritdoc} |
||
587 | */ |
||
588 | public function getIdToNameMap($entity, $nameField) { |
||
608 | |||
609 | /** |
||
610 | * {@inheritdoc} |
||
611 | */ |
||
612 | public function countBy($table, array $params, array $paramsOperators, $excludeDeleted) { |
||
652 | |||
653 | /** |
||
654 | * {@inheritdoc} |
||
655 | */ |
||
656 | public function hasManySet($field, array $thatIds, $excludeId = null) { |
||
678 | |||
679 | } |
||
680 |