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\Core\Repository\Helper\ContentTypeDomainMapper |
||
67 | */ |
||
68 | protected $contentTypeDomainMapper; |
||
69 | |||
70 | /** |
||
71 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
72 | */ |
||
73 | protected $contentLanguageHandler; |
||
74 | |||
75 | /** |
||
76 | * @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry |
||
77 | */ |
||
78 | protected $fieldTypeRegistry; |
||
79 | |||
80 | /** |
||
81 | * Setups service with reference to repository. |
||
82 | * |
||
83 | * @param \eZ\Publish\SPI\Persistence\Content\Handler $contentHandler |
||
84 | * @param \eZ\Publish\SPI\Persistence\Content\Location\Handler $locationHandler |
||
85 | * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler |
||
86 | * @param \eZ\Publish\Core\Repository\Helper\ContentTypeDomainMapper $contentTypeDomainMapper |
||
87 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $contentLanguageHandler |
||
88 | * @param \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry $fieldTypeRegistry |
||
89 | */ |
||
90 | public function __construct( |
||
105 | |||
106 | /** |
||
107 | * Builds a Content domain object from value object returned from persistence. |
||
108 | * |
||
109 | * @param \eZ\Publish\SPI\Persistence\Content $spiContent |
||
110 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
111 | * @param array $prioritizedLanguages Prioritized language codes to filter fields on |
||
112 | * @param string|null $fieldAlwaysAvailableLanguage Language code fallback if a given field is not found in $prioritizedLanguages |
||
113 | * |
||
114 | * @return \eZ\Publish\Core\Repository\Values\Content\Content |
||
115 | */ |
||
116 | public function buildContentDomainObject( |
||
142 | |||
143 | /** |
||
144 | * Builds a Content proxy object (lazy loaded, loads as soon as used). |
||
145 | */ |
||
146 | public function buildContentProxy( |
||
155 | |||
156 | /** |
||
157 | * Builds a list of Content proxy objects (lazy loaded, loads all as soon as one of them loads). |
||
158 | * |
||
159 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo[] $infoList |
||
160 | * @param string[] $prioritizedLanguages |
||
161 | * @param bool $useAlwaysAvailable |
||
162 | * |
||
163 | * @return \eZ\Publish\API\Repository\Values\Content\Content[<int>] |
||
|
|||
164 | */ |
||
165 | public function buildContentProxyList( |
||
178 | |||
179 | /** |
||
180 | * @todo Maybe change signature to generatorForContentList($contentIds, $prioritizedLanguages, $translations) |
||
181 | * @todo to avoid keeping referance to $infoList all the way until the generator is called. |
||
182 | * |
||
183 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo[] $infoList |
||
184 | * @param string[] $prioritizedLanguages |
||
185 | * @param bool $useAlwaysAvailable |
||
186 | * |
||
187 | * @return \Generator |
||
188 | */ |
||
189 | private function generatorForContentList( |
||
190 | array $infoList, |
||
191 | array $prioritizedLanguages = [], |
||
192 | bool $useAlwaysAvailable = true |
||
193 | ): \Generator { |
||
194 | $contentIds = []; |
||
195 | $contentTypeIds = []; |
||
196 | $translations = $prioritizedLanguages; |
||
197 | foreach ($infoList as $info) { |
||
198 | $contentIds[] = $info->id; |
||
199 | $contentTypeIds[] = $info->contentTypeId; |
||
200 | // Unless we are told to load all languages, we add main language to translations so they are loaded too |
||
201 | // Might in some case load more languages then intended, but prioritised handling will pick right one |
||
202 | if (!empty($prioritizedLanguages) && $useAlwaysAvailable && $info->alwaysAvailable) { |
||
203 | $translations[] = $info->mainLanguageCode; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | unset($infoList); |
||
208 | |||
209 | $contentList = $this->contentHandler->loadContentList($contentIds, array_unique($translations)); |
||
210 | $contentTypeList = $this->contentTypeHandler->loadContentTypeList(array_unique($contentTypeIds)); |
||
211 | while (!empty($contentList)) { |
||
212 | $id = yield; |
||
213 | /** @var \eZ\Publish\SPI\Persistence\Content\ContentInfo $info */ |
||
214 | $info = $contentList[$id]->versionInfo->contentInfo; |
||
215 | yield $this->buildContentDomainObject( |
||
216 | $contentList[$id], |
||
217 | $this->contentTypeDomainMapper->buildContentTypeDomainObject( |
||
218 | $contentTypeList[$info->contentTypeId], |
||
219 | $prioritizedLanguages |
||
220 | ), |
||
221 | $prioritizedLanguages, |
||
222 | $info->alwaysAvailable ? $info->mainLanguageCode : null |
||
223 | ); |
||
224 | |||
225 | unset($contentList[$id]); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Returns an array of domain fields created from given array of SPI fields. |
||
231 | * |
||
232 | * @throws InvalidArgumentType On invalid $contentType |
||
233 | * |
||
234 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields |
||
235 | * @param ContentType|SPIType $contentType |
||
236 | * @param array $prioritizedLanguages A language priority, filters returned fields and is used as prioritized language code on |
||
237 | * returned value object. If not given all languages are returned. |
||
238 | * @param string|null $alwaysAvailableLanguage Language code fallback if a given field is not found in $prioritizedLanguages |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | public function buildDomainFields( |
||
307 | |||
308 | /** |
||
309 | * Builds a VersionInfo domain object from value object returned from persistence. |
||
310 | * |
||
311 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $spiVersionInfo |
||
312 | * @param array $prioritizedLanguages |
||
313 | * |
||
314 | * @return \eZ\Publish\Core\Repository\Values\Content\VersionInfo |
||
315 | */ |
||
316 | public function buildVersionInfoDomainObject(SPIVersionInfo $spiVersionInfo, array $prioritizedLanguages = []) |
||
358 | |||
359 | /** |
||
360 | * Builds a ContentInfo domain object from value object returned from persistence. |
||
361 | * |
||
362 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $spiContentInfo |
||
363 | * |
||
364 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
365 | */ |
||
366 | public function buildContentInfoDomainObject(SPIContentInfo $spiContentInfo) |
||
407 | |||
408 | /** |
||
409 | * Builds API Relation object from provided SPI Relation object. |
||
410 | * |
||
411 | * @param \eZ\Publish\SPI\Persistence\Content\Relation $spiRelation |
||
412 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $sourceContentInfo |
||
413 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContentInfo |
||
414 | * |
||
415 | * @return \eZ\Publish\API\Repository\Values\Content\Relation |
||
416 | */ |
||
417 | public function buildRelationDomainObject( |
||
445 | |||
446 | /** |
||
447 | * @deprecated Since 7.2, use buildLocationWithContent(), buildLocation() or (private) mapLocation() instead. |
||
448 | */ |
||
449 | public function buildLocationDomainObject( |
||
463 | |||
464 | public function buildLocation( |
||
481 | |||
482 | /** |
||
483 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation |
||
484 | * @param \eZ\Publish\API\Repository\Values\Content\Content|null $content |
||
485 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo|null $spiContentInfo |
||
486 | * |
||
487 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
488 | * |
||
489 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
490 | */ |
||
491 | public function buildLocationWithContent( |
||
512 | |||
513 | /** |
||
514 | * Builds API Location object for tree root. |
||
515 | * |
||
516 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation |
||
517 | * |
||
518 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
519 | */ |
||
520 | private function buildRootLocation(SPILocation $spiLocation): APILocation |
||
546 | |||
547 | private function mapLocation(SPILocation $spiLocation, ContentInfo $contentInfo, APIContent $content): APILocation |
||
566 | |||
567 | /** |
||
568 | * Build API Content domain objects in bulk and apply to ContentSearchResult. |
||
569 | * |
||
570 | * Loading of Content objects are done in bulk. |
||
571 | * |
||
572 | * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $result SPI search result with SPI ContentInfo items as hits |
||
573 | * @param array $languageFilter |
||
574 | * |
||
575 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo[] ContentInfo we did not find content for is returned. |
||
576 | */ |
||
577 | public function buildContentDomainObjectsOnSearchResult(SearchResult $result, array $languageFilter) |
||
622 | |||
623 | /** |
||
624 | * Build API Location and corresponding ContentInfo domain objects and apply to LocationSearchResult. |
||
625 | * |
||
626 | * This is done in order to be able to: |
||
627 | * Load ContentInfo objects in bulk, generate proxy objects for Content that will loaded in bulk on-demand (on use). |
||
628 | * |
||
629 | * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $result SPI search result with SPI Location items as hits |
||
630 | * @param array $languageFilter |
||
631 | * |
||
632 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] Locations we did not find content info for is returned. |
||
633 | */ |
||
634 | public function buildLocationDomainObjectsOnSearchResult(SearchResult $result, array $languageFilter) |
||
667 | |||
668 | /** |
||
669 | * Creates an array of SPI location create structs from given array of API location create structs. |
||
670 | * |
||
671 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
672 | * |
||
673 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
674 | * @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation |
||
675 | * @param mixed $mainLocation |
||
676 | * @param mixed $contentId |
||
677 | * @param mixed $contentVersionNo |
||
678 | * |
||
679 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct |
||
680 | */ |
||
681 | public function buildSPILocationCreateStruct( |
||
744 | |||
745 | /** |
||
746 | * Checks if given $sortField value is one of the defined sort field constants. |
||
747 | * |
||
748 | * @param mixed $sortField |
||
749 | * |
||
750 | * @return bool |
||
751 | */ |
||
752 | public function isValidLocationSortField($sortField) |
||
772 | |||
773 | /** |
||
774 | * Checks if given $sortOrder value is one of the defined sort order constants. |
||
775 | * |
||
776 | * @param mixed $sortOrder |
||
777 | * |
||
778 | * @return bool |
||
779 | */ |
||
780 | public function isValidLocationSortOrder($sortOrder) |
||
790 | |||
791 | /** |
||
792 | * Checks if given $priority is valid. |
||
793 | * |
||
794 | * @param int $priority |
||
795 | * |
||
796 | * @return bool |
||
797 | */ |
||
798 | public function isValidLocationPriority($priority) |
||
806 | |||
807 | /** |
||
808 | * Validates given translated list $list, which should be an array of strings with language codes as keys. |
||
809 | * |
||
810 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
811 | * |
||
812 | * @param mixed $list |
||
813 | * @param string $argumentName |
||
814 | */ |
||
815 | public function validateTranslatedList($list, $argumentName) |
||
829 | |||
830 | /** |
||
831 | * Returns \DateTime object from given $timestamp in environment timezone. |
||
832 | * |
||
833 | * This method is needed because constructing \DateTime with $timestamp will |
||
834 | * return the object in UTC timezone. |
||
835 | * |
||
836 | * @param int $timestamp |
||
837 | * |
||
838 | * @return \DateTime |
||
839 | */ |
||
840 | public function getDateTime($timestamp) |
||
847 | |||
848 | /** |
||
849 | * Creates unique hash string for given $object. |
||
850 | * |
||
851 | * Used for remoteId. |
||
852 | * |
||
853 | * @param object $object |
||
854 | * |
||
855 | * @return string |
||
856 | */ |
||
857 | public function getUniqueHash($object) |
||
861 | |||
862 | /** |
||
863 | * Returns true if given location is a tree root. |
||
864 | * |
||
865 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation |
||
866 | * |
||
867 | * @return bool |
||
868 | */ |
||
869 | private function isRootLocation(SPILocation $spiLocation): bool |
||
873 | } |
||
874 |
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.