vendor/shopware/elasticsearch/Product/CustomFieldUpdater.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Elasticsearch\Product;
  3. use Elasticsearch\Client;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  5. use Shopware\Core\System\CustomField\CustomFieldDefinition;
  6. use Shopware\Core\System\CustomField\CustomFieldTypes;
  7. use Shopware\Elasticsearch\Framework\ElasticsearchHelper;
  8. use Shopware\Elasticsearch\Framework\ElasticsearchOutdatedIndexDetector;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  12.  */
  13. class CustomFieldUpdater implements EventSubscriberInterface
  14. {
  15.     private ElasticsearchOutdatedIndexDetector $indexDetector;
  16.     private Client $client;
  17.     private ElasticsearchHelper $elasticsearchHelper;
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(ElasticsearchOutdatedIndexDetector $indexDetectorClient $clientElasticsearchHelper $elasticsearchHelper)
  22.     {
  23.         $this->indexDetector $indexDetector;
  24.         $this->client $client;
  25.         $this->elasticsearchHelper $elasticsearchHelper;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             EntityWrittenContainerEvent::class => 'onNewCustomFieldCreated',
  31.         ];
  32.     }
  33.     public function onNewCustomFieldCreated(EntityWrittenContainerEvent $containerEvent): void
  34.     {
  35.         $event $containerEvent->getEventByEntityName(CustomFieldDefinition::ENTITY_NAME);
  36.         if ($event === null) {
  37.             return;
  38.         }
  39.         if (!$this->elasticsearchHelper->allowIndexing()) {
  40.             return;
  41.         }
  42.         $newCreatedFields = [];
  43.         foreach ($event->getWriteResults() as $writeResult) {
  44.             $existence $writeResult->getExistence();
  45.             if ($existence && $existence->exists()) {
  46.                 continue;
  47.             }
  48.             /** @var array<mixed> $esType */
  49.             $esType self::getTypeFromCustomFieldType($writeResult->getProperty('type'));
  50.             $newCreatedFields[(string) $writeResult->getProperty('name')] = $esType;
  51.         }
  52.         if (\count($newCreatedFields) === 0) {
  53.             return;
  54.         }
  55.         $this->createNewFieldsInIndices($newCreatedFields);
  56.     }
  57.     /**
  58.      * @deprecated tag:v6.5.0 - Return type will be changed to not nullable - reason:return-type-change
  59.      *
  60.      * @return array<mixed>|null
  61.      */
  62.     public static function getTypeFromCustomFieldType(string $type): ?array
  63.     {
  64.         switch ($type) {
  65.             case CustomFieldTypes::INT:
  66.                 return [
  67.                     'type' => 'long',
  68.                 ];
  69.             case CustomFieldTypes::FLOAT:
  70.                 return [
  71.                     'type' => 'double',
  72.                 ];
  73.             case CustomFieldTypes::BOOL:
  74.                 return [
  75.                     'type' => 'boolean',
  76.                 ];
  77.             case CustomFieldTypes::DATETIME:
  78.                 return [
  79.                     'type' => 'date',
  80.                     'format' => 'yyyy-MM-dd HH:mm:ss.000',
  81.                     'ignore_malformed' => true,
  82.                 ];
  83.             case CustomFieldTypes::PRICE:
  84.             case CustomFieldTypes::JSON:
  85.                 return [
  86.                     'type' => 'object',
  87.                     'dynamic' => true,
  88.                 ];
  89.             case CustomFieldTypes::HTML:
  90.             case CustomFieldTypes::TEXT:
  91.                 return [
  92.                     'type' => 'text',
  93.                 ];
  94.             case CustomFieldTypes::COLORPICKER:
  95.             case CustomFieldTypes::ENTITY:
  96.             case CustomFieldTypes::MEDIA:
  97.             case CustomFieldTypes::SELECT:
  98.             case CustomFieldTypes::SWITCH:
  99.             default:
  100.                 return [
  101.                     'type' => 'keyword',
  102.                 ];
  103.         }
  104.     }
  105.     /**
  106.      * @param array<string, array<mixed>> $newCreatedFields
  107.      */
  108.     private function createNewFieldsInIndices(array $newCreatedFields): void
  109.     {
  110.         $indices $this->indexDetector->getAllUsedIndices();
  111.         foreach ($indices as $indexName) {
  112.             $body = [
  113.                 'properties' => [
  114.                     'customFields' => [
  115.                         'properties' => $newCreatedFields,
  116.                     ],
  117.                 ],
  118.             ];
  119.             // For some reason, we need to include the includes to prevent merge conflicts.
  120.             // This error can happen for example after updating from version <6.4.
  121.             $current $this->client->indices()->get(['index' => $indexName]);
  122.             $includes $current[$indexName]['mappings']['_source']['includes'] ?? [];
  123.             if ($includes !== []) {
  124.                 $body['_source'] = [
  125.                     'includes' => $includes,
  126.                 ];
  127.             }
  128.             $this->client->indices()->putMapping([
  129.                 'index' => $indexName,
  130.                 'body' => $body,
  131.             ]);
  132.         }
  133.     }
  134. }