vendor/ibexa/product-catalog/src/bundle/EventSubscriber/ProductPublishEventSubscriber.php line 36

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\Content\PublishVersionEvent;
  9. use Ibexa\ProductCatalog\FieldType\ProductSpecification\Type as ProductSpecificationType;
  10. use Ibexa\ProductCatalog\FieldType\ProductSpecification\Value;
  11. use Ibexa\ProductCatalog\Local\Persistence\Legacy\Product\GatewayInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Webmozart\Assert\Assert;
  14. final class ProductPublishEventSubscriber implements EventSubscriberInterface
  15. {
  16. private GatewayInterface $productGateway;
  17. public function __construct(GatewayInterface $productGateway)
  18. {
  19. $this->productGateway = $productGateway;
  20. }
  21. public static function getSubscribedEvents(): array
  22. {
  23. return [
  24. PublishVersionEvent::class => [
  25. 'onProductPublish',
  26. ],
  27. ];
  28. }
  29. public function onProductPublish(PublishVersionEvent $event): void
  30. {
  31. $content = $event->getContent();
  32. foreach ($content->getFields() as $field) {
  33. if ($field->getFieldTypeIdentifier() !== ProductSpecificationType::FIELD_TYPE_IDENTIFIER) {
  34. continue;
  35. }
  36. $value = $field->getValue();
  37. Assert::isInstanceOf($value, Value::class);
  38. $originalCode = $value->getOriginalCode();
  39. Assert::string($originalCode, sprintf(
  40. '%s object is incomplete. Original code is null.',
  41. Value::class,
  42. ));
  43. $this->productGateway->update(
  44. $originalCode,
  45. $value->getCode(),
  46. true,
  47. );
  48. }
  49. }
  50. }