vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/ContentService.php line 51

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\ContentService as ContentServiceInterface;
  9. use eZ\Publish\API\Repository\Values\Content\Content;
  10. use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
  11. use eZ\Publish\API\Repository\Values\Content\ContentInfo;
  12. use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
  13. use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
  14. use eZ\Publish\API\Repository\Values\Content\Language;
  15. use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
  16. use eZ\Publish\API\Repository\Values\Content\Relation;
  17. use eZ\Publish\API\Repository\Values\Content\VersionInfo;
  18. use eZ\Publish\API\Repository\Values\User\User;
  19. use eZ\Publish\API\Repository\Events\Content\AddRelationEvent;
  20. use eZ\Publish\API\Repository\Events\Content\BeforeAddRelationEvent;
  21. use eZ\Publish\API\Repository\Events\Content\BeforeCopyContentEvent;
  22. use eZ\Publish\API\Repository\Events\Content\BeforeCreateContentDraftEvent;
  23. use eZ\Publish\API\Repository\Events\Content\BeforeCreateContentEvent;
  24. use eZ\Publish\API\Repository\Events\Content\BeforeDeleteContentEvent;
  25. use eZ\Publish\API\Repository\Events\Content\BeforeDeleteRelationEvent;
  26. use eZ\Publish\API\Repository\Events\Content\BeforeDeleteTranslationEvent;
  27. use eZ\Publish\API\Repository\Events\Content\BeforeDeleteVersionEvent;
  28. use eZ\Publish\API\Repository\Events\Content\BeforeHideContentEvent;
  29. use eZ\Publish\API\Repository\Events\Content\BeforePublishVersionEvent;
  30. use eZ\Publish\API\Repository\Events\Content\BeforeRevealContentEvent;
  31. use eZ\Publish\API\Repository\Events\Content\BeforeUpdateContentEvent;
  32. use eZ\Publish\API\Repository\Events\Content\BeforeUpdateContentMetadataEvent;
  33. use eZ\Publish\API\Repository\Events\Content\CopyContentEvent;
  34. use eZ\Publish\API\Repository\Events\Content\CreateContentDraftEvent;
  35. use eZ\Publish\API\Repository\Events\Content\CreateContentEvent;
  36. use eZ\Publish\API\Repository\Events\Content\DeleteContentEvent;
  37. use eZ\Publish\API\Repository\Events\Content\DeleteRelationEvent;
  38. use eZ\Publish\API\Repository\Events\Content\DeleteTranslationEvent;
  39. use eZ\Publish\API\Repository\Events\Content\DeleteVersionEvent;
  40. use eZ\Publish\API\Repository\Events\Content\HideContentEvent;
  41. use eZ\Publish\API\Repository\Events\Content\PublishVersionEvent;
  42. use eZ\Publish\API\Repository\Events\Content\RevealContentEvent;
  43. use eZ\Publish\API\Repository\Events\Content\UpdateContentEvent;
  44. use eZ\Publish\API\Repository\Events\Content\UpdateContentMetadataEvent;
  45. use eZ\Publish\SPI\Repository\Decorator\ContentServiceDecorator;
  46. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  47. class ContentService extends ContentServiceDecorator
  48. {
  49.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  50.     protected $eventDispatcher;
  51.     public function __construct(
  52.         ContentServiceInterface $innerService,
  53.         EventDispatcherInterface $eventDispatcher
  54.     ) {
  55.         parent::__construct($innerService);
  56.         $this->eventDispatcher $eventDispatcher;
  57.     }
  58.     public function createContent(
  59.         ContentCreateStruct $contentCreateStruct,
  60.         array $locationCreateStructs = [],
  61.         ?array $fieldIdentifiersToValidate null
  62.     ): Content {
  63.         $eventData = [
  64.             $contentCreateStruct,
  65.             $locationCreateStructs,
  66.             $fieldIdentifiersToValidate,
  67.         ];
  68.         $beforeEvent = new BeforeCreateContentEvent(...$eventData);
  69.         $this->eventDispatcher->dispatch($beforeEvent);
  70.         if ($beforeEvent->isPropagationStopped()) {
  71.             return $beforeEvent->getContent();
  72.         }
  73.         $content $beforeEvent->hasContent()
  74.             ? $beforeEvent->getContent()
  75.             : $this->innerService->createContent($contentCreateStruct$locationCreateStructs$fieldIdentifiersToValidate);
  76.         $this->eventDispatcher->dispatch(
  77.             new CreateContentEvent($content, ...$eventData)
  78.         );
  79.         return $content;
  80.     }
  81.     public function updateContentMetadata(
  82.         ContentInfo $contentInfo,
  83.         ContentMetadataUpdateStruct $contentMetadataUpdateStruct
  84.     ): Content {
  85.         $eventData = [
  86.             $contentInfo,
  87.             $contentMetadataUpdateStruct,
  88.         ];
  89.         $beforeEvent = new BeforeUpdateContentMetadataEvent(...$eventData);
  90.         $this->eventDispatcher->dispatch($beforeEvent);
  91.         if ($beforeEvent->isPropagationStopped()) {
  92.             return $beforeEvent->getContent();
  93.         }
  94.         $content $beforeEvent->hasContent()
  95.             ? $beforeEvent->getContent()
  96.             : $this->innerService->updateContentMetadata($contentInfo$contentMetadataUpdateStruct);
  97.         $this->eventDispatcher->dispatch(
  98.             new UpdateContentMetadataEvent($content, ...$eventData)
  99.         );
  100.         return $content;
  101.     }
  102.     public function deleteContent(ContentInfo $contentInfo): iterable
  103.     {
  104.         $eventData = [$contentInfo];
  105.         $beforeEvent = new BeforeDeleteContentEvent(...$eventData);
  106.         $this->eventDispatcher->dispatch($beforeEvent);
  107.         if ($beforeEvent->isPropagationStopped()) {
  108.             return $beforeEvent->getLocations();
  109.         }
  110.         $locations $beforeEvent->hasLocations()
  111.             ? $beforeEvent->getLocations()
  112.             : $this->innerService->deleteContent($contentInfo);
  113.         $this->eventDispatcher->dispatch(
  114.             new DeleteContentEvent($locations, ...$eventData)
  115.         );
  116.         return $locations;
  117.     }
  118.     public function createContentDraft(
  119.         ContentInfo $contentInfo,
  120.         ?VersionInfo $versionInfo null,
  121.         ?User $creator null,
  122.         ?Language $language null
  123.     ): Content {
  124.         $eventData = [
  125.             $contentInfo,
  126.             $versionInfo,
  127.             $creator,
  128.             $language,
  129.         ];
  130.         $beforeEvent = new BeforeCreateContentDraftEvent(...$eventData);
  131.         $this->eventDispatcher->dispatch($beforeEvent);
  132.         if ($beforeEvent->isPropagationStopped()) {
  133.             return $beforeEvent->getContentDraft();
  134.         }
  135.         $contentDraft $beforeEvent->hasContentDraft()
  136.             ? $beforeEvent->getContentDraft()
  137.             : $this->innerService->createContentDraft($contentInfo$versionInfo$creator$language);
  138.         $this->eventDispatcher->dispatch(
  139.             new CreateContentDraftEvent($contentDraft, ...$eventData)
  140.         );
  141.         return $contentDraft;
  142.     }
  143.     public function updateContent(
  144.         VersionInfo $versionInfo,
  145.         ContentUpdateStruct $contentUpdateStruct,
  146.         ?array $fieldIdentifiersToValidate null
  147.     ): Content {
  148.         $eventData = [
  149.             $versionInfo,
  150.             $contentUpdateStruct,
  151.             $fieldIdentifiersToValidate,
  152.         ];
  153.         $beforeEvent = new BeforeUpdateContentEvent(...$eventData);
  154.         $this->eventDispatcher->dispatch($beforeEvent);
  155.         if ($beforeEvent->isPropagationStopped()) {
  156.             return $beforeEvent->getContent();
  157.         }
  158.         $content $beforeEvent->hasContent()
  159.             ? $beforeEvent->getContent()
  160.             : $this->innerService->updateContent($versionInfo$contentUpdateStruct$fieldIdentifiersToValidate);
  161.         $this->eventDispatcher->dispatch(
  162.             new UpdateContentEvent($content, ...$eventData)
  163.         );
  164.         return $content;
  165.     }
  166.     public function publishVersion(VersionInfo $versionInfo, array $translations Language::ALL): Content
  167.     {
  168.         $eventData = [
  169.             $versionInfo,
  170.             $translations,
  171.         ];
  172.         $beforeEvent = new BeforePublishVersionEvent(...$eventData);
  173.         $this->eventDispatcher->dispatch($beforeEvent);
  174.         if ($beforeEvent->isPropagationStopped()) {
  175.             return $beforeEvent->getContent();
  176.         }
  177.         $content $beforeEvent->hasContent()
  178.             ? $beforeEvent->getContent()
  179.             : $this->innerService->publishVersion($versionInfo$translations);
  180.         $this->eventDispatcher->dispatch(
  181.             new PublishVersionEvent($content, ...$eventData)
  182.         );
  183.         return $content;
  184.     }
  185.     public function deleteVersion(VersionInfo $versionInfo): void
  186.     {
  187.         $eventData = [$versionInfo];
  188.         $beforeEvent = new BeforeDeleteVersionEvent(...$eventData);
  189.         $this->eventDispatcher->dispatch($beforeEvent);
  190.         if ($beforeEvent->isPropagationStopped()) {
  191.             return;
  192.         }
  193.         $this->innerService->deleteVersion($versionInfo);
  194.         $this->eventDispatcher->dispatch(
  195.             new DeleteVersionEvent(...$eventData)
  196.         );
  197.     }
  198.     public function copyContent(
  199.         ContentInfo $contentInfo,
  200.         LocationCreateStruct $destinationLocationCreateStruct,
  201.         ?VersionInfo $versionInfo null
  202.     ): Content {
  203.         $eventData = [
  204.             $contentInfo,
  205.             $destinationLocationCreateStruct,
  206.             $versionInfo,
  207.         ];
  208.         $beforeEvent = new BeforeCopyContentEvent(...$eventData);
  209.         $this->eventDispatcher->dispatch($beforeEvent);
  210.         if ($beforeEvent->isPropagationStopped()) {
  211.             return $beforeEvent->getContent();
  212.         }
  213.         $content $beforeEvent->hasContent()
  214.             ? $beforeEvent->getContent()
  215.             : $this->innerService->copyContent($contentInfo$destinationLocationCreateStruct$versionInfo);
  216.         $this->eventDispatcher->dispatch(
  217.             new CopyContentEvent($content, ...$eventData)
  218.         );
  219.         return $content;
  220.     }
  221.     public function addRelation(
  222.         VersionInfo $sourceVersion,
  223.         ContentInfo $destinationContent
  224.     ): Relation {
  225.         $eventData = [
  226.             $sourceVersion,
  227.             $destinationContent,
  228.         ];
  229.         $beforeEvent = new BeforeAddRelationEvent(...$eventData);
  230.         $this->eventDispatcher->dispatch($beforeEvent);
  231.         if ($beforeEvent->isPropagationStopped()) {
  232.             return $beforeEvent->getRelation();
  233.         }
  234.         $relation $beforeEvent->hasRelation()
  235.             ? $beforeEvent->getRelation()
  236.             : $this->innerService->addRelation($sourceVersion$destinationContent);
  237.         $this->eventDispatcher->dispatch(
  238.             new AddRelationEvent($relation, ...$eventData)
  239.         );
  240.         return $relation;
  241.     }
  242.     public function deleteRelation(
  243.         VersionInfo $sourceVersion,
  244.         ContentInfo $destinationContent
  245.     ): void {
  246.         $eventData = [
  247.             $sourceVersion,
  248.             $destinationContent,
  249.         ];
  250.         $beforeEvent = new BeforeDeleteRelationEvent(...$eventData);
  251.         $this->eventDispatcher->dispatch($beforeEvent);
  252.         if ($beforeEvent->isPropagationStopped()) {
  253.             return;
  254.         }
  255.         $this->innerService->deleteRelation($sourceVersion$destinationContent);
  256.         $this->eventDispatcher->dispatch(
  257.             new DeleteRelationEvent(...$eventData)
  258.         );
  259.     }
  260.     public function deleteTranslation(
  261.         ContentInfo $contentInfo,
  262.         string $languageCode
  263.     ): void {
  264.         $eventData = [
  265.             $contentInfo,
  266.             $languageCode,
  267.         ];
  268.         $beforeEvent = new BeforeDeleteTranslationEvent(...$eventData);
  269.         $this->eventDispatcher->dispatch($beforeEvent);
  270.         if ($beforeEvent->isPropagationStopped()) {
  271.             return;
  272.         }
  273.         $this->innerService->deleteTranslation($contentInfo$languageCode);
  274.         $this->eventDispatcher->dispatch(
  275.             new DeleteTranslationEvent(...$eventData)
  276.         );
  277.     }
  278.     public function hideContent(ContentInfo $contentInfo): void
  279.     {
  280.         $eventData = [$contentInfo];
  281.         $beforeEvent = new BeforeHideContentEvent(...$eventData);
  282.         $this->eventDispatcher->dispatch($beforeEvent);
  283.         if ($beforeEvent->isPropagationStopped()) {
  284.             return;
  285.         }
  286.         $this->innerService->hideContent($contentInfo);
  287.         $this->eventDispatcher->dispatch(
  288.             new HideContentEvent(...$eventData)
  289.         );
  290.     }
  291.     public function revealContent(ContentInfo $contentInfo): void
  292.     {
  293.         $eventData = [$contentInfo];
  294.         $beforeEvent = new BeforeRevealContentEvent(...$eventData);
  295.         $this->eventDispatcher->dispatch($beforeEvent);
  296.         if ($beforeEvent->isPropagationStopped()) {
  297.             return;
  298.         }
  299.         $this->innerService->revealContent($contentInfo);
  300.         $this->eventDispatcher->dispatch(
  301.             new RevealContentEvent(...$eventData)
  302.         );
  303.     }
  304. }