Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
38 | class ObjectStateService implements ObjectStateServiceInterface |
||
39 | { |
||
40 | /** @var \eZ\Publish\API\Repository\Repository */ |
||
41 | protected $repository; |
||
42 | |||
43 | /** @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler */ |
||
44 | protected $objectStateHandler; |
||
45 | |||
46 | /** @var array */ |
||
47 | protected $settings; |
||
48 | |||
49 | /** |
||
50 | * Setups service with reference to repository object that created it & corresponding handler. |
||
51 | * |
||
52 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
53 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler |
||
54 | * @param array $settings |
||
55 | */ |
||
56 | public function __construct(RepositoryInterface $repository, Handler $objectStateHandler, array $settings = []) |
||
65 | |||
66 | /** |
||
67 | * Creates a new object state group. |
||
68 | * |
||
69 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state group |
||
70 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists |
||
71 | * |
||
72 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreateStruct |
||
73 | * |
||
74 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
75 | */ |
||
76 | View Code Duplication | public function createObjectStateGroup(ObjectStateGroupCreateStruct $objectStateGroupCreateStruct) |
|
77 | { |
||
78 | if (!$this->repository->canUser('state', 'administrate', $objectStateGroupCreateStruct)) { |
||
|
|||
79 | throw new UnauthorizedException('state', 'administrate'); |
||
80 | } |
||
81 | |||
82 | $inputStruct = $this->buildCreateInputStruct( |
||
83 | $objectStateGroupCreateStruct->identifier, |
||
84 | $objectStateGroupCreateStruct->defaultLanguageCode, |
||
85 | $objectStateGroupCreateStruct->names, |
||
86 | $objectStateGroupCreateStruct->descriptions |
||
87 | ); |
||
88 | |||
89 | try { |
||
90 | $this->objectStateHandler->loadGroupByIdentifier($inputStruct->identifier); |
||
91 | throw new InvalidArgumentException( |
||
92 | 'objectStateGroupCreateStruct', |
||
93 | 'Object state group with provided identifier already exists' |
||
94 | ); |
||
95 | } catch (APINotFoundException $e) { |
||
96 | // Do nothing |
||
97 | } |
||
98 | |||
99 | $this->repository->beginTransaction(); |
||
100 | try { |
||
101 | $spiObjectStateGroup = $this->objectStateHandler->createGroup($inputStruct); |
||
102 | $this->repository->commit(); |
||
103 | } catch (Exception $e) { |
||
104 | $this->repository->rollback(); |
||
105 | throw $e; |
||
106 | } |
||
107 | |||
108 | return $this->buildDomainObjectStateGroupObject($spiObjectStateGroup); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function loadObjectStateGroup($objectStateGroupId, array $prioritizedLanguages = []) |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function loadObjectStateGroups($offset = 0, $limit = -1, array $prioritizedLanguages = []) |
||
138 | |||
139 | /** |
||
140 | * This method returns the ordered list of object states of a group. |
||
141 | * |
||
142 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
143 | * @param string[] $prioritizedLanguages |
||
144 | * |
||
145 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[] |
||
146 | */ |
||
147 | public function loadObjectStates( |
||
164 | |||
165 | /** |
||
166 | * Updates an object state group. |
||
167 | * |
||
168 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state group |
||
169 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists |
||
170 | * |
||
171 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
172 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct |
||
173 | * |
||
174 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
175 | */ |
||
176 | View Code Duplication | public function updateObjectStateGroup(APIObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct) |
|
220 | |||
221 | /** |
||
222 | * Deletes a object state group including all states and links to content. |
||
223 | * |
||
224 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state group |
||
225 | * |
||
226 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
227 | */ |
||
228 | View Code Duplication | public function deleteObjectStateGroup(APIObjectStateGroup $objectStateGroup) |
|
245 | |||
246 | /** |
||
247 | * Creates a new object state in the given group. |
||
248 | * |
||
249 | * Note: in current kernel: If it is the first state all content objects will |
||
250 | * set to this state. |
||
251 | * |
||
252 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state |
||
253 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group |
||
254 | * |
||
255 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
256 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct $objectStateCreateStruct |
||
257 | * |
||
258 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
259 | */ |
||
260 | public function createObjectState(APIObjectStateGroup $objectStateGroup, ObjectStateCreateStruct $objectStateCreateStruct) |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function loadObjectState($stateId, array $prioritizedLanguages = []) |
||
316 | |||
317 | /** |
||
318 | * Updates an object state. |
||
319 | * |
||
320 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state |
||
321 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group |
||
322 | * |
||
323 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
324 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdateStruct |
||
325 | * |
||
326 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
327 | */ |
||
328 | View Code Duplication | public function updateObjectState(APIObjectState $objectState, ObjectStateUpdateStruct $objectStateUpdateStruct) |
|
376 | |||
377 | /** |
||
378 | * Changes the priority of the state. |
||
379 | * |
||
380 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change priority on an object state |
||
381 | * |
||
382 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
383 | * @param int $priority |
||
384 | */ |
||
385 | View Code Duplication | public function setPriorityOfObjectState(APIObjectState $objectState, $priority) |
|
409 | |||
410 | /** |
||
411 | * Deletes a object state. The state of the content objects is reset to the |
||
412 | * first object state in the group. |
||
413 | * |
||
414 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state |
||
415 | * |
||
416 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
417 | */ |
||
418 | View Code Duplication | public function deleteObjectState(APIObjectState $objectState) |
|
435 | |||
436 | /** |
||
437 | * Sets the object-state of a state group to $state for the given content. |
||
438 | * |
||
439 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state does not belong to the given group |
||
440 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change the object state |
||
441 | * |
||
442 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
443 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
444 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
445 | */ |
||
446 | public function setContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup, APIObjectState $objectState) |
||
471 | |||
472 | /** |
||
473 | * Gets the object-state of object identified by $contentId. |
||
474 | * |
||
475 | * The $state is the id of the state within one group. |
||
476 | * |
||
477 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
478 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
479 | * |
||
480 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
481 | */ |
||
482 | public function getContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup) |
||
491 | |||
492 | /** |
||
493 | * Returns the number of objects which are in this state. |
||
494 | * |
||
495 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
496 | * |
||
497 | * @return int |
||
498 | */ |
||
499 | public function getContentCount(APIObjectState $objectState) |
||
505 | |||
506 | /** |
||
507 | * Instantiates a new Object State Group Create Struct and sets $identified in it. |
||
508 | * |
||
509 | * @param string $identifier |
||
510 | * |
||
511 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct |
||
512 | */ |
||
513 | public function newObjectStateGroupCreateStruct($identifier) |
||
520 | |||
521 | /** |
||
522 | * Instantiates a new Object State Group Update Struct. |
||
523 | * |
||
524 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct |
||
525 | */ |
||
526 | public function newObjectStateGroupUpdateStruct() |
||
530 | |||
531 | /** |
||
532 | * Instantiates a new Object State Create Struct and sets $identifier in it. |
||
533 | * |
||
534 | * @param string $identifier |
||
535 | * |
||
536 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct |
||
537 | */ |
||
538 | public function newObjectStateCreateStruct($identifier) |
||
545 | |||
546 | /** |
||
547 | * Instantiates a new Object State Update Struct. |
||
548 | * |
||
549 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct |
||
550 | */ |
||
551 | public function newObjectStateUpdateStruct() |
||
555 | |||
556 | /** |
||
557 | * Converts the object state SPI value object to API value object. |
||
558 | * |
||
559 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $spiObjectState |
||
560 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
561 | * @param string[] $prioritizedLanguages |
||
562 | * |
||
563 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
564 | */ |
||
565 | protected function buildDomainObjectStateObject( |
||
586 | |||
587 | /** |
||
588 | * Converts the object state group SPI value object to API value object. |
||
589 | * |
||
590 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $spiObjectStateGroup |
||
591 | * @param array $prioritizedLanguages |
||
592 | * |
||
593 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
594 | */ |
||
595 | protected function buildDomainObjectStateGroupObject( |
||
611 | |||
612 | /** |
||
613 | * Validates input for creating object states/groups and builds the InputStruct object. |
||
614 | * |
||
615 | * @param string $identifier |
||
616 | * @param string $defaultLanguageCode |
||
617 | * @param string[] $names |
||
618 | * @param string[] $descriptions |
||
619 | * |
||
620 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
621 | */ |
||
622 | protected function buildCreateInputStruct($identifier, $defaultLanguageCode, $names, $descriptions) |
||
674 | |||
675 | /** |
||
676 | * Validates input for updating object states and builds the InputStruct object. |
||
677 | * |
||
678 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
679 | * @param string $identifier |
||
680 | * @param string $defaultLanguageCode |
||
681 | * @param string[] $names |
||
682 | * @param string[] $descriptions |
||
683 | * |
||
684 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
685 | */ |
||
686 | View Code Duplication | protected function buildObjectStateUpdateInputStruct(APIObjectState $objectState, $identifier, $defaultLanguageCode, $names, $descriptions) |
|
742 | |||
743 | /** |
||
744 | * Validates input for updating object state groups and builds the InputStruct object. |
||
745 | * |
||
746 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
747 | * @param string $identifier |
||
748 | * @param string $defaultLanguageCode |
||
749 | * @param string[] $names |
||
750 | * @param string[] $descriptions |
||
751 | * |
||
752 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
753 | */ |
||
754 | View Code Duplication | protected function buildObjectStateGroupUpdateInputStruct(APIObjectStateGroup $objectStateGroup, $identifier, $defaultLanguageCode, $names, $descriptions) |
|
810 | } |
||
811 |
This method 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 method will be removed from the class and what other method or class to use instead.