vendor/ibexa/product-catalog/src/bundle/EventSubscriber/ProductSpecificationTranslatabilitySubscriber.php line 59

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 Ibexa\Bundle\ProductCatalog\EventSubscriber;
  8. use Ibexa\Contracts\Core\Repository\Events\ContentType\AddFieldDefinitionEvent;
  9. use Ibexa\Contracts\Core\Repository\Events\ContentType\BeforeCreateContentTypeEvent;
  10. use Ibexa\Contracts\Core\Repository\Events\ContentType\BeforeUpdateContentTypeDraftEvent;
  11. use Ibexa\ProductCatalog\Exception\BadStateException;
  12. use Ibexa\ProductCatalog\FieldType\ProductSpecification\Type;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. final class ProductSpecificationTranslatabilitySubscriber implements EventSubscriberInterface
  15. {
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             BeforeCreateContentTypeEvent::class => 'onBeforeCreateContentType',
  20.             BeforeUpdateContentTypeDraftEvent::class => 'onBeforeUpdateContentTypeDraft',
  21.             AddFieldDefinitionEvent::class => 'onAddProductSpecificationFieldDefinition',
  22.         ];
  23.     }
  24.     /**
  25.      * @throws \Ibexa\ProductCatalog\Exception\BadStateException
  26.      */
  27.     public function onBeforeCreateContentType(BeforeCreateContentTypeEvent $event): void
  28.     {
  29.         foreach ($event->getContentTypeCreateStruct()->fieldDefinitions as $fieldDefinition) {
  30.             if (
  31.                 $fieldDefinition->fieldTypeIdentifier === Type::FIELD_TYPE_IDENTIFIER
  32.                 && $fieldDefinition->isTranslatable === true
  33.             ) {
  34.                 throw $this->getBadStateException();
  35.             }
  36.         }
  37.     }
  38.     /**
  39.      * @throws \Ibexa\ProductCatalog\Exception\BadStateException
  40.      */
  41.     public function onBeforeUpdateContentTypeDraft(BeforeUpdateContentTypeDraftEvent $event): void
  42.     {
  43.         foreach ($event->getContentTypeDraft()->fieldDefinitions as $fieldDefinition) {
  44.             if (
  45.                 $fieldDefinition->fieldTypeIdentifier === Type::FIELD_TYPE_IDENTIFIER
  46.                 && $fieldDefinition->isTranslatable === true
  47.             ) {
  48.                 throw $this->getBadStateException();
  49.             }
  50.         }
  51.     }
  52.     public function onAddProductSpecificationFieldDefinition(AddFieldDefinitionEvent $event): void
  53.     {
  54.         $fieldDefinition $event->getFieldDefinitionCreateStruct();
  55.         if ($fieldDefinition->fieldTypeIdentifier !== Type::FIELD_TYPE_IDENTIFIER) {
  56.             return;
  57.         }
  58.         if ($fieldDefinition->isTranslatable === true) {
  59.             throw $this->getBadStateException();
  60.         }
  61.     }
  62.     private function getBadStateException(): BadStateException
  63.     {
  64.         return new BadStateException(
  65.             '$fieldDefinition',
  66.             'Product Specification FieldType cannot be translatable'
  67.         );
  68.     }
  69. }