Complex classes like DomainMapper 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 DomainMapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class DomainMapper |
||
46 | { |
||
47 | const MAX_LOCATION_PRIORITY = 2147483647; |
||
48 | const MIN_LOCATION_PRIORITY = -2147483648; |
||
49 | |||
50 | /** |
||
51 | * @var \eZ\Publish\SPI\Persistence\Content\Handler |
||
52 | */ |
||
53 | protected $contentHandler; |
||
54 | |||
55 | /** |
||
56 | * @var \eZ\Publish\SPI\Persistence\Content\Location\Handler |
||
57 | */ |
||
58 | protected $locationHandler; |
||
59 | |||
60 | /** |
||
61 | * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler |
||
62 | */ |
||
63 | protected $contentTypeHandler; |
||
64 | |||
65 | /** |
||
66 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
67 | */ |
||
68 | protected $contentLanguageHandler; |
||
69 | |||
70 | /** |
||
71 | * @var FieldTypeRegistry |
||
72 | */ |
||
73 | protected $fieldTypeRegistry; |
||
74 | |||
75 | /** |
||
76 | * Setups service with reference to repository. |
||
77 | * |
||
78 | * @param \eZ\Publish\SPI\Persistence\Content\Handler $contentHandler |
||
79 | * @param \eZ\Publish\SPI\Persistence\Content\Location\Handler $locationHandler |
||
80 | * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler |
||
81 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $contentLanguageHandler |
||
82 | * @param FieldTypeRegistry $fieldTypeRegistry |
||
83 | */ |
||
84 | public function __construct( |
||
97 | |||
98 | /** |
||
99 | * Builds a Content domain object from value object returned from persistence. |
||
100 | * |
||
101 | * @param \eZ\Publish\SPI\Persistence\Content $spiContent |
||
102 | * @param ContentType|SPIType $contentType |
||
103 | * @param array $prioritizedLanguages Prioritized language codes to filter fields on |
||
104 | * @param string|null $fieldAlwaysAvailableLanguage Language code fallback if a given field is not found in $prioritizedLanguages |
||
105 | * |
||
106 | * @return \eZ\Publish\Core\Repository\Values\Content\Content |
||
107 | */ |
||
108 | public function buildContentDomainObject( |
||
139 | |||
140 | /** |
||
141 | * Builds a Content proxy object (lazy loaded, loads as soon as used). |
||
142 | */ |
||
143 | public function buildContentProxy( |
||
152 | |||
153 | /** |
||
154 | * Builds a list of Content proxy objects (lazy loaded, loads all as soon as one of them loads). |
||
155 | * |
||
156 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo[] $infoList |
||
157 | * @param string[] $prioritizedLanguages |
||
158 | * @param bool $useAlwaysAvailable |
||
159 | * |
||
160 | * @return \eZ\Publish\API\Repository\Values\Content\Content[<int>] |
||
|
|||
161 | */ |
||
162 | public function buildContentProxyList( |
||
175 | |||
176 | /** |
||
177 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo[] $infoList |
||
178 | * @param string[] $prioritizedLanguages |
||
179 | * @param bool $useAlwaysAvailable |
||
180 | * |
||
181 | * @return \Generator |
||
182 | */ |
||
183 | private function generatorForContentList( |
||
184 | array $infoList, |
||
185 | array $prioritizedLanguages = [], |
||
186 | bool $useAlwaysAvailable = true |
||
187 | ): \Generator { |
||
188 | $contentIds = []; |
||
189 | $translations = $prioritizedLanguages; |
||
190 | foreach ($infoList as $info) { |
||
191 | $contentIds[] = $info->id; |
||
192 | // Unless we are told to load all languages, we add main language to translations so they are loaded too |
||
193 | // Might in some case load more languages then intended, but prioritised handling will pick right one |
||
194 | if (!empty($prioritizedLanguages) && $useAlwaysAvailable && $info->alwaysAvailable) { |
||
195 | $translations[] = $info->mainLanguageCode; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | $list = $this->contentHandler->loadContentList($contentIds, array_unique($translations)); |
||
200 | |||
201 | while (!empty($list)) { |
||
202 | $id = yield; |
||
203 | $info = $list[$id]->versionInfo->contentInfo; |
||
204 | yield $this->buildContentDomainObject( |
||
205 | $list[$id], |
||
206 | null, |
||
207 | //@todo bulk load content type, AND(~/OR~) add in-memory cache for it which will also benefit all cases |
||
208 | $prioritizedLanguages, |
||
209 | $info->alwaysAvailable ? $info->mainLanguageCode : null |
||
210 | ); |
||
211 | |||
212 | unset($list[$id]); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Returns an array of domain fields created from given array of SPI fields. |
||
218 | * |
||
219 | * @throws InvalidArgumentType On invalid $contentType |
||
220 | * |
||
221 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields |
||
222 | * @param ContentType|SPIType $contentType |
||
223 | * @param array $prioritizedLanguages A language priority, filters returned fields and is used as prioritized language code on |
||
224 | * returned value object. If not given all languages are returned. |
||
225 | * @param string|null $alwaysAvailableLanguage Language code fallback if a given field is not found in $prioritizedLanguages |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function buildDomainFields( |
||
230 | array $spiFields, |
||
231 | $contentType, |
||
232 | array $prioritizedLanguages = [], |
||
233 | string $alwaysAvailableLanguage = null |
||
234 | ) { |
||
235 | if (!$contentType instanceof SPIType && !$contentType instanceof ContentType) { |
||
236 | throw new InvalidArgumentType('$contentType', 'SPI ContentType | API ContentType'); |
||
237 | } |
||
238 | |||
239 | $fieldDefinitionsMap = []; |
||
240 | foreach ($contentType->fieldDefinitions as $fieldDefinition) { |
||
241 | $fieldDefinitionsMap[$fieldDefinition->id] = $fieldDefinition; |
||
242 | } |
||
243 | |||
244 | $fieldInFilterLanguagesMap = array(); |
||
245 | if (!empty($prioritizedLanguages) && $alwaysAvailableLanguage !== null) { |
||
246 | foreach ($spiFields as $spiField) { |
||
247 | if (in_array($spiField->languageCode, $prioritizedLanguages)) { |
||
248 | $fieldInFilterLanguagesMap[$spiField->fieldDefinitionId] = true; |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | |||
253 | $fields = array(); |
||
254 | foreach ($spiFields as $spiField) { |
||
255 | // We ignore fields in content not part of the content type |
||
256 | if (!isset($fieldDefinitionsMap[$spiField->fieldDefinitionId])) { |
||
257 | continue; |
||
258 | } |
||
259 | |||
260 | $fieldDefinition = $fieldDefinitionsMap[$spiField->fieldDefinitionId]; |
||
261 | |||
262 | if (!empty($prioritizedLanguages) && !in_array($spiField->languageCode, $prioritizedLanguages)) { |
||
263 | // If filtering is enabled we ignore fields in other languages then $prioritizedLanguages, if: |
||
264 | if ($alwaysAvailableLanguage === null) { |
||
265 | // Ignore field if we don't have $alwaysAvailableLanguageCode fallback |
||
266 | continue; |
||
267 | } elseif (!empty($fieldInFilterLanguagesMap[$spiField->fieldDefinitionId])) { |
||
268 | // Ignore field if it exists in one of the filtered languages |
||
269 | continue; |
||
270 | } elseif ($spiField->languageCode !== $alwaysAvailableLanguage) { |
||
271 | // Also ignore if field is not in $alwaysAvailableLanguageCode |
||
272 | continue; |
||
273 | } |
||
274 | } |
||
275 | |||
276 | $fields[$fieldDefinition->position][] = new Field( |
||
277 | array( |
||
278 | 'id' => $spiField->id, |
||
279 | 'value' => $this->fieldTypeRegistry->getFieldType($spiField->type) |
||
280 | ->fromPersistenceValue($spiField->value), |
||
281 | 'languageCode' => $spiField->languageCode, |
||
282 | 'fieldDefIdentifier' => $fieldDefinition->identifier, |
||
283 | 'fieldTypeIdentifier' => $spiField->type, |
||
284 | ) |
||
285 | ); |
||
286 | } |
||
287 | |||
288 | // Sort fields by content type field definition priority |
||
289 | ksort($fields, SORT_NUMERIC); |
||
290 | |||
291 | // Flatten array |
||
292 | return array_merge(...$fields); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Builds a VersionInfo domain object from value object returned from persistence. |
||
297 | * |
||
298 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $spiVersionInfo |
||
299 | * @param array $prioritizedLanguages |
||
300 | * |
||
301 | * @return \eZ\Publish\Core\Repository\Values\Content\VersionInfo |
||
302 | */ |
||
303 | public function buildVersionInfoDomainObject(SPIVersionInfo $spiVersionInfo, array $prioritizedLanguages = []) |
||
345 | |||
346 | /** |
||
347 | * Builds a ContentInfo domain object from value object returned from persistence. |
||
348 | * |
||
349 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $spiContentInfo |
||
350 | * |
||
351 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
352 | */ |
||
353 | public function buildContentInfoDomainObject(SPIContentInfo $spiContentInfo) |
||
393 | |||
394 | /** |
||
395 | * Builds API Relation object from provided SPI Relation object. |
||
396 | * |
||
397 | * @param \eZ\Publish\SPI\Persistence\Content\Relation $spiRelation |
||
398 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $sourceContentInfo |
||
399 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContentInfo |
||
400 | * |
||
401 | * @return \eZ\Publish\API\Repository\Values\Content\Relation |
||
402 | */ |
||
403 | public function buildRelationDomainObject( |
||
431 | |||
432 | /** |
||
433 | * @deprecated Since 7.2, use buildLocationWithContent(), buildLocation() or (private) mapLocation() instead. |
||
434 | */ |
||
435 | public function buildLocationDomainObject( |
||
449 | |||
450 | public function buildLocation( |
||
451 | SPILocation $spiLocation, |
||
467 | |||
468 | /** |
||
469 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation |
||
470 | * @param \eZ\Publish\API\Repository\Values\Content\Content|null $content |
||
471 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo|null $spiContentInfo |
||
472 | * |
||
473 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
474 | * |
||
475 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
476 | */ |
||
477 | public function buildLocationWithContent( |
||
498 | |||
499 | /** |
||
500 | * Builds API Location object for tree root. |
||
501 | * |
||
502 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation |
||
503 | * |
||
504 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
505 | */ |
||
506 | private function buildRootLocation(SPILocation $spiLocation): APILocation |
||
532 | |||
533 | private function mapLocation(SPILocation $spiLocation, ContentInfo $contentInfo, APIContent $content): APILocation |
||
552 | |||
553 | /** |
||
554 | * Build API Content domain objects in bulk and apply to ContentSearchResult. |
||
555 | * |
||
556 | * Loading of Content objects are done in bulk. |
||
557 | * |
||
558 | * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $result SPI search result with SPI ContentInfo items as hits |
||
559 | * @param array $languageFilter |
||
560 | * |
||
561 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo[] ContentInfo we did not find content for is returned. |
||
562 | */ |
||
563 | public function buildContentDomainObjectsOnSearchResult(SearchResult $result, array $languageFilter) |
||
600 | |||
601 | /** |
||
602 | * Build API Location and corresponding ContentInfo domain objects and apply to LocationSearchResult. |
||
603 | * |
||
604 | * This is done in order to be able to: |
||
605 | * Load ContentInfo objects in bulk, generate proxy objects for Content that will loaded in bulk on-demand (on use). |
||
606 | * |
||
607 | * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $result SPI search result with SPI Location items as hits |
||
608 | * @param array $languageFilter |
||
609 | * |
||
610 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] Locations we did not find content info for is returned. |
||
611 | */ |
||
612 | public function buildLocationDomainObjectsOnSearchResult(SearchResult $result, array $languageFilter) |
||
645 | |||
646 | /** |
||
647 | * Creates an array of SPI location create structs from given array of API location create structs. |
||
648 | * |
||
649 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
650 | * |
||
651 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
652 | * @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation |
||
653 | * @param mixed $mainLocation |
||
654 | * @param mixed $contentId |
||
655 | * @param mixed $contentVersionNo |
||
656 | * |
||
657 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct |
||
658 | */ |
||
659 | public function buildSPILocationCreateStruct( |
||
722 | |||
723 | /** |
||
724 | * Checks if given $sortField value is one of the defined sort field constants. |
||
725 | * |
||
726 | * @param mixed $sortField |
||
727 | * |
||
728 | * @return bool |
||
729 | */ |
||
730 | public function isValidLocationSortField($sortField) |
||
750 | |||
751 | /** |
||
752 | * Checks if given $sortOrder value is one of the defined sort order constants. |
||
753 | * |
||
754 | * @param mixed $sortOrder |
||
755 | * |
||
756 | * @return bool |
||
757 | */ |
||
758 | public function isValidLocationSortOrder($sortOrder) |
||
768 | |||
769 | /** |
||
770 | * Checks if given $priority is valid. |
||
771 | * |
||
772 | * @param int $priority |
||
773 | * |
||
774 | * @return bool |
||
775 | */ |
||
776 | public function isValidLocationPriority($priority) |
||
784 | |||
785 | /** |
||
786 | * Validates given translated list $list, which should be an array of strings with language codes as keys. |
||
787 | * |
||
788 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
789 | * |
||
790 | * @param mixed $list |
||
791 | * @param string $argumentName |
||
792 | */ |
||
793 | public function validateTranslatedList($list, $argumentName) |
||
807 | |||
808 | /** |
||
809 | * Returns \DateTime object from given $timestamp in environment timezone. |
||
810 | * |
||
811 | * This method is needed because constructing \DateTime with $timestamp will |
||
812 | * return the object in UTC timezone. |
||
813 | * |
||
814 | * @param int $timestamp |
||
815 | * |
||
816 | * @return \DateTime |
||
817 | */ |
||
818 | public function getDateTime($timestamp) |
||
825 | |||
826 | /** |
||
827 | * Creates unique hash string for given $object. |
||
828 | * |
||
829 | * Used for remoteId. |
||
830 | * |
||
831 | * @param object $object |
||
832 | * |
||
833 | * @return string |
||
834 | */ |
||
835 | public function getUniqueHash($object) |
||
839 | |||
840 | /** |
||
841 | * Returns true if given location is a tree root. |
||
842 | * |
||
843 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation |
||
844 | * |
||
845 | * @return bool |
||
846 | */ |
||
847 | private function isRootLocation(SPILocation $spiLocation): bool |
||
851 | } |
||
852 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.