vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/ContentTypeService.php line 56

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\ContentTypeService as ContentTypeServiceInterface;
  9. use eZ\Publish\API\Repository\Values\ContentType\ContentType;
  10. use eZ\Publish\API\Repository\Values\ContentType\ContentTypeCreateStruct;
  11. use eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft;
  12. use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup;
  13. use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroupCreateStruct;
  14. use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroupUpdateStruct;
  15. use eZ\Publish\API\Repository\Values\ContentType\ContentTypeUpdateStruct;
  16. use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
  17. use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
  18. use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct;
  19. use eZ\Publish\API\Repository\Values\User\User;
  20. use eZ\Publish\API\Repository\Events\ContentType\AddFieldDefinitionEvent;
  21. use eZ\Publish\API\Repository\Events\ContentType\AssignContentTypeGroupEvent;
  22. use eZ\Publish\API\Repository\Events\ContentType\BeforeAddFieldDefinitionEvent;
  23. use eZ\Publish\API\Repository\Events\ContentType\BeforeAssignContentTypeGroupEvent;
  24. use eZ\Publish\API\Repository\Events\ContentType\BeforeCopyContentTypeEvent;
  25. use eZ\Publish\API\Repository\Events\ContentType\BeforeCreateContentTypeDraftEvent;
  26. use eZ\Publish\API\Repository\Events\ContentType\BeforeCreateContentTypeEvent;
  27. use eZ\Publish\API\Repository\Events\ContentType\BeforeCreateContentTypeGroupEvent;
  28. use eZ\Publish\API\Repository\Events\ContentType\BeforeDeleteContentTypeEvent;
  29. use eZ\Publish\API\Repository\Events\ContentType\BeforeDeleteContentTypeGroupEvent;
  30. use eZ\Publish\API\Repository\Events\ContentType\BeforePublishContentTypeDraftEvent;
  31. use eZ\Publish\API\Repository\Events\ContentType\BeforeRemoveContentTypeTranslationEvent;
  32. use eZ\Publish\API\Repository\Events\ContentType\BeforeRemoveFieldDefinitionEvent;
  33. use eZ\Publish\API\Repository\Events\ContentType\BeforeUnassignContentTypeGroupEvent;
  34. use eZ\Publish\API\Repository\Events\ContentType\BeforeUpdateContentTypeDraftEvent;
  35. use eZ\Publish\API\Repository\Events\ContentType\BeforeUpdateContentTypeGroupEvent;
  36. use eZ\Publish\API\Repository\Events\ContentType\BeforeUpdateFieldDefinitionEvent;
  37. use eZ\Publish\API\Repository\Events\ContentType\CopyContentTypeEvent;
  38. use eZ\Publish\API\Repository\Events\ContentType\CreateContentTypeDraftEvent;
  39. use eZ\Publish\API\Repository\Events\ContentType\CreateContentTypeEvent;
  40. use eZ\Publish\API\Repository\Events\ContentType\CreateContentTypeGroupEvent;
  41. use eZ\Publish\API\Repository\Events\ContentType\DeleteContentTypeEvent;
  42. use eZ\Publish\API\Repository\Events\ContentType\DeleteContentTypeGroupEvent;
  43. use eZ\Publish\API\Repository\Events\ContentType\PublishContentTypeDraftEvent;
  44. use eZ\Publish\API\Repository\Events\ContentType\RemoveContentTypeTranslationEvent;
  45. use eZ\Publish\API\Repository\Events\ContentType\RemoveFieldDefinitionEvent;
  46. use eZ\Publish\API\Repository\Events\ContentType\UnassignContentTypeGroupEvent;
  47. use eZ\Publish\API\Repository\Events\ContentType\UpdateContentTypeDraftEvent;
  48. use eZ\Publish\API\Repository\Events\ContentType\UpdateContentTypeGroupEvent;
  49. use eZ\Publish\API\Repository\Events\ContentType\UpdateFieldDefinitionEvent;
  50. use eZ\Publish\SPI\Repository\Decorator\ContentTypeServiceDecorator;
  51. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  52. class ContentTypeService extends ContentTypeServiceDecorator
  53. {
  54.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  55.     protected $eventDispatcher;
  56.     public function __construct(
  57.         ContentTypeServiceInterface $innerService,
  58.         EventDispatcherInterface $eventDispatcher
  59.     ) {
  60.         parent::__construct($innerService);
  61.         $this->eventDispatcher $eventDispatcher;
  62.     }
  63.     public function createContentTypeGroup(ContentTypeGroupCreateStruct $contentTypeGroupCreateStruct): ContentTypeGroup
  64.     {
  65.         $eventData = [$contentTypeGroupCreateStruct];
  66.         $beforeEvent = new BeforeCreateContentTypeGroupEvent(...$eventData);
  67.         $this->eventDispatcher->dispatch($beforeEvent);
  68.         if ($beforeEvent->isPropagationStopped()) {
  69.             return $beforeEvent->getContentTypeGroup();
  70.         }
  71.         $contentTypeGroup $beforeEvent->hasContentTypeGroup()
  72.             ? $beforeEvent->getContentTypeGroup()
  73.             : $this->innerService->createContentTypeGroup($contentTypeGroupCreateStruct);
  74.         $this->eventDispatcher->dispatch(
  75.             new CreateContentTypeGroupEvent($contentTypeGroup, ...$eventData)
  76.         );
  77.         return $contentTypeGroup;
  78.     }
  79.     public function updateContentTypeGroup(
  80.         ContentTypeGroup $contentTypeGroup,
  81.         ContentTypeGroupUpdateStruct $contentTypeGroupUpdateStruct
  82.     ): void {
  83.         $eventData = [
  84.             $contentTypeGroup,
  85.             $contentTypeGroupUpdateStruct,
  86.         ];
  87.         $beforeEvent = new BeforeUpdateContentTypeGroupEvent(...$eventData);
  88.         $this->eventDispatcher->dispatch($beforeEvent);
  89.         if ($beforeEvent->isPropagationStopped()) {
  90.             return;
  91.         }
  92.         $this->innerService->updateContentTypeGroup($contentTypeGroup$contentTypeGroupUpdateStruct);
  93.         $this->eventDispatcher->dispatch(
  94.             new UpdateContentTypeGroupEvent(...$eventData)
  95.         );
  96.     }
  97.     public function deleteContentTypeGroup(ContentTypeGroup $contentTypeGroup): void
  98.     {
  99.         $eventData = [$contentTypeGroup];
  100.         $beforeEvent = new BeforeDeleteContentTypeGroupEvent(...$eventData);
  101.         $this->eventDispatcher->dispatch($beforeEvent);
  102.         if ($beforeEvent->isPropagationStopped()) {
  103.             return;
  104.         }
  105.         $this->innerService->deleteContentTypeGroup($contentTypeGroup);
  106.         $this->eventDispatcher->dispatch(
  107.             new DeleteContentTypeGroupEvent(...$eventData)
  108.         );
  109.     }
  110.     public function createContentType(
  111.         ContentTypeCreateStruct $contentTypeCreateStruct,
  112.         array $contentTypeGroups
  113.     ): ContentTypeDraft {
  114.         $eventData = [
  115.             $contentTypeCreateStruct,
  116.             $contentTypeGroups,
  117.         ];
  118.         $beforeEvent = new BeforeCreateContentTypeEvent(...$eventData);
  119.         $this->eventDispatcher->dispatch($beforeEvent);
  120.         if ($beforeEvent->isPropagationStopped()) {
  121.             return $beforeEvent->getContentTypeDraft();
  122.         }
  123.         $contentTypeDraft $beforeEvent->hasContentTypeDraft()
  124.             ? $beforeEvent->getContentTypeDraft()
  125.             : $this->innerService->createContentType($contentTypeCreateStruct$contentTypeGroups);
  126.         $this->eventDispatcher->dispatch(
  127.             new CreateContentTypeEvent($contentTypeDraft, ...$eventData)
  128.         );
  129.         return $contentTypeDraft;
  130.     }
  131.     public function createContentTypeDraft(ContentType $contentType): ContentTypeDraft
  132.     {
  133.         $eventData = [$contentType];
  134.         $beforeEvent = new BeforeCreateContentTypeDraftEvent(...$eventData);
  135.         $this->eventDispatcher->dispatch($beforeEvent);
  136.         if ($beforeEvent->isPropagationStopped()) {
  137.             return $beforeEvent->getContentTypeDraft();
  138.         }
  139.         $contentTypeDraft $beforeEvent->hasContentTypeDraft()
  140.             ? $beforeEvent->getContentTypeDraft()
  141.             : $this->innerService->createContentTypeDraft($contentType);
  142.         $this->eventDispatcher->dispatch(
  143.             new CreateContentTypeDraftEvent($contentTypeDraft, ...$eventData)
  144.         );
  145.         return $contentTypeDraft;
  146.     }
  147.     public function updateContentTypeDraft(
  148.         ContentTypeDraft $contentTypeDraft,
  149.         ContentTypeUpdateStruct $contentTypeUpdateStruct
  150.     ): void {
  151.         $eventData = [
  152.             $contentTypeDraft,
  153.             $contentTypeUpdateStruct,
  154.         ];
  155.         $beforeEvent = new BeforeUpdateContentTypeDraftEvent(...$eventData);
  156.         $this->eventDispatcher->dispatch($beforeEvent);
  157.         if ($beforeEvent->isPropagationStopped()) {
  158.             return;
  159.         }
  160.         $this->innerService->updateContentTypeDraft($contentTypeDraft$contentTypeUpdateStruct);
  161.         $this->eventDispatcher->dispatch(
  162.             new UpdateContentTypeDraftEvent(...$eventData)
  163.         );
  164.     }
  165.     public function deleteContentType(ContentType $contentType): void
  166.     {
  167.         $eventData = [$contentType];
  168.         $beforeEvent = new BeforeDeleteContentTypeEvent(...$eventData);
  169.         $this->eventDispatcher->dispatch($beforeEvent);
  170.         if ($beforeEvent->isPropagationStopped()) {
  171.             return;
  172.         }
  173.         $this->innerService->deleteContentType($contentType);
  174.         $this->eventDispatcher->dispatch(
  175.             new DeleteContentTypeEvent(...$eventData)
  176.         );
  177.     }
  178.     public function copyContentType(
  179.         ContentType $contentType,
  180.         User $creator null
  181.     ): ContentType {
  182.         $eventData = [
  183.             $contentType,
  184.             $creator,
  185.         ];
  186.         $beforeEvent = new BeforeCopyContentTypeEvent(...$eventData);
  187.         $this->eventDispatcher->dispatch($beforeEvent);
  188.         if ($beforeEvent->isPropagationStopped()) {
  189.             return $beforeEvent->getContentTypeCopy();
  190.         }
  191.         $contentTypeCopy $beforeEvent->hasContentTypeCopy()
  192.             ? $beforeEvent->getContentTypeCopy()
  193.             : $this->innerService->copyContentType($contentType$creator);
  194.         $this->eventDispatcher->dispatch(
  195.             new CopyContentTypeEvent($contentTypeCopy, ...$eventData)
  196.         );
  197.         return $contentTypeCopy;
  198.     }
  199.     public function assignContentTypeGroup(
  200.         ContentType $contentType,
  201.         ContentTypeGroup $contentTypeGroup
  202.     ): void {
  203.         $eventData = [
  204.             $contentType,
  205.             $contentTypeGroup,
  206.         ];
  207.         $beforeEvent = new BeforeAssignContentTypeGroupEvent(...$eventData);
  208.         $this->eventDispatcher->dispatch($beforeEvent);
  209.         if ($beforeEvent->isPropagationStopped()) {
  210.             return;
  211.         }
  212.         $this->innerService->assignContentTypeGroup($contentType$contentTypeGroup);
  213.         $this->eventDispatcher->dispatch(
  214.             new AssignContentTypeGroupEvent(...$eventData)
  215.         );
  216.     }
  217.     public function unassignContentTypeGroup(
  218.         ContentType $contentType,
  219.         ContentTypeGroup $contentTypeGroup
  220.     ): void {
  221.         $eventData = [
  222.             $contentType,
  223.             $contentTypeGroup,
  224.         ];
  225.         $beforeEvent = new BeforeUnassignContentTypeGroupEvent(...$eventData);
  226.         $this->eventDispatcher->dispatch($beforeEvent);
  227.         if ($beforeEvent->isPropagationStopped()) {
  228.             return;
  229.         }
  230.         $this->innerService->unassignContentTypeGroup($contentType$contentTypeGroup);
  231.         $this->eventDispatcher->dispatch(
  232.             new UnassignContentTypeGroupEvent(...$eventData)
  233.         );
  234.     }
  235.     public function addFieldDefinition(
  236.         ContentTypeDraft $contentTypeDraft,
  237.         FieldDefinitionCreateStruct $fieldDefinitionCreateStruct
  238.     ): void {
  239.         $eventData = [
  240.             $contentTypeDraft,
  241.             $fieldDefinitionCreateStruct,
  242.         ];
  243.         $beforeEvent = new BeforeAddFieldDefinitionEvent(...$eventData);
  244.         $this->eventDispatcher->dispatch($beforeEvent);
  245.         if ($beforeEvent->isPropagationStopped()) {
  246.             return;
  247.         }
  248.         $this->innerService->addFieldDefinition($contentTypeDraft$fieldDefinitionCreateStruct);
  249.         $this->eventDispatcher->dispatch(
  250.             new AddFieldDefinitionEvent(...$eventData)
  251.         );
  252.     }
  253.     public function removeFieldDefinition(
  254.         ContentTypeDraft $contentTypeDraft,
  255.         FieldDefinition $fieldDefinition
  256.     ): void {
  257.         $eventData = [
  258.             $contentTypeDraft,
  259.             $fieldDefinition,
  260.         ];
  261.         $beforeEvent = new BeforeRemoveFieldDefinitionEvent(...$eventData);
  262.         $this->eventDispatcher->dispatch($beforeEvent);
  263.         if ($beforeEvent->isPropagationStopped()) {
  264.             return;
  265.         }
  266.         $this->innerService->removeFieldDefinition($contentTypeDraft$fieldDefinition);
  267.         $this->eventDispatcher->dispatch(
  268.             new RemoveFieldDefinitionEvent(...$eventData)
  269.         );
  270.     }
  271.     public function updateFieldDefinition(
  272.         ContentTypeDraft $contentTypeDraft,
  273.         FieldDefinition $fieldDefinition,
  274.         FieldDefinitionUpdateStruct $fieldDefinitionUpdateStruct
  275.     ): void {
  276.         $eventData = [
  277.             $contentTypeDraft,
  278.             $fieldDefinition,
  279.             $fieldDefinitionUpdateStruct,
  280.         ];
  281.         $beforeEvent = new BeforeUpdateFieldDefinitionEvent(...$eventData);
  282.         $this->eventDispatcher->dispatch($beforeEvent);
  283.         if ($beforeEvent->isPropagationStopped()) {
  284.             return;
  285.         }
  286.         $this->innerService->updateFieldDefinition($contentTypeDraft$fieldDefinition$fieldDefinitionUpdateStruct);
  287.         $this->eventDispatcher->dispatch(
  288.             new UpdateFieldDefinitionEvent(...$eventData)
  289.         );
  290.     }
  291.     public function publishContentTypeDraft(ContentTypeDraft $contentTypeDraft): void
  292.     {
  293.         $eventData = [$contentTypeDraft];
  294.         $beforeEvent = new BeforePublishContentTypeDraftEvent(...$eventData);
  295.         $this->eventDispatcher->dispatch($beforeEvent);
  296.         if ($beforeEvent->isPropagationStopped()) {
  297.             return;
  298.         }
  299.         $this->innerService->publishContentTypeDraft($contentTypeDraft);
  300.         $this->eventDispatcher->dispatch(
  301.             new PublishContentTypeDraftEvent(...$eventData)
  302.         );
  303.     }
  304.     public function removeContentTypeTranslation(
  305.         ContentTypeDraft $contentTypeDraft,
  306.         string $languageCode
  307.     ): ContentTypeDraft {
  308.         $eventData = [
  309.             $contentTypeDraft,
  310.             $languageCode,
  311.         ];
  312.         $beforeEvent = new BeforeRemoveContentTypeTranslationEvent(...$eventData);
  313.         $this->eventDispatcher->dispatch($beforeEvent);
  314.         if ($beforeEvent->isPropagationStopped()) {
  315.             return $beforeEvent->getNewContentTypeDraft();
  316.         }
  317.         $newContentTypeDraft $beforeEvent->hasNewContentTypeDraft()
  318.             ? $beforeEvent->getNewContentTypeDraft()
  319.             : $this->innerService->removeContentTypeTranslation($contentTypeDraft$languageCode);
  320.         $this->eventDispatcher->dispatch(
  321.             new RemoveContentTypeTranslationEvent($newContentTypeDraft, ...$eventData)
  322.         );
  323.         return $newContentTypeDraft;
  324.     }
  325. }