Complex classes like AbstractObject 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 AbstractObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 65 | abstract class AbstractObject implements ObjectInterface |
||
| 66 | { |
||
| 67 | /** |
||
| 68 | * Use traits |
||
| 69 | */ |
||
| 70 | use SystemPropertiesTrait, MetaPropertiesTrait, DomainPropertiesTrait, RelationsTrait, |
||
| 71 | ProcessingInstructionsTrait, PayloadTrait, IterableTrait, StatesTrait; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Repository locator |
||
| 75 | * |
||
| 76 | * @var RepositoryLocatorInterface |
||
| 77 | */ |
||
| 78 | protected $locator; |
||
| 79 | /** |
||
| 80 | * Latest revision |
||
| 81 | * |
||
| 82 | * @var Revision |
||
| 83 | */ |
||
| 84 | protected $latestRevision; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Object constructor |
||
| 88 | * |
||
| 89 | * @param RepositoryLocatorInterface $locator Object repository locator |
||
| 90 | * @param string $payload Object payload |
||
| 91 | * @param array $propertyData Property data |
||
| 92 | */ |
||
| 93 | 46 | public function __construct(RepositoryLocatorInterface $locator, $payload = '', array $propertyData = []) |
|
| 94 | { |
||
| 95 | // If the domain property collection class is invalid |
||
| 96 | 46 | if (!$this->domainPropertyCClass |
|
| 97 | 46 | || !class_exists($this->domainPropertyCClass) |
|
| 98 | 46 | || !(new \ReflectionClass($this->domainPropertyCClass))->isSubclassOf(AbstractDomainProperties::class) |
|
| 99 | ) { |
||
| 100 | 1 | throw new PropertyInvalidArgumentException( |
|
| 101 | sprintf( |
||
| 102 | 1 | 'Invalid domain property collection class "%s"', |
|
| 103 | 1 | $this->domainPropertyCClass |
|
| 104 | ), |
||
| 105 | 1 | PropertyInvalidArgumentException::INVALID_DOMAIN_PROPERTY_COLLECTION_CLASS |
|
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Save the original object locator |
||
| 110 | 45 | $this->locator = $locator; |
|
| 111 | |||
| 112 | // Load the current revision data |
||
| 113 | 45 | $this->loadRevisionData($payload, $propertyData); |
|
| 114 | |||
| 115 | // Determine the latest revision number (considering a possible draft) |
||
| 116 | 44 | $this->latestRevision = $this->hasDraft() |
|
| 117 | ? Kernel::create(Revision::class, [$this->getRevision()->getRevision() + 1, true]) |
||
| 118 | 44 | : $this->getRevision(); |
|
| 119 | |||
| 120 | // Update the object locator |
||
| 121 | 44 | $this->updateLocator(); |
|
| 122 | 44 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Load object revision data |
||
| 126 | * |
||
| 127 | * @param string $payload Object payload |
||
| 128 | * @param array $propertyData Property data |
||
| 129 | */ |
||
| 130 | 45 | protected function loadRevisionData($payload = '', array $propertyData = []) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Return whether this object already has a draft revision |
||
| 183 | * |
||
| 184 | * @return bool Whether this object has a draft revision |
||
| 185 | */ |
||
| 186 | 44 | protected function hasDraft() |
|
| 187 | { |
||
| 188 | // Create the object draft resource locator |
||
| 189 | 44 | $draftLocator = $this->locator->setRevision(Revision::current(true)); |
|
| 190 | |||
| 191 | // Use the object manager to look for a draft resource |
||
| 192 | /** @var ManagerInterface $objectManager */ |
||
| 193 | 44 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
|
| 194 | 44 | return $objectManager->objectResourceExists($draftLocator); |
|
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Update the object locator |
||
| 199 | */ |
||
| 200 | 44 | protected function updateLocator() |
|
| 201 | { |
||
| 202 | 44 | $revision = $this->getRevision(); |
|
| 203 | 44 | $this->locator = $this->locator->setRevision( |
|
| 204 | 44 | !$revision->isDraft() && ($this->getCurrentRevision()->getRevision() == $revision->getRevision()) |
|
| 205 | 34 | ? Revision::current($revision->isDraft()) |
|
| 206 | 44 | : $revision |
|
| 207 | 44 | )->setHidden($this->isDeleted()); |
|
| 208 | 44 | } |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Return this object's current revision |
||
| 212 | * |
||
| 213 | * @return Revision Current revision |
||
| 214 | */ |
||
| 215 | 41 | protected function getCurrentRevision() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Use a specific object revision |
||
| 225 | * |
||
| 226 | * @param Revision $revision Revision to be used |
||
| 227 | * @return ObjectInterface Object |
||
| 228 | * @throws OutOfBoundsException If a revision beyond the latest one is requested |
||
| 229 | */ |
||
| 230 | 40 | public function useRevision(Revision $revision) |
|
| 231 | { |
||
| 232 | // If a revision beyond the latest one is requested |
||
| 233 | 40 | if (!$revision->isCurrent() && ($revision->getRevision() > $this->latestRevision->getRevision())) { |
|
| 234 | 1 | throw new OutOfBoundsException( |
|
| 235 | 1 | sprintf('Invalid object revision "%s"', $revision->getRevision()), |
|
| 236 | 1 | OutOfBoundsException::INVALID_OBJECT_REVISION |
|
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Determine whether the current revision was requested |
||
| 241 | 40 | $currentRevision = $this->getCurrentRevision(); |
|
| 242 | 40 | $isCurrentRevision = $revision->isCurrent() || $currentRevision->equals($revision); |
|
| 243 | 40 | if ($isCurrentRevision) { |
|
| 244 | 40 | $revision = $currentRevision; |
|
| 245 | } |
||
| 246 | |||
| 247 | // If the requested revision is not already used |
||
| 248 | 40 | if (!$this->getRevision()->equals($revision)) { |
|
| 249 | |||
| 250 | /** @var ManagerInterface $objectManager */ |
||
| 251 | 1 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
|
| 252 | /** @var Revision $newRevision */ |
||
| 253 | 1 | $newRevision = $isCurrentRevision ? Revision::current() : $revision; |
|
| 254 | /** @var RepositoryLocator $newRevisionLocator */ |
||
| 255 | 1 | $newRevisionLocator = $this->locator->setRevision($newRevision); |
|
| 256 | |||
| 257 | // Instantiate the requested revision resource |
||
| 258 | 1 | $revisionResource = $objectManager->loadObjectResource($newRevisionLocator, SelectorInterface::ALL); |
|
| 259 | |||
| 260 | // Load the revision resource data |
||
| 261 | 1 | $this->loadRevisionData($revisionResource->getPayload(), $revisionResource->getPropertyData()); |
|
| 262 | |||
| 263 | // Update the object locator |
||
| 264 | 1 | $this->updateLocator(); |
|
| 265 | } |
||
| 266 | |||
| 267 | 40 | return $this; |
|
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return the object repository locator |
||
| 272 | * |
||
| 273 | * @return RepositoryLocatorInterface Object repository locator |
||
| 274 | */ |
||
| 275 | 46 | public function getRepositoryLocator() |
|
| 276 | { |
||
| 277 | 46 | return $this->locator; |
|
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return the object property data |
||
| 282 | * |
||
| 283 | * @param bool $serialize Serialize property objects |
||
| 284 | * @return array Object property data |
||
| 285 | */ |
||
| 286 | 10 | public function getPropertyData($serialize = true) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Return the absolute object URL |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | 5 | public function getAbsoluteUrl() |
|
| 307 | { |
||
| 308 | 5 | return getenv('APPARAT_BASE_URL').rtrim($this->locator->getRepository()->getUrl(), '/').strval($this->locator); |
|
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Return the canonical object URL |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | 1 | public function getCanonicalUrl() |
|
| 317 | { |
||
| 318 | 1 | $canonicalLocator = $this->locator->setRevision(Revision::current()); |
|
| 319 | 1 | $canonicalUrl = ltrim($this->locator->getRepository()->getUrl(), '/').strval($canonicalLocator); |
|
| 320 | 1 | return getenv('APPARAT_BASE_URL').$canonicalUrl; |
|
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Persist the current object revision |
||
| 325 | * |
||
| 326 | * @return ObjectInterface Object |
||
| 327 | */ |
||
| 328 | 5 | public function persist() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Post persistence hook |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | 3 | protected function postPersist() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Publish the current object revision |
||
| 367 | * |
||
| 368 | * @return ObjectInterface Object |
||
| 369 | */ |
||
| 370 | 2 | public function publish() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Delete the object and all its revisions |
||
| 384 | * |
||
| 385 | * @return ObjectInterface Object |
||
| 386 | */ |
||
| 387 | 3 | public function delete() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Undelete the object and all its revisions |
||
| 400 | * |
||
| 401 | * @return ObjectInterface Object |
||
| 402 | */ |
||
| 403 | 2 | public function undelete() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Convert this object revision into a draft |
||
| 416 | */ |
||
| 417 | 5 | protected function convertToDraft() |
|
| 433 | } |
||
| 434 |