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(array $spiFields, $contentType, array $languages = null, $alwaysAvailableLanguage = null) |
||
| 145 | { |
||
| 146 | if (!$contentType instanceof SPIType && !$contentType instanceof ContentType) { |
||
| 147 | throw new InvalidArgumentType('$contentType', 'SPI ContentType | API ContentType'); |
||
| 148 | } |
||
| 149 | |||
| 150 | $fieldIdentifierMap = array(); |
||
| 151 | foreach ($contentType->fieldDefinitions as $fieldDefinitions) { |
||
| 152 | $fieldIdentifierMap[$fieldDefinitions->id] = $fieldDefinitions->identifier; |
||
| 153 | } |
||
| 154 | |||
| 155 | $fieldInFilterLanguagesMap = array(); |
||
| 156 | if ($languages !== null && $alwaysAvailableLanguage !== null) { |
||
| 157 | foreach ($spiFields as $spiField) { |
||
| 158 | if (in_array($spiField->languageCode, $languages)) { |
||
| 159 | $fieldInFilterLanguagesMap[$spiField->fieldDefinitionId] = true; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | $fields = array(); |
||
| 165 | foreach ($spiFields as $spiField) { |
||
| 166 | // We ignore fields in content not part of the content type |
||
| 167 | if (!isset($fieldIdentifierMap[$spiField->fieldDefinitionId])) { |
||
| 168 | continue; |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($languages !== null && !in_array($spiField->languageCode, $languages)) { |
||
| 172 | // If filtering is enabled we ignore fields in other languages then $fieldLanguages, if: |
||
| 173 | if ($alwaysAvailableLanguage === null) { |
||
| 174 | // Ignore field if we don't have $alwaysAvailableLanguageCode fallback |
||
| 175 | continue; |
||
| 176 | } elseif (!empty($fieldInFilterLanguagesMap[$spiField->fieldDefinitionId])) { |
||
| 177 | // Ignore field if it exists in one of the filtered languages |
||
| 178 | continue; |
||
| 179 | } elseif ($spiField->languageCode !== $alwaysAvailableLanguage) { |
||
| 180 | // Also ignore if field is not in $alwaysAvailableLanguageCode |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | $fields[] = new Field( |
||
| 186 | array( |
||
| 187 | 'id' => $spiField->id, |
||
| 188 | 'value' => $this->fieldTypeRegistry->getFieldType($spiField->type) |
||
| 189 | ->fromPersistenceValue($spiField->value), |
||
| 190 | 'languageCode' => $spiField->languageCode, |
||
| 191 | 'fieldDefIdentifier' => $fieldIdentifierMap[$spiField->fieldDefinitionId], |
||
| 192 | 'fieldTypeIdentifier' => $spiField->type, |
||
| 193 | ) |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | |||
| 197 | return $fields; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Builds a VersionInfo domain object from value object returned from persistence. |
||
| 202 | * |
||
| 203 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $spiVersionInfo |
||
| 204 | * @param array $prioritizedLanguages |
||
| 205 | * |
||
| 206 | * @return \eZ\Publish\Core\Repository\Values\Content\VersionInfo |
||
| 207 | */ |
||
| 208 | public function buildVersionInfoDomainObject(SPIVersionInfo $spiVersionInfo, array $prioritizedLanguages = []) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Builds a ContentInfo domain object from value object returned from persistence. |
||
| 253 | * |
||
| 254 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $spiContentInfo |
||
| 255 | * |
||
| 256 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 257 | */ |
||
| 258 | public function buildContentInfoDomainObject(SPIContentInfo $spiContentInfo) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Builds API Relation object from provided SPI Relation object. |
||
| 285 | * |
||
| 286 | * @param \eZ\Publish\SPI\Persistence\Content\Relation $spiRelation |
||
| 287 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $sourceContentInfo |
||
| 288 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContentInfo |
||
| 289 | * |
||
| 290 | * @return \eZ\Publish\API\Repository\Values\Content\Relation |
||
| 291 | */ |
||
| 292 | public function buildRelationDomainObject( |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Builds domain location object from provided persistence location. |
||
| 323 | * |
||
| 324 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation |
||
| 325 | * |
||
| 326 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
| 327 | */ |
||
| 328 | public function buildLocationDomainObject(SPILocation $spiLocation) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Creates an array of SPI location create structs from given array of API location create structs. |
||
| 375 | * |
||
| 376 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 377 | * |
||
| 378 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
| 379 | * @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation |
||
| 380 | * @param mixed $mainLocation |
||
| 381 | * @param mixed $contentId |
||
| 382 | * @param mixed $contentVersionNo |
||
| 383 | * |
||
| 384 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct |
||
| 385 | */ |
||
| 386 | public function buildSPILocationCreateStruct( |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Checks if given $sortField value is one of the defined sort field constants. |
||
| 452 | * |
||
| 453 | * @param mixed $sortField |
||
| 454 | * |
||
| 455 | * @return bool |
||
| 456 | */ |
||
| 457 | public function isValidLocationSortField($sortField) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Checks if given $sortOrder value is one of the defined sort order constants. |
||
| 480 | * |
||
| 481 | * @param mixed $sortOrder |
||
| 482 | * |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | public function isValidLocationSortOrder($sortOrder) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Validates given translated list $list, which should be an array of strings with language codes as keys. |
||
| 498 | * |
||
| 499 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 500 | * |
||
| 501 | * @param mixed $list |
||
| 502 | * @param string $argumentName |
||
| 503 | */ |
||
| 504 | public function validateTranslatedList($list, $argumentName) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns \DateTime object from given $timestamp in environment timezone. |
||
| 521 | * |
||
| 522 | * This method is needed because constructing \DateTime with $timestamp will |
||
| 523 | * return the object in UTC timezone. |
||
| 524 | * |
||
| 525 | * @param int $timestamp |
||
| 526 | * |
||
| 527 | * @return \DateTime |
||
| 528 | */ |
||
| 529 | public function getDateTime($timestamp) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Creates unique hash string for given $object. |
||
| 539 | * |
||
| 540 | * Used for remoteId. |
||
| 541 | * |
||
| 542 | * @param object $object |
||
| 543 | * |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | public function getUniqueHash($object) |
||
| 550 | } |
||
| 551 |
This class constant has been deprecated.