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 |
||
| 42 | class DomainMapper |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var \eZ\Publish\SPI\Persistence\Content\Handler |
||
| 46 | */ |
||
| 47 | protected $contentHandler; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \eZ\Publish\SPI\Persistence\Content\Location\Handler |
||
| 51 | */ |
||
| 52 | protected $locationHandler; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler |
||
| 56 | */ |
||
| 57 | protected $contentTypeHandler; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
| 61 | */ |
||
| 62 | protected $contentLanguageHandler; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var FieldTypeRegistry |
||
| 66 | */ |
||
| 67 | protected $fieldTypeRegistry; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Setups service with reference to repository. |
||
| 71 | * |
||
| 72 | * @param \eZ\Publish\SPI\Persistence\Content\Handler $contentHandler |
||
| 73 | * @param \eZ\Publish\SPI\Persistence\Content\Location\Handler $locationHandler |
||
| 74 | * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler |
||
| 75 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $contentLanguageHandler |
||
| 76 | * @param FieldTypeRegistry $fieldTypeRegistry |
||
| 77 | */ |
||
| 78 | public function __construct( |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Builds a Content domain object from value object returned from persistence. |
||
| 94 | * |
||
| 95 | * @param \eZ\Publish\SPI\Persistence\Content $spiContent |
||
| 96 | * @param ContentType|SPIType $contentType |
||
| 97 | * @param array|null $fieldLanguages Language codes to filter fields on |
||
| 98 | * @param string|null $fieldAlwaysAvailableLanguage Language code fallback if a given field is not found in $fieldLanguages |
||
| 99 | * |
||
| 100 | * @return \eZ\Publish\Core\Repository\Values\Content\Content |
||
| 101 | */ |
||
| 102 | public function buildContentDomainObject(SPIContent $spiContent, $contentType = null, array $fieldLanguages = null, $fieldAlwaysAvailableLanguage = null) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns an array of domain fields created from given array of SPI fields. |
||
| 133 | * |
||
| 134 | * @throws InvalidArgumentType On invalid $contentType |
||
| 135 | * |
||
| 136 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields |
||
| 137 | * @param ContentType|SPIType $contentType |
||
| 138 | * @param array $languages A language priority, filters returned fields and is used as prioritized language code on |
||
| 139 | * returned value object. If not given all languages are returned. |
||
| 140 | * @param string|null $alwaysAvailableLanguage Language code fallback if a given field is not found in $languages |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function buildDomainFields( |
||
| 145 | array $spiFields, |
||
| 146 | $contentType, |
||
| 147 | array $languages = null, |
||
| 148 | $alwaysAvailableLanguage = null |
||
| 149 | ) { |
||
| 150 | if (!$contentType instanceof SPIType && !$contentType instanceof ContentType) { |
||
| 151 | throw new InvalidArgumentType('$contentType', 'SPI ContentType | API ContentType'); |
||
| 152 | } |
||
| 153 | |||
| 154 | $fieldIdentifierMap = array(); |
||
| 155 | foreach ($contentType->fieldDefinitions as $fieldDefinitions) { |
||
| 156 | $fieldIdentifierMap[$fieldDefinitions->id] = $fieldDefinitions->identifier; |
||
| 157 | } |
||
| 158 | |||
| 159 | $fieldInFilterLanguagesMap = array(); |
||
| 160 | if (!empty($languages) && $alwaysAvailableLanguage !== null) { |
||
| 161 | foreach ($spiFields as $spiField) { |
||
| 162 | if (in_array($spiField->languageCode, $languages)) { |
||
| 163 | $fieldInFilterLanguagesMap[$spiField->fieldDefinitionId] = true; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | $fields = array(); |
||
| 169 | foreach ($spiFields as $spiField) { |
||
| 170 | // We ignore fields in content not part of the content type |
||
| 171 | if (!isset($fieldIdentifierMap[$spiField->fieldDefinitionId])) { |
||
| 172 | continue; |
||
| 173 | } |
||
| 174 | |||
| 175 | if (!empty($languages) && !in_array($spiField->languageCode, $languages)) { |
||
| 176 | // If filtering is enabled we ignore fields in other languages then $fieldLanguages, if: |
||
| 177 | if ($alwaysAvailableLanguage === null) { |
||
| 178 | // Ignore field if we don't have $alwaysAvailableLanguageCode fallback |
||
| 179 | continue; |
||
| 180 | } elseif (!empty($fieldInFilterLanguagesMap[$spiField->fieldDefinitionId])) { |
||
| 181 | // Ignore field if it exists in one of the filtered languages |
||
| 182 | continue; |
||
| 183 | } elseif ($spiField->languageCode !== $alwaysAvailableLanguage) { |
||
| 184 | // Also ignore if field is not in $alwaysAvailableLanguageCode |
||
| 185 | continue; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | $fields[] = new Field( |
||
| 190 | array( |
||
| 191 | 'id' => $spiField->id, |
||
| 192 | 'value' => $this->fieldTypeRegistry->getFieldType($spiField->type) |
||
| 193 | ->fromPersistenceValue($spiField->value), |
||
| 194 | 'languageCode' => $spiField->languageCode, |
||
| 195 | 'fieldDefIdentifier' => $fieldIdentifierMap[$spiField->fieldDefinitionId], |
||
| 196 | 'fieldTypeIdentifier' => $spiField->type, |
||
| 197 | ) |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $fields; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Builds a VersionInfo domain object from value object returned from persistence. |
||
| 206 | * |
||
| 207 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $spiVersionInfo |
||
| 208 | * @param array $prioritizedLanguages |
||
| 209 | * |
||
| 210 | * @return \eZ\Publish\Core\Repository\Values\Content\VersionInfo |
||
| 211 | */ |
||
| 212 | public function buildVersionInfoDomainObject(SPIVersionInfo $spiVersionInfo, array $prioritizedLanguages = []) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Builds a ContentInfo domain object from value object returned from persistence. |
||
| 257 | * |
||
| 258 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $spiContentInfo |
||
| 259 | * |
||
| 260 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 261 | */ |
||
| 262 | public function buildContentInfoDomainObject(SPIContentInfo $spiContentInfo) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Builds API Relation object from provided SPI Relation object. |
||
| 305 | * |
||
| 306 | * @param \eZ\Publish\SPI\Persistence\Content\Relation $spiRelation |
||
| 307 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $sourceContentInfo |
||
| 308 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContentInfo |
||
| 309 | * |
||
| 310 | * @return \eZ\Publish\API\Repository\Values\Content\Relation |
||
| 311 | */ |
||
| 312 | public function buildRelationDomainObject( |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Builds domain location object from provided persistence location. |
||
| 343 | * |
||
| 344 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation |
||
| 345 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $spiContentInfo pre-loaded Content Info |
||
| 346 | * |
||
| 347 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
| 348 | * |
||
| 349 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException In case if the given Content does not belong to the given Location |
||
| 350 | */ |
||
| 351 | public function buildLocationDomainObject(SPILocation $spiLocation, SPIContentInfo $spiContentInfo = null) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Creates an array of SPI location create structs from given array of API location create structs. |
||
| 411 | * |
||
| 412 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 413 | * |
||
| 414 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
| 415 | * @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation |
||
| 416 | * @param mixed $mainLocation |
||
| 417 | * @param mixed $contentId |
||
| 418 | * @param mixed $contentVersionNo |
||
| 419 | * |
||
| 420 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct |
||
| 421 | */ |
||
| 422 | public function buildSPILocationCreateStruct( |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Checks if given $sortField value is one of the defined sort field constants. |
||
| 488 | * |
||
| 489 | * @param mixed $sortField |
||
| 490 | * |
||
| 491 | * @return bool |
||
| 492 | */ |
||
| 493 | public function isValidLocationSortField($sortField) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Checks if given $sortOrder value is one of the defined sort order constants. |
||
| 516 | * |
||
| 517 | * @param mixed $sortOrder |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | public function isValidLocationSortOrder($sortOrder) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Validates given translated list $list, which should be an array of strings with language codes as keys. |
||
| 534 | * |
||
| 535 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 536 | * |
||
| 537 | * @param mixed $list |
||
| 538 | * @param string $argumentName |
||
| 539 | */ |
||
| 540 | public function validateTranslatedList($list, $argumentName) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Returns \DateTime object from given $timestamp in environment timezone. |
||
| 557 | * |
||
| 558 | * This method is needed because constructing \DateTime with $timestamp will |
||
| 559 | * return the object in UTC timezone. |
||
| 560 | * |
||
| 561 | * @param int $timestamp |
||
| 562 | * |
||
| 563 | * @return \DateTime |
||
| 564 | */ |
||
| 565 | public function getDateTime($timestamp) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Creates unique hash string for given $object. |
||
| 575 | * |
||
| 576 | * Used for remoteId. |
||
| 577 | * |
||
| 578 | * @param object $object |
||
| 579 | * |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | public function getUniqueHash($object) |
||
| 586 | } |
||
| 587 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.