Complex classes like ObjectStateService 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 ObjectStateService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class ObjectStateService implements ObjectStateServiceInterface |
||
| 40 | { |
||
| 41 | /** @var \eZ\Publish\API\Repository\Repository */ |
||
| 42 | protected $repository; |
||
| 43 | |||
| 44 | /** @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler */ |
||
| 45 | protected $objectStateHandler; |
||
| 46 | |||
| 47 | /** @var array */ |
||
| 48 | protected $settings; |
||
| 49 | |||
| 50 | /** @var \eZ\Publish\API\Repository\PermissionResolver */ |
||
| 51 | private $permissionResolver; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 55 | * |
||
| 56 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 57 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler |
||
| 58 | * @param array $settings |
||
| 59 | */ |
||
| 60 | public function __construct( |
||
| 61 | RepositoryInterface $repository, |
||
| 62 | Handler $objectStateHandler, |
||
| 63 | PermissionResolver $permissionResolver, |
||
| 64 | array $settings = [] |
||
| 65 | ) { |
||
| 66 | $this->repository = $repository; |
||
| 67 | $this->objectStateHandler = $objectStateHandler; |
||
| 68 | $this->permissionResolver = $permissionResolver; |
||
| 69 | // Union makes sure default settings are ignored if provided in argument |
||
| 70 | $this->settings = $settings + [ |
||
| 71 | //'defaultSetting' => array(), |
||
| 72 | ]; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates a new object state group. |
||
| 77 | * |
||
| 78 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state group |
||
| 79 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists |
||
| 80 | * |
||
| 81 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreateStruct |
||
| 82 | * |
||
| 83 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
| 84 | */ |
||
| 85 | public function createObjectStateGroup(ObjectStateGroupCreateStruct $objectStateGroupCreateStruct): APIObjectStateGroup |
||
| 86 | { |
||
| 87 | if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateGroupCreateStruct)) { |
||
| 88 | throw new UnauthorizedException('state', 'administrate'); |
||
| 89 | } |
||
| 90 | |||
| 91 | $inputStruct = $this->buildCreateInputStruct( |
||
| 92 | $objectStateGroupCreateStruct->identifier, |
||
| 93 | $objectStateGroupCreateStruct->defaultLanguageCode, |
||
| 94 | $objectStateGroupCreateStruct->names, |
||
| 95 | $objectStateGroupCreateStruct->descriptions |
||
| 96 | ); |
||
| 97 | |||
| 98 | try { |
||
| 99 | $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier); |
||
| 100 | throw new InvalidArgumentException( |
||
| 101 | 'objectStateGroupCreateStruct', |
||
| 102 | 'Object state group with provided identifier already exists' |
||
| 103 | ); |
||
| 104 | } catch (APINotFoundException $e) { |
||
| 105 | // Do nothing |
||
| 106 | } |
||
| 107 | |||
| 108 | $this->repository->beginTransaction(); |
||
| 109 | try { |
||
| 110 | $spiObjectStateGroup = $this->objectStateHandler->createGroup($inputStruct); |
||
| 111 | $this->repository->commit(); |
||
| 112 | } catch (Exception $e) { |
||
| 113 | $this->repository->rollback(); |
||
| 114 | throw $e; |
||
| 115 | } |
||
| 116 | |||
| 117 | return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | public function loadObjectStateGroup(int $objectStateGroupId, array $prioritizedLanguages = []): APIObjectStateGroup |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function loadObjectStateGroups(int $offset = 0, int $limit = -1, array $prioritizedLanguages = []): iterable |
||
| 147 | |||
| 148 | /** |
||
| 149 | * This method returns the ordered list of object states of a group. |
||
| 150 | * |
||
| 151 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 152 | * @param string[] $prioritizedLanguages |
||
| 153 | * |
||
| 154 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[] |
||
| 155 | */ |
||
| 156 | public function loadObjectStates( |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Updates an object state group. |
||
| 176 | * |
||
| 177 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state group |
||
| 178 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists |
||
| 179 | * |
||
| 180 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 181 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct |
||
| 182 | * |
||
| 183 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
| 184 | */ |
||
| 185 | public function updateObjectStateGroup(APIObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct): APIObjectStateGroup |
||
| 186 | { |
||
| 187 | if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateGroup)) { |
||
| 188 | throw new UnauthorizedException('state', 'administrate'); |
||
| 189 | } |
||
| 190 | |||
| 191 | $loadedObjectStateGroup = $this->loadObjectStateGroup($objectStateGroup->id); |
||
| 192 | |||
| 193 | $inputStruct = $this->buildObjectStateGroupUpdateInputStruct( |
||
| 194 | $loadedObjectStateGroup, |
||
| 195 | $objectStateGroupUpdateStruct->identifier, |
||
| 196 | $objectStateGroupUpdateStruct->defaultLanguageCode, |
||
| 197 | $objectStateGroupUpdateStruct->names, |
||
| 198 | $objectStateGroupUpdateStruct->descriptions |
||
| 199 | ); |
||
| 200 | |||
| 201 | if ($objectStateGroupUpdateStruct->identifier !== null) { |
||
| 202 | try { |
||
| 203 | $existingObjectStateGroup = $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier); |
||
| 204 | if ($existingObjectStateGroup->id != $loadedObjectStateGroup->id) { |
||
| 205 | throw new InvalidArgumentException( |
||
| 206 | 'objectStateGroupUpdateStruct', |
||
| 207 | 'Object state group with provided identifier already exists' |
||
| 208 | ); |
||
| 209 | } |
||
| 210 | } catch (APINotFoundException $e) { |
||
| 211 | // Do nothing |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | $this->repository->beginTransaction(); |
||
| 216 | try { |
||
| 217 | $spiObjectStateGroup = $this->objectStateHandler->updateGroup( |
||
| 218 | $loadedObjectStateGroup->id, |
||
| 219 | $inputStruct |
||
| 220 | ); |
||
| 221 | $this->repository->commit(); |
||
| 222 | } catch (Exception $e) { |
||
| 223 | $this->repository->rollback(); |
||
| 224 | throw $e; |
||
| 225 | } |
||
| 226 | |||
| 227 | return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Deletes a object state group including all states and links to content. |
||
| 232 | * |
||
| 233 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state group |
||
| 234 | * |
||
| 235 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 236 | */ |
||
| 237 | public function deleteObjectStateGroup(APIObjectStateGroup $objectStateGroup): void |
||
| 238 | { |
||
| 239 | if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateGroup)) { |
||
| 240 | throw new UnauthorizedException('state', 'administrate'); |
||
| 241 | } |
||
| 242 | |||
| 243 | $loadedObjectStateGroup = $this->loadObjectStateGroup($objectStateGroup->id); |
||
| 244 | |||
| 245 | $this->repository->beginTransaction(); |
||
| 246 | try { |
||
| 247 | $this->objectStateHandler->deleteGroup($loadedObjectStateGroup->id); |
||
| 248 | $this->repository->commit(); |
||
| 249 | } catch (Exception $e) { |
||
| 250 | $this->repository->rollback(); |
||
| 251 | throw $e; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Creates a new object state in the given group. |
||
| 257 | * |
||
| 258 | * Note: in current kernel: If it is the first state all content objects will |
||
| 259 | * set to this state. |
||
| 260 | * |
||
| 261 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state |
||
| 262 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group |
||
| 263 | * |
||
| 264 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 265 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct $objectStateCreateStruct |
||
| 266 | * |
||
| 267 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 268 | */ |
||
| 269 | public function createObjectState(APIObjectStateGroup $objectStateGroup, ObjectStateCreateStruct $objectStateCreateStruct): APIObjectState |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function loadObjectState(int $stateId, array $prioritizedLanguages = []): APIObjectState |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Updates an object state. |
||
| 328 | * |
||
| 329 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state |
||
| 330 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group |
||
| 331 | * |
||
| 332 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 333 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdateStruct |
||
| 334 | * |
||
| 335 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 336 | */ |
||
| 337 | public function updateObjectState(APIObjectState $objectState, ObjectStateUpdateStruct $objectStateUpdateStruct): APIObjectState |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Changes the priority of the state. |
||
| 388 | * |
||
| 389 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change priority on an object state |
||
| 390 | * |
||
| 391 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 392 | * @param int $priority |
||
| 393 | */ |
||
| 394 | public function setPriorityOfObjectState(APIObjectState $objectState, int $priority): void |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Deletes a object state. The state of the content objects is reset to the |
||
| 417 | * first object state in the group. |
||
| 418 | * |
||
| 419 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state |
||
| 420 | * |
||
| 421 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 422 | */ |
||
| 423 | public function deleteObjectState(APIObjectState $objectState): void |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Sets the object-state of a state group to $state for the given content. |
||
| 443 | * |
||
| 444 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state does not belong to the given group |
||
| 445 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change the object state |
||
| 446 | * |
||
| 447 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 448 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 449 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 450 | */ |
||
| 451 | public function setContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup, APIObjectState $objectState): void |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Gets the object-state of object identified by $contentId. |
||
| 479 | * |
||
| 480 | * The $state is the id of the state within one group. |
||
| 481 | * |
||
| 482 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 483 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 484 | * |
||
| 485 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 486 | */ |
||
| 487 | public function getContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup): APIObjectState |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Returns the number of objects which are in this state. |
||
| 499 | * |
||
| 500 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 501 | * |
||
| 502 | * @return int |
||
| 503 | */ |
||
| 504 | public function getContentCount(APIObjectState $objectState): int |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Instantiates a new Object State Group Create Struct and sets $identified in it. |
||
| 513 | * |
||
| 514 | * @param string $identifier |
||
| 515 | * |
||
| 516 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct |
||
| 517 | */ |
||
| 518 | public function newObjectStateGroupCreateStruct(string $identifier): ObjectStateGroupCreateStruct |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Instantiates a new Object State Group Update Struct. |
||
| 528 | * |
||
| 529 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct |
||
| 530 | */ |
||
| 531 | public function newObjectStateGroupUpdateStruct(): ObjectStateGroupUpdateStruct |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Instantiates a new Object State Create Struct and sets $identifier in it. |
||
| 538 | * |
||
| 539 | * @param string $identifier |
||
| 540 | * |
||
| 541 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct |
||
| 542 | */ |
||
| 543 | public function newObjectStateCreateStruct(string $identifier): ObjectStateCreateStruct |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Instantiates a new Object State Update Struct. |
||
| 553 | * |
||
| 554 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct |
||
| 555 | */ |
||
| 556 | public function newObjectStateUpdateStruct(): ObjectStateUpdateStruct |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Converts the object state SPI value object to API value object. |
||
| 563 | * |
||
| 564 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $spiObjectState |
||
| 565 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 566 | * @param string[] $prioritizedLanguages |
||
| 567 | * |
||
| 568 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 569 | */ |
||
| 570 | protected function buildDomainObjectStateObject( |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Converts the object state group SPI value object to API value object. |
||
| 594 | * |
||
| 595 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $spiObjectStateGroup |
||
| 596 | * @param array $prioritizedLanguages |
||
| 597 | * |
||
| 598 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
| 599 | */ |
||
| 600 | protected function buildDomainObjectStateGroupObject( |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Validates input for creating object states/groups and builds the InputStruct object. |
||
| 619 | * |
||
| 620 | * @param string $identifier |
||
| 621 | * @param string $defaultLanguageCode |
||
| 622 | * @param string[] $names |
||
| 623 | * @param string[]|null $descriptions |
||
| 624 | * |
||
| 625 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
| 626 | */ |
||
| 627 | protected function buildCreateInputStruct( |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Validates input for updating object states and builds the InputStruct object. |
||
| 682 | * |
||
| 683 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 684 | * @param string|null $identifier |
||
| 685 | * @param string|null $defaultLanguageCode |
||
| 686 | * @param string[]|null $names |
||
| 687 | * @param string[]|null $descriptions |
||
| 688 | * |
||
| 689 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
| 690 | */ |
||
| 691 | protected function buildObjectStateUpdateInputStruct( |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Validates input for updating object state groups and builds the InputStruct object. |
||
| 751 | * |
||
| 752 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 753 | * @param string|null $identifier |
||
| 754 | * @param string|null $defaultLanguageCode |
||
| 755 | * @param string[]|null $names |
||
| 756 | * @param string[]|null $descriptions |
||
| 757 | * |
||
| 758 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
| 759 | */ |
||
| 760 | protected function buildObjectStateGroupUpdateInputStruct( |
||
| 817 | } |
||
| 818 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.