Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ContentManager 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 ContentManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ContentManager extends RepositoryExecutor implements MigrationGeneratorInterface |
||
29 | { |
||
30 | protected $supportedStepTypes = array('content'); |
||
31 | protected $supportedActions = array('create', 'load', 'update', 'delete'); |
||
32 | |||
33 | protected $contentMatcher; |
||
34 | protected $sectionMatcher; |
||
35 | protected $userMatcher; |
||
36 | protected $objectStateMatcher; |
||
37 | protected $objectStateGroupMatcher; |
||
38 | protected $fieldHandlerManager; |
||
39 | protected $locationManager; |
||
40 | protected $sortConverter; |
||
41 | |||
42 | 79 | public function __construct( |
|
61 | |||
62 | /** |
||
63 | * Handles the content create migration action type |
||
64 | */ |
||
65 | 14 | protected function create($step) |
|
66 | { |
||
67 | 14 | $contentService = $this->repository->getContentService(); |
|
68 | 14 | $locationService = $this->repository->getLocationService(); |
|
69 | 14 | $contentTypeService = $this->repository->getContentTypeService(); |
|
70 | |||
71 | 14 | $contentTypeIdentifier = $step->dsl['content_type']; |
|
72 | 14 | $contentTypeIdentifier = $this->referenceResolver->resolveReference($contentTypeIdentifier); |
|
73 | /// @todo use a contenttypematcher |
||
74 | 14 | $contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier); |
|
75 | |||
76 | 14 | $contentCreateStruct = $contentService->newContentCreateStruct($contentType, $this->getLanguageCode($step)); |
|
77 | |||
78 | 14 | $this->setFields($contentCreateStruct, $step->dsl['attributes'], $contentType, $step); |
|
79 | |||
80 | 12 | if (isset($step->dsl['always_available'])) { |
|
81 | $contentCreateStruct->alwaysAvailable = $step->dsl['always_available']; |
||
82 | } else { |
||
83 | // Could be removed when https://github.com/ezsystems/ezpublish-kernel/pull/1874 is merged, |
||
84 | // but we strive to support old eZ kernel versions as well... |
||
85 | 12 | $contentCreateStruct->alwaysAvailable = $contentType->defaultAlwaysAvailable; |
|
86 | } |
||
87 | |||
88 | 12 | if (isset($step->dsl['remote_id'])) { |
|
89 | 3 | $contentCreateStruct->remoteId = $step->dsl['remote_id']; |
|
90 | } |
||
91 | |||
92 | 12 | View Code Duplication | if (isset($step->dsl['section'])) { |
|
|||
93 | 1 | $sectionKey = $this->referenceResolver->resolveReference($step->dsl['section']); |
|
94 | 1 | $section = $this->sectionMatcher->matchOneByKey($sectionKey); |
|
95 | 1 | $contentCreateStruct->sectionId = $section->id; |
|
96 | } |
||
97 | |||
98 | 12 | View Code Duplication | if (isset($step->dsl['owner'])) { |
99 | 2 | $owner = $this->getUser($step->dsl['owner']); |
|
100 | 2 | $contentCreateStruct->ownerId = $owner->id; |
|
101 | } |
||
102 | |||
103 | // This is a bit tricky, as the eZPublish API does not support having a different creator and owner with only 1 version. |
||
104 | // We allow it, hoping that nothing gets broken because of it |
||
105 | 12 | if (isset($step->dsl['version_creator'])) { |
|
106 | 1 | $realContentOwnerId = $contentCreateStruct->ownerId; |
|
107 | 1 | if ($realContentOwnerId == null) { |
|
108 | 1 | $realContentOwnerId = $this->repository->getCurrentUser()->id; |
|
109 | } |
||
110 | 1 | $versionCreator = $this->getUser($step->dsl['version_creator']); |
|
111 | 1 | $contentCreateStruct->ownerId = $versionCreator->id; |
|
112 | } |
||
113 | |||
114 | 12 | if (isset($step->dsl['modification_date'])) { |
|
115 | 1 | $contentCreateStruct->modificationDate = $this->toDateTime($step->dsl['modification_date']); |
|
116 | } |
||
117 | |||
118 | // instantiate a location create struct from the parent location: |
||
119 | // BC |
||
120 | 12 | $locationId = isset($step->dsl['parent_location']) ? $step->dsl['parent_location'] : ( |
|
121 | 12 | isset($step->dsl['main_location']) ? $step->dsl['main_location'] : null |
|
122 | ); |
||
123 | // 1st resolve references |
||
124 | 12 | $locationId = $this->referenceResolver->resolveReference($locationId); |
|
125 | // 2nd allow to specify the location via remote_id |
||
126 | 12 | $locationId = $this->locationManager->matchLocationByKey($locationId)->id; |
|
127 | 12 | $locationCreateStruct = $locationService->newLocationCreateStruct($locationId); |
|
128 | |||
129 | 12 | if (isset($step->dsl['location_remote_id'])) { |
|
130 | 2 | $locationCreateStruct->remoteId = $step->dsl['location_remote_id']; |
|
131 | } |
||
132 | |||
133 | 12 | if (isset($step->dsl['priority'])) { |
|
134 | 1 | $locationCreateStruct->priority = $step->dsl['priority']; |
|
135 | } |
||
136 | |||
137 | 12 | if (isset($step->dsl['is_hidden'])) { |
|
138 | 1 | $locationCreateStruct->hidden = $step->dsl['is_hidden']; |
|
139 | } |
||
140 | |||
141 | 12 | if (isset($step->dsl['sort_field'])) { |
|
142 | 1 | $locationCreateStruct->sortField = $this->sortConverter->hash2SortField($step->dsl['sort_field']); |
|
143 | } else { |
||
144 | 12 | $locationCreateStruct->sortField = $contentType->defaultSortField; |
|
145 | } |
||
146 | |||
147 | 12 | if (isset($step->dsl['sort_order'])) { |
|
148 | 1 | $locationCreateStruct->sortOrder = $this->sortConverter->hash2SortOrder($step->dsl['sort_order']); |
|
149 | } else { |
||
150 | 12 | $locationCreateStruct->sortOrder = $contentType->defaultSortOrder; |
|
151 | } |
||
152 | |||
153 | 12 | $locations = array($locationCreateStruct); |
|
154 | |||
155 | // BC |
||
156 | 12 | $other_locations = isset($step->dsl['other_parent_locations']) ? $step->dsl['other_parent_locations'] : ( |
|
157 | 12 | isset($step->dsl['other_locations']) ? $step->dsl['other_locations'] : null |
|
158 | ); |
||
159 | 12 | if (isset($other_locations)) { |
|
160 | foreach ($other_locations as $locationId) { |
||
161 | $locationId = $this->referenceResolver->resolveReference($locationId); |
||
162 | $locationId = $this->locationManager->matchLocationByKey($locationId)->id; |
||
163 | $secondaryLocationCreateStruct = $locationService->newLocationCreateStruct($locationId); |
||
164 | array_push($locations, $secondaryLocationCreateStruct); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // create a draft using the content and location create struct and publish it |
||
169 | 12 | $draft = $contentService->createContent($contentCreateStruct, $locations); |
|
170 | 10 | $content = $contentService->publishVersion($draft->versionInfo); |
|
171 | |||
172 | 10 | if (isset($step->dsl['object_states'])) { |
|
173 | 2 | $this->setObjectStates($content, $step->dsl['object_states']); |
|
174 | } |
||
175 | |||
176 | // 2nd part of the hack: re-set the content owner to its intended value |
||
177 | 10 | if (isset($step->dsl['version_creator']) || isset($step->dsl['publication_date'])) { |
|
178 | 1 | $contentMetaDataUpdateStruct = $contentService->newContentMetadataUpdateStruct(); |
|
179 | |||
180 | 1 | if (isset($step->dsl['version_creator'])) { |
|
181 | 1 | $contentMetaDataUpdateStruct->ownerId = $realContentOwnerId; |
|
182 | } |
||
183 | 1 | if (isset($step->dsl['publication_date'])) { |
|
184 | 1 | $contentMetaDataUpdateStruct->publishedDate = $this->toDateTime($step->dsl['publication_date']); |
|
185 | } |
||
186 | // we have to do this to make sure we preserve the custom modification date |
||
187 | 1 | if (isset($step->dsl['modification_date'])) { |
|
188 | 1 | $contentMetaDataUpdateStruct->modificationDate = $this->toDateTime($step->dsl['modification_date']); |
|
189 | } |
||
190 | |||
191 | 1 | $contentService->updateContentMetadata($content->contentInfo, $contentMetaDataUpdateStruct); |
|
192 | } |
||
193 | |||
194 | 10 | $this->setReferences($content, $step); |
|
195 | |||
196 | 10 | return $content; |
|
197 | } |
||
198 | |||
199 | 5 | protected function load($step) |
|
207 | |||
208 | /** |
||
209 | * Handles the content update migration action type |
||
210 | * |
||
211 | * @todo handle updating of more metadata fields |
||
212 | */ |
||
213 | 9 | protected function update($step) |
|
305 | |||
306 | /** |
||
307 | * Handles the content delete migration action type |
||
308 | */ |
||
309 | 8 | protected function delete($step) |
|
328 | |||
329 | /** |
||
330 | * @param string $action |
||
331 | * @return ContentCollection |
||
332 | * @throws \Exception |
||
333 | */ |
||
334 | 13 | View Code Duplication | protected function matchContents($action, $step) |
357 | |||
358 | /** |
||
359 | * @param Content $content |
||
360 | * @param array $references the definitions of the references to set |
||
361 | * @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute |
||
362 | * @return array key: the reference names, values: the reference values |
||
363 | */ |
||
364 | 9 | protected function getReferencesValues($content, array $references, $step) |
|
365 | { |
||
366 | 9 | $refs = array(); |
|
367 | |||
368 | 9 | foreach ($references as $reference) { |
|
369 | |||
370 | 9 | switch ($reference['attribute']) { |
|
371 | 9 | case 'object_id': |
|
372 | 9 | case 'content_id': |
|
373 | 8 | case 'id': |
|
374 | 9 | $value = $content->id; |
|
375 | 9 | break; |
|
376 | 6 | case 'remote_id': |
|
377 | 6 | case 'content_remote_id': |
|
378 | 2 | $value = $content->contentInfo->remoteId; |
|
379 | 2 | break; |
|
380 | 6 | case 'always_available': |
|
381 | 1 | $value = $content->contentInfo->alwaysAvailable; |
|
382 | 1 | break; |
|
383 | 6 | case 'content_type_id': |
|
384 | 1 | $value = $content->contentInfo->contentTypeId; |
|
385 | 1 | break; |
|
386 | 6 | case 'content_type_identifier': |
|
387 | 1 | $contentTypeService = $this->repository->getContentTypeService(); |
|
388 | 1 | $value = $contentTypeService->loadContentType($content->contentInfo->contentTypeId)->identifier; |
|
389 | 1 | break; |
|
390 | 6 | case 'current_version': |
|
391 | 6 | case 'current_version_no': |
|
392 | 1 | $value = $content->contentInfo->currentVersionNo; |
|
393 | 1 | break; |
|
394 | 6 | case 'location_id': |
|
395 | 4 | case 'main_location_id': |
|
396 | 3 | $value = $content->contentInfo->mainLocationId; |
|
397 | 3 | break; |
|
398 | 4 | case 'main_language_code': |
|
399 | 1 | $value = $content->contentInfo->mainLanguageCode; |
|
400 | 1 | break; |
|
401 | 4 | case 'modification_date': |
|
402 | 1 | $value = $content->contentInfo->modificationDate->getTimestamp(); |
|
403 | 1 | break; |
|
404 | 4 | case 'name': |
|
405 | 1 | $value = $content->contentInfo->name; |
|
406 | 1 | break; |
|
407 | 4 | case 'owner_id': |
|
408 | 1 | $value = $content->contentInfo->ownerId; |
|
409 | 1 | break; |
|
410 | 4 | case 'path': |
|
411 | 2 | $locationService = $this->repository->getLocationService(); |
|
412 | 2 | $value = $locationService->loadLocation($content->contentInfo->mainLocationId)->pathString; |
|
413 | 2 | break; |
|
414 | 3 | case 'publication_date': |
|
415 | 1 | $value = $content->contentInfo->publishedDate->getTimestamp(); |
|
416 | 1 | break; |
|
417 | 3 | case 'section_id': |
|
418 | 1 | $value = $content->contentInfo->sectionId; |
|
419 | 1 | break; |
|
420 | 3 | case 'section_identifier': |
|
421 | 1 | $sectionService = $this->repository->getSectionService(); |
|
422 | 1 | $value = $sectionService->loadSection($content->contentInfo->sectionId)->identifier; |
|
423 | 1 | break; |
|
424 | 2 | case 'version_count': |
|
425 | 1 | $contentService = $this->repository->getContentService(); |
|
426 | 1 | $value = count($contentService->loadVersions($content->contentInfo)); |
|
427 | 1 | break; |
|
428 | default: |
||
429 | 1 | if (strpos($reference['attribute'], 'object_state.') === 0) { |
|
430 | 1 | $stateGroupKey = substr($reference['attribute'], 13); |
|
431 | 1 | $stateGroup = $this->objectStateGroupMatcher->matchOneByKey($stateGroupKey); |
|
432 | 1 | $value = $stateGroupKey . '/' . $this->repository->getObjectStateService()-> |
|
433 | 1 | getContentState($content->contentInfo, $stateGroup)->identifier; |
|
434 | 1 | break; |
|
435 | } |
||
436 | |||
437 | // allow to get the value of fields as well as their sub-parts |
||
438 | if (strpos($reference['attribute'], 'attributes.') === 0) { |
||
439 | $contentType = $this->repository->getContentTypeService()->loadContentType( |
||
440 | $content->contentInfo->contentTypeId |
||
441 | ); |
||
442 | $parts = explode('.', $reference['attribute']); |
||
443 | // totally not sure if this list of special chars is correct for what could follow a jmespath identifier... |
||
444 | // also what about quoted strings? |
||
445 | $fieldIdentifier = preg_replace('/[[(|&!{].*$/', '', $parts[1]); |
||
446 | $field = $content->getField($fieldIdentifier); |
||
447 | $fieldDefinition = $contentType->getFieldDefinition($fieldIdentifier); |
||
448 | $hashValue = $this->fieldHandlerManager->fieldValueToHash( |
||
449 | $fieldDefinition->fieldTypeIdentifier, $contentType->identifier, $field->value |
||
450 | ); |
||
451 | if (is_array($hashValue)) { |
||
452 | View Code Duplication | if (count($parts) == 2 && $fieldIdentifier === $parts[1]) { |
|
453 | throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute'] . ': the given attribute has an array value'); |
||
454 | } |
||
455 | $value = JmesPath::search(implode('.', array_slice($parts, 1)), array($fieldIdentifier => $hashValue)); |
||
456 | } else { |
||
457 | if (count($parts) > 2) { |
||
458 | throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute'] . ': the given attribute has a scalar value'); |
||
459 | } |
||
460 | $value = $hashValue; |
||
461 | } |
||
462 | break; |
||
463 | } |
||
464 | |||
465 | throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute']); |
||
466 | } |
||
467 | |||
468 | 9 | $refs[$reference['identifier']] = $value; |
|
469 | } |
||
470 | |||
471 | 9 | return $refs; |
|
472 | } |
||
473 | |||
474 | /** |
||
475 | * @param array $matchCondition |
||
476 | * @param string $mode |
||
477 | * @param array $context |
||
478 | * @throws \Exception |
||
479 | * @return array |
||
480 | * |
||
481 | * @todo add support for dumping all object languages |
||
482 | * @todo add 2ndary locations when in 'update' mode |
||
483 | * @todo add dumping of sort_field and sort_order for 2ndary locations |
||
484 | */ |
||
485 | 3 | public function generateMigration(array $matchCondition, $mode, array $context = array()) |
|
586 | |||
587 | /** |
||
588 | * Helper function to set the fields of a ContentCreateStruct based on the DSL attribute settings. |
||
589 | * |
||
590 | * @param ContentCreateStruct|ContentUpdateStruct $createOrUpdateStruct |
||
591 | * @param array $fields see description of expected format in code below |
||
592 | * @param ContentType $contentType |
||
593 | * @param $step |
||
594 | * @throws \Exception |
||
595 | */ |
||
596 | 14 | protected function setFields($createOrUpdateStruct, array $fields, ContentType $contentType, $step) |
|
612 | |||
613 | /** |
||
614 | * Helper function to accommodate the definition of fields |
||
615 | * - using a legacy DSL version |
||
616 | * - using either single-language or multi-language style |
||
617 | * |
||
618 | * @param array $fields |
||
619 | * @return array |
||
620 | */ |
||
621 | 14 | protected function normalizeFieldDefs($fields, $step) |
|
653 | |||
654 | /** |
||
655 | * Checks whether all fields are using multilang syntax ie. a valid language as key. |
||
656 | * |
||
657 | * @param array $fields |
||
658 | * @return bool |
||
659 | */ |
||
660 | 14 | protected function hasLanguageCodesAsKeys(array $fields) |
|
678 | |||
679 | /** |
||
680 | * Returns all enabled Languages in the repo. |
||
681 | * @todo move to parent class? |
||
682 | * |
||
683 | * @return string[] |
||
684 | */ |
||
685 | 14 | protected function getContentLanguageCodes() |
|
699 | |||
700 | 1 | View Code Duplication | protected function setSection(Content $content, $sectionKey) |
708 | |||
709 | 2 | protected function setObjectStates(Content $content, array $stateKeys) |
|
720 | |||
721 | 1 | protected function setMainLocation(Content $content, $locationId) |
|
739 | |||
740 | /** |
||
741 | * Create the field value from the migration definition hash |
||
742 | * |
||
743 | * @param mixed $value |
||
744 | * @param FieldDefinition $fieldDefinition |
||
745 | * @param string $contentTypeIdentifier |
||
746 | * @param array $context |
||
747 | * @throws \InvalidArgumentException |
||
748 | * @return mixed |
||
749 | */ |
||
750 | 14 | protected function getFieldValue($value, FieldDefinition $fieldDefinition, $contentTypeIdentifier, array $context = array()) |
|
767 | |||
768 | /** |
||
769 | * Create the field value for a primitive field from the migration definition hash |
||
770 | * |
||
771 | * @param mixed $value |
||
772 | * @param FieldDefinition $fieldDefinition |
||
773 | * @param string $contentTypeIdentifier |
||
774 | * @param array $context |
||
775 | * @throws \InvalidArgumentException |
||
776 | * @return mixed |
||
777 | */ |
||
778 | 12 | protected function getSingleFieldValue($value, FieldDefinition $fieldDefinition, $contentTypeIdentifier, array $context = array()) |
|
788 | |||
789 | /** |
||
790 | * Load user using either login, email, id - resolving eventual references |
||
791 | * @param int|string $userKey |
||
792 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
793 | */ |
||
794 | 2 | protected function getUser($userKey) |
|
799 | |||
800 | /** |
||
801 | * @param int|string $date if integer, we assume a timestamp |
||
802 | * @return \DateTime |
||
803 | */ |
||
804 | 1 | protected function toDateTime($date) |
|
812 | } |
||
813 |
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.