vendor/ibexa/measurement/src/bundle/EventSubscriber/Migrations/ProductAttributeConversionSubscriber.php line 46

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\Measurement\EventSubscriber\Migrations;
  8. use Ibexa\Contracts\Measurement\MeasurementServiceInterface;
  9. use Ibexa\Contracts\Measurement\Value\ValueInterface;
  10. use Ibexa\Contracts\ProductCatalog\AttributeDefinitionServiceInterface;
  11. use Ibexa\Migration\Event\FieldValueFromHashEvent;
  12. use Ibexa\Migration\Event\MigrationEvents;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Webmozart\Assert\Assert;
  15. final class ProductAttributeConversionSubscriber implements EventSubscriberInterface
  16. {
  17.     private MeasurementServiceInterface $measurementService;
  18.     private AttributeDefinitionServiceInterface $attributeDefinitionService;
  19.     public function __construct(
  20.         MeasurementServiceInterface $measurementService,
  21.         AttributeDefinitionServiceInterface $attributeDefinitionService
  22.     ) {
  23.         $this->measurementService $measurementService;
  24.         $this->attributeDefinitionService $attributeDefinitionService;
  25.     }
  26.     /**
  27.      * @return array<string, array{string, int}>
  28.      */
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             MigrationEvents::BEFORE_FIELD_VALUE_FROM_HASH => [
  33.                 'convertScalarHashIntoObjects',
  34.                 -100,
  35.             ],
  36.         ];
  37.     }
  38.     public function convertScalarHashIntoObjects(FieldValueFromHashEvent $event): void
  39.     {
  40.         $hash $event->getHash();
  41.         $fieldTypeIdentifier $event->getFieldTypeIdentifier();
  42.         if ($fieldTypeIdentifier !== 'ibexa_product_specification') {
  43.             return;
  44.         }
  45.         if ($hash === null) {
  46.             return;
  47.         }
  48.         Assert::isArray($hash);
  49.         if (!isset($hash['attributes'])) {
  50.             return;
  51.         }
  52.         Assert::isArray($hash['attributes']);
  53.         foreach ($hash['attributes'] as $attributeDefinitionIdentifier => $attributeData) {
  54.             if ($attributeData instanceof ValueInterface) {
  55.                 continue;
  56.             }
  57.             if (!is_array($attributeData)) {
  58.                 continue;
  59.             }
  60.             $attributeDefinition $this->attributeDefinitionService->getAttributeDefinition(
  61.                 (string)$attributeDefinitionIdentifier
  62.             );
  63.             $measurementTypeIdentifier $attributeDefinition->getType()->getIdentifier();
  64.             switch ($measurementTypeIdentifier) {
  65.                 case 'measurement_range':
  66.                     Assert::keyExists($attributeData'measurementType');
  67.                     $measurementType $attributeData['measurementType'];
  68.                     Assert::keyExists($attributeData'measurementRangeMinimumValue');
  69.                     $minimumValue $attributeData['measurementRangeMinimumValue'];
  70.                     Assert::numeric($minimumValue);
  71.                     $minimumValue = (float)$minimumValue;
  72.                     Assert::keyExists($attributeData'measurementRangeMaximumValue');
  73.                     $maximumValue $attributeData['measurementRangeMaximumValue'];
  74.                     Assert::numeric($maximumValue);
  75.                     $maximumValue = (float)$maximumValue;
  76.                     Assert::keyExists($attributeData'measurementUnit');
  77.                     $measurementUnit $attributeData['measurementUnit'];
  78.                     $measurement $this->measurementService->buildRangeValue(
  79.                         $measurementType,
  80.                         $minimumValue,
  81.                         $maximumValue,
  82.                         $measurementUnit,
  83.                     );
  84.                     $hash['attributes'][$attributeDefinitionIdentifier] = $measurement;
  85.                     break;
  86.                 case 'measurement_single':
  87.                     Assert::keyExists($attributeData'measurementType');
  88.                     $measurementType $attributeData['measurementType'];
  89.                     Assert::keyExists($attributeData'value');
  90.                     $value $attributeData['value'];
  91.                     Assert::numeric($value);
  92.                     $value = (float)$value;
  93.                     Assert::keyExists($attributeData'measurementUnit');
  94.                     $measurementUnit $attributeData['measurementUnit'];
  95.                     $measurement $this->measurementService->buildSimpleValue(
  96.                         $measurementType,
  97.                         $value,
  98.                         $measurementUnit,
  99.                     );
  100.                     $hash['attributes'][$attributeDefinitionIdentifier] = $measurement;
  101.                     break;
  102.             }
  103.         }
  104.         $event->setHash($hash);
  105.     }
  106. }