vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/ObjectStateService.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace eZ\Publish\Core\Event;
  8. use eZ\Publish\API\Repository\ObjectStateService as ObjectStateServiceInterface;
  9. use eZ\Publish\API\Repository\Values\Content\ContentInfo;
  10. use eZ\Publish\API\Repository\Values\ObjectState\ObjectState;
  11. use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct;
  12. use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup;
  13. use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct;
  14. use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct;
  15. use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct;
  16. use eZ\Publish\API\Repository\Events\ObjectState\BeforeCreateObjectStateEvent;
  17. use eZ\Publish\API\Repository\Events\ObjectState\BeforeCreateObjectStateGroupEvent;
  18. use eZ\Publish\API\Repository\Events\ObjectState\BeforeDeleteObjectStateEvent;
  19. use eZ\Publish\API\Repository\Events\ObjectState\BeforeDeleteObjectStateGroupEvent;
  20. use eZ\Publish\API\Repository\Events\ObjectState\BeforeSetContentStateEvent;
  21. use eZ\Publish\API\Repository\Events\ObjectState\BeforeSetPriorityOfObjectStateEvent;
  22. use eZ\Publish\API\Repository\Events\ObjectState\BeforeUpdateObjectStateEvent;
  23. use eZ\Publish\API\Repository\Events\ObjectState\BeforeUpdateObjectStateGroupEvent;
  24. use eZ\Publish\API\Repository\Events\ObjectState\CreateObjectStateEvent;
  25. use eZ\Publish\API\Repository\Events\ObjectState\CreateObjectStateGroupEvent;
  26. use eZ\Publish\API\Repository\Events\ObjectState\DeleteObjectStateEvent;
  27. use eZ\Publish\API\Repository\Events\ObjectState\DeleteObjectStateGroupEvent;
  28. use eZ\Publish\API\Repository\Events\ObjectState\SetContentStateEvent;
  29. use eZ\Publish\API\Repository\Events\ObjectState\SetPriorityOfObjectStateEvent;
  30. use eZ\Publish\API\Repository\Events\ObjectState\UpdateObjectStateEvent;
  31. use eZ\Publish\API\Repository\Events\ObjectState\UpdateObjectStateGroupEvent;
  32. use eZ\Publish\SPI\Repository\Decorator\ObjectStateServiceDecorator;
  33. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  34. class ObjectStateService extends ObjectStateServiceDecorator
  35. {
  36.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  37.     protected $eventDispatcher;
  38.     public function __construct(
  39.         ObjectStateServiceInterface $innerService,
  40.         EventDispatcherInterface $eventDispatcher
  41.     ) {
  42.         parent::__construct($innerService);
  43.         $this->eventDispatcher $eventDispatcher;
  44.     }
  45.     public function createObjectStateGroup(ObjectStateGroupCreateStruct $objectStateGroupCreateStruct): ObjectStateGroup
  46.     {
  47.         $eventData = [$objectStateGroupCreateStruct];
  48.         $beforeEvent = new BeforeCreateObjectStateGroupEvent(...$eventData);
  49.         $this->eventDispatcher->dispatch($beforeEvent);
  50.         if ($beforeEvent->isPropagationStopped()) {
  51.             return $beforeEvent->getObjectStateGroup();
  52.         }
  53.         $objectStateGroup $beforeEvent->hasObjectStateGroup()
  54.             ? $beforeEvent->getObjectStateGroup()
  55.             : $this->innerService->createObjectStateGroup($objectStateGroupCreateStruct);
  56.         $this->eventDispatcher->dispatch(
  57.             new CreateObjectStateGroupEvent($objectStateGroup, ...$eventData)
  58.         );
  59.         return $objectStateGroup;
  60.     }
  61.     public function updateObjectStateGroup(
  62.         ObjectStateGroup $objectStateGroup,
  63.         ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct
  64.     ): ObjectStateGroup {
  65.         $eventData = [
  66.             $objectStateGroup,
  67.             $objectStateGroupUpdateStruct,
  68.         ];
  69.         $beforeEvent = new BeforeUpdateObjectStateGroupEvent(...$eventData);
  70.         $this->eventDispatcher->dispatch($beforeEvent);
  71.         if ($beforeEvent->isPropagationStopped()) {
  72.             return $beforeEvent->getUpdatedObjectStateGroup();
  73.         }
  74.         $updatedObjectStateGroup $beforeEvent->hasUpdatedObjectStateGroup()
  75.             ? $beforeEvent->getUpdatedObjectStateGroup()
  76.             : $this->innerService->updateObjectStateGroup($objectStateGroup$objectStateGroupUpdateStruct);
  77.         $this->eventDispatcher->dispatch(
  78.             new UpdateObjectStateGroupEvent($updatedObjectStateGroup, ...$eventData)
  79.         );
  80.         return $updatedObjectStateGroup;
  81.     }
  82.     public function deleteObjectStateGroup(ObjectStateGroup $objectStateGroup): void
  83.     {
  84.         $eventData = [$objectStateGroup];
  85.         $beforeEvent = new BeforeDeleteObjectStateGroupEvent(...$eventData);
  86.         $this->eventDispatcher->dispatch($beforeEvent);
  87.         if ($beforeEvent->isPropagationStopped()) {
  88.             return;
  89.         }
  90.         $this->innerService->deleteObjectStateGroup($objectStateGroup);
  91.         $this->eventDispatcher->dispatch(
  92.             new DeleteObjectStateGroupEvent(...$eventData)
  93.         );
  94.     }
  95.     public function createObjectState(
  96.         ObjectStateGroup $objectStateGroup,
  97.         ObjectStateCreateStruct $objectStateCreateStruct
  98.     ): ObjectState {
  99.         $eventData = [
  100.             $objectStateGroup,
  101.             $objectStateCreateStruct,
  102.         ];
  103.         $beforeEvent = new BeforeCreateObjectStateEvent(...$eventData);
  104.         $this->eventDispatcher->dispatch($beforeEvent);
  105.         if ($beforeEvent->isPropagationStopped()) {
  106.             return $beforeEvent->getObjectState();
  107.         }
  108.         $objectState $beforeEvent->hasObjectState()
  109.             ? $beforeEvent->getObjectState()
  110.             : $this->innerService->createObjectState($objectStateGroup$objectStateCreateStruct);
  111.         $this->eventDispatcher->dispatch(
  112.             new CreateObjectStateEvent($objectState, ...$eventData)
  113.         );
  114.         return $objectState;
  115.     }
  116.     public function updateObjectState(
  117.         ObjectState $objectState,
  118.         ObjectStateUpdateStruct $objectStateUpdateStruct
  119.     ): ObjectState {
  120.         $eventData = [
  121.             $objectState,
  122.             $objectStateUpdateStruct,
  123.         ];
  124.         $beforeEvent = new BeforeUpdateObjectStateEvent(...$eventData);
  125.         $this->eventDispatcher->dispatch($beforeEvent);
  126.         if ($beforeEvent->isPropagationStopped()) {
  127.             return $beforeEvent->getUpdatedObjectState();
  128.         }
  129.         $updatedObjectState $beforeEvent->hasUpdatedObjectState()
  130.             ? $beforeEvent->getUpdatedObjectState()
  131.             : $this->innerService->updateObjectState($objectState$objectStateUpdateStruct);
  132.         $this->eventDispatcher->dispatch(
  133.             new UpdateObjectStateEvent($updatedObjectState, ...$eventData)
  134.         );
  135.         return $updatedObjectState;
  136.     }
  137.     public function setPriorityOfObjectState(
  138.         ObjectState $objectState,
  139.         int $priority
  140.     ): void {
  141.         $eventData = [
  142.             $objectState,
  143.             $priority,
  144.         ];
  145.         $beforeEvent = new BeforeSetPriorityOfObjectStateEvent(...$eventData);
  146.         $this->eventDispatcher->dispatch($beforeEvent);
  147.         if ($beforeEvent->isPropagationStopped()) {
  148.             return;
  149.         }
  150.         $this->innerService->setPriorityOfObjectState($objectState$priority);
  151.         $this->eventDispatcher->dispatch(
  152.             new SetPriorityOfObjectStateEvent(...$eventData)
  153.         );
  154.     }
  155.     public function deleteObjectState(ObjectState $objectState): void
  156.     {
  157.         $eventData = [$objectState];
  158.         $beforeEvent = new BeforeDeleteObjectStateEvent(...$eventData);
  159.         $this->eventDispatcher->dispatch($beforeEvent);
  160.         if ($beforeEvent->isPropagationStopped()) {
  161.             return;
  162.         }
  163.         $this->innerService->deleteObjectState($objectState);
  164.         $this->eventDispatcher->dispatch(
  165.             new DeleteObjectStateEvent(...$eventData)
  166.         );
  167.     }
  168.     public function setContentState(
  169.         ContentInfo $contentInfo,
  170.         ObjectStateGroup $objectStateGroup,
  171.         ObjectState $objectState
  172.     ): void {
  173.         $eventData = [
  174.             $contentInfo,
  175.             $objectStateGroup,
  176.             $objectState,
  177.         ];
  178.         $beforeEvent = new BeforeSetContentStateEvent(...$eventData);
  179.         $this->eventDispatcher->dispatch($beforeEvent);
  180.         if ($beforeEvent->isPropagationStopped()) {
  181.             return;
  182.         }
  183.         $this->innerService->setContentState($contentInfo$objectStateGroup$objectState);
  184.         $this->eventDispatcher->dispatch(
  185.             new SetContentStateEvent(...$eventData)
  186.         );
  187.     }
  188. }