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 |
||
37 | class ObjectStateService implements ObjectStateServiceInterface |
||
38 | { |
||
39 | /** @var \eZ\Publish\API\Repository\Repository */ |
||
40 | protected $repository; |
||
41 | |||
42 | /** @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler */ |
||
43 | protected $objectStateHandler; |
||
44 | |||
45 | /** @var array */ |
||
46 | protected $settings; |
||
47 | |||
48 | /** @var \eZ\Publish\API\Repository\PermissionResolver */ |
||
49 | private $permissionResolver; |
||
50 | |||
51 | /** |
||
52 | * Setups service with reference to repository object that created it & corresponding handler. |
||
53 | * |
||
54 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
55 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler |
||
56 | * @param array $settings |
||
57 | */ |
||
58 | public function __construct( |
||
59 | RepositoryInterface $repository, |
||
60 | Handler $objectStateHandler, |
||
61 | PermissionResolver $permissionResolver, |
||
62 | array $settings = [] |
||
63 | ) { |
||
64 | $this->repository = $repository; |
||
65 | $this->objectStateHandler = $objectStateHandler; |
||
66 | $this->permissionResolver = $permissionResolver; |
||
67 | // Union makes sure default settings are ignored if provided in argument |
||
68 | $this->settings = $settings + [ |
||
69 | //'defaultSetting' => array(), |
||
70 | ]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Creates a new object state group. |
||
75 | * |
||
76 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state group |
||
77 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists |
||
78 | * |
||
79 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreateStruct |
||
80 | * |
||
81 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
82 | */ |
||
83 | public function createObjectStateGroup(ObjectStateGroupCreateStruct $objectStateGroupCreateStruct): APIObjectStateGroup |
||
84 | { |
||
85 | if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateGroupCreateStruct)) { |
||
86 | throw new UnauthorizedException('state', 'administrate'); |
||
87 | } |
||
88 | |||
89 | $inputStruct = $this->buildCreateInputStruct( |
||
90 | $objectStateGroupCreateStruct->identifier, |
||
91 | $objectStateGroupCreateStruct->defaultLanguageCode, |
||
92 | $objectStateGroupCreateStruct->names, |
||
93 | $objectStateGroupCreateStruct->descriptions |
||
94 | ); |
||
95 | |||
96 | try { |
||
97 | $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier); |
||
98 | throw new InvalidArgumentException( |
||
99 | 'objectStateGroupCreateStruct', |
||
100 | 'An Object state group with the provided identifier already exists' |
||
101 | ); |
||
102 | } catch (APINotFoundException $e) { |
||
103 | // Do nothing |
||
104 | } |
||
105 | |||
106 | $this->repository->beginTransaction(); |
||
107 | try { |
||
108 | $spiObjectStateGroup = $this->objectStateHandler->createGroup($inputStruct); |
||
109 | $this->repository->commit(); |
||
110 | } catch (Exception $e) { |
||
111 | $this->repository->rollback(); |
||
112 | throw $e; |
||
113 | } |
||
114 | |||
115 | return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function loadObjectStateGroup(int $objectStateGroupId, array $prioritizedLanguages = []): APIObjectStateGroup |
||
122 | { |
||
123 | $spiObjectStateGroup = $this->objectStateHandler->loadGroup($objectStateGroupId); |
||
124 | |||
125 | return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup, $prioritizedLanguages); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function loadObjectStateGroups(int $offset = 0, int $limit = -1, array $prioritizedLanguages = []): iterable |
||
132 | { |
||
133 | $spiObjectStateGroups = $this->objectStateHandler->loadAllGroups($offset, $limit); |
||
134 | |||
135 | $objectStateGroups = []; |
||
136 | foreach ($spiObjectStateGroups as $spiObjectStateGroup) { |
||
137 | $objectStateGroups[] = $this->buildDomainObjectStateGroupObject( |
||
138 | $spiObjectStateGroup, |
||
139 | $prioritizedLanguages |
||
140 | ); |
||
141 | } |
||
142 | |||
143 | return $objectStateGroups; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * This method returns the ordered list of object states of a group. |
||
148 | * |
||
149 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
150 | * @param string[] $prioritizedLanguages |
||
151 | * |
||
152 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[] |
||
153 | */ |
||
154 | public function loadObjectStates( |
||
155 | APIObjectStateGroup $objectStateGroup, |
||
156 | array $prioritizedLanguages = [] |
||
157 | ): iterable { |
||
158 | $spiObjectStates = $this->objectStateHandler->loadObjectStates($objectStateGroup->id); |
||
159 | |||
160 | $objectStates = []; |
||
161 | foreach ($spiObjectStates as $spiObjectState) { |
||
162 | $objectStates[] = $this->buildDomainObjectStateObject( |
||
163 | $spiObjectState, |
||
164 | $objectStateGroup, |
||
165 | $prioritizedLanguages |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | return $objectStates; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Updates an object state group. |
||
174 | * |
||
175 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state group |
||
176 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists |
||
177 | * |
||
178 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
179 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct |
||
180 | * |
||
181 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
182 | */ |
||
183 | public function updateObjectStateGroup(APIObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct): APIObjectStateGroup |
||
184 | { |
||
185 | if (!$this->permissionResolver->canUser('state', 'administrate', $objectStateGroup)) { |
||
186 | throw new UnauthorizedException('state', 'administrate'); |
||
187 | } |
||
188 | |||
189 | $loadedObjectStateGroup = $this->loadObjectStateGroup($objectStateGroup->id); |
||
190 | |||
191 | $inputStruct = $this->buildObjectStateGroupUpdateInputStruct( |
||
192 | $loadedObjectStateGroup, |
||
193 | $objectStateGroupUpdateStruct->identifier, |
||
194 | $objectStateGroupUpdateStruct->defaultLanguageCode, |
||
195 | $objectStateGroupUpdateStruct->names, |
||
196 | $objectStateGroupUpdateStruct->descriptions |
||
197 | ); |
||
198 | |||
199 | if ($objectStateGroupUpdateStruct->identifier !== null) { |
||
200 | try { |
||
201 | $existingObjectStateGroup = $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier); |
||
202 | if ($existingObjectStateGroup->id != $loadedObjectStateGroup->id) { |
||
203 | throw new InvalidArgumentException( |
||
204 | 'objectStateGroupUpdateStruct', |
||
205 | 'An Object state group with the provided identifier already exists' |
||
206 | ); |
||
207 | } |
||
208 | } catch (APINotFoundException $e) { |
||
209 | // Do nothing |
||
210 | } |
||
211 | } |
||
212 | |||
213 | $this->repository->beginTransaction(); |
||
214 | try { |
||
215 | $spiObjectStateGroup = $this->objectStateHandler->updateGroup( |
||
216 | $loadedObjectStateGroup->id, |
||
217 | $inputStruct |
||
218 | ); |
||
219 | $this->repository->commit(); |
||
220 | } catch (Exception $e) { |
||
221 | $this->repository->rollback(); |
||
222 | throw $e; |
||
223 | } |
||
224 | |||
225 | return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup, $objectStateGroup->prioritizedLanguages); |
||
|
|||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Deletes a object state group including all states and links to content. |
||
230 | * |
||
231 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state group |
||
232 | * |
||
233 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
234 | */ |
||
235 | public function deleteObjectStateGroup(APIObjectStateGroup $objectStateGroup): void |
||
252 | |||
253 | /** |
||
254 | * Creates a new object state in the given group. |
||
255 | * |
||
256 | * Note: in current kernel: If it is the first state all content objects will |
||
257 | * set to this state. |
||
258 | * |
||
259 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state |
||
260 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group |
||
261 | * |
||
262 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
263 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct $objectStateCreateStruct |
||
264 | * |
||
265 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
266 | */ |
||
267 | public function createObjectState(APIObjectStateGroup $objectStateGroup, ObjectStateCreateStruct $objectStateCreateStruct): APIObjectState |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function loadObjectState(int $stateId, array $prioritizedLanguages = []): APIObjectState |
||
318 | { |
||
319 | $spiObjectState = $this->objectStateHandler->load($stateId); |
||
323 | |||
324 | /** |
||
325 | * Updates an object state. |
||
326 | * |
||
327 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state |
||
328 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group |
||
329 | * |
||
330 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
331 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdateStruct |
||
332 | * |
||
333 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
334 | */ |
||
335 | public function updateObjectState(APIObjectState $objectState, ObjectStateUpdateStruct $objectStateUpdateStruct): APIObjectState |
||
383 | |||
384 | /** |
||
385 | * Changes the priority of the state. |
||
386 | * |
||
387 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change priority on an object state |
||
388 | * |
||
389 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
390 | * @param int $priority |
||
391 | */ |
||
392 | public function setPriorityOfObjectState(APIObjectState $objectState, int $priority): void |
||
412 | |||
413 | /** |
||
414 | * Deletes a object state. The state of the content objects is reset to the |
||
415 | * first object state in the group. |
||
416 | * |
||
417 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state |
||
418 | * |
||
419 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
420 | */ |
||
421 | public function deleteObjectState(APIObjectState $objectState): void |
||
438 | |||
439 | /** |
||
440 | * Sets the object-state of a state group to $state for the given content. |
||
441 | * |
||
442 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state does not belong to the given group |
||
443 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change the object state |
||
444 | * |
||
445 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
446 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
447 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
448 | */ |
||
449 | public function setContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup, APIObjectState $objectState): void |
||
474 | |||
475 | /** |
||
476 | * Gets the object-state of object identified by $contentId. |
||
477 | * |
||
478 | * The $state is the id of the state within one group. |
||
479 | * |
||
480 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
481 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
482 | * |
||
483 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
484 | */ |
||
485 | public function getContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup): APIObjectState |
||
494 | |||
495 | /** |
||
496 | * Returns the number of objects which are in this state. |
||
497 | * |
||
498 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
499 | * |
||
500 | * @return int |
||
501 | */ |
||
502 | public function getContentCount(APIObjectState $objectState): int |
||
508 | |||
509 | /** |
||
510 | * Instantiates a new Object State Group Create Struct and sets $identified in it. |
||
511 | * |
||
512 | * @param string $identifier |
||
513 | * |
||
514 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct |
||
515 | */ |
||
516 | public function newObjectStateGroupCreateStruct(string $identifier): ObjectStateGroupCreateStruct |
||
523 | |||
524 | /** |
||
525 | * Instantiates a new Object State Group Update Struct. |
||
526 | * |
||
527 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct |
||
528 | */ |
||
529 | public function newObjectStateGroupUpdateStruct(): ObjectStateGroupUpdateStruct |
||
533 | |||
534 | /** |
||
535 | * Instantiates a new Object State Create Struct and sets $identifier in it. |
||
536 | * |
||
537 | * @param string $identifier |
||
538 | * |
||
539 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct |
||
540 | */ |
||
541 | public function newObjectStateCreateStruct(string $identifier): ObjectStateCreateStruct |
||
548 | |||
549 | /** |
||
550 | * Instantiates a new Object State Update Struct. |
||
551 | * |
||
552 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct |
||
553 | */ |
||
554 | public function newObjectStateUpdateStruct(): ObjectStateUpdateStruct |
||
558 | |||
559 | /** |
||
560 | * Converts the object state SPI value object to API value object. |
||
561 | * |
||
562 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $spiObjectState |
||
563 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
564 | * @param string[] $prioritizedLanguages |
||
565 | * |
||
566 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
567 | */ |
||
568 | protected function buildDomainObjectStateObject( |
||
589 | |||
590 | /** |
||
591 | * Converts the object state group SPI value object to API value object. |
||
592 | * |
||
593 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $spiObjectStateGroup |
||
594 | * @param array $prioritizedLanguages |
||
595 | * |
||
596 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
597 | */ |
||
598 | protected function buildDomainObjectStateGroupObject( |
||
614 | |||
615 | /** |
||
616 | * Validates input for creating object states/groups and builds the InputStruct object. |
||
617 | * |
||
618 | * @param string $identifier |
||
619 | * @param string $defaultLanguageCode |
||
620 | * @param string[] $names |
||
621 | * @param string[]|null $descriptions |
||
622 | * |
||
623 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
624 | */ |
||
625 | protected function buildCreateInputStruct( |
||
677 | |||
678 | /** |
||
679 | * Validates input for updating object states and builds the InputStruct object. |
||
680 | * |
||
681 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
682 | * @param string|null $identifier |
||
683 | * @param string|null $defaultLanguageCode |
||
684 | * @param string[]|null $names |
||
685 | * @param string[]|null $descriptions |
||
686 | * |
||
687 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
688 | */ |
||
689 | protected function buildObjectStateUpdateInputStruct( |
||
746 | |||
747 | /** |
||
748 | * Validates input for updating object state groups and builds the InputStruct object. |
||
749 | * |
||
750 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
751 | * @param string|null $identifier |
||
752 | * @param string|null $defaultLanguageCode |
||
753 | * @param string[]|null $names |
||
754 | * @param string[]|null $descriptions |
||
755 | * |
||
756 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
757 | */ |
||
758 | protected function buildObjectStateGroupUpdateInputStruct( |
||
815 | } |
||
816 |
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@property
annotation 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.