1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Elasticsearch\Framework; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\Exception; |
7
|
|
|
use Shopware\Core\Content\Product\ProductDefinition; |
8
|
|
|
use Shopware\Core\Framework\Context; |
9
|
|
|
use Shopware\Core\Framework\Feature; |
10
|
|
|
use Shopware\Core\Framework\Log\Package; |
11
|
|
|
use Shopware\Elasticsearch\Event\ElasticsearchCustomFieldsMappingEvent; |
12
|
|
|
use Shopware\Elasticsearch\Product\Event\ElasticsearchProductCustomFieldsMappingEvent; |
13
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @final |
18
|
|
|
*/ |
19
|
|
|
#[Package('buyers-experience')] |
20
|
|
|
class ElasticsearchIndexingUtils |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array<string, array<string, string>> |
24
|
|
|
*/ |
25
|
|
|
private array $customFieldsTypes = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @internal |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
private readonly Connection $connection, |
32
|
|
|
private readonly EventDispatcherInterface $eventDispatcher, |
33
|
|
|
private readonly ParameterBagInterface $parameterBag, |
34
|
|
|
) { |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws Exception |
39
|
|
|
* |
40
|
|
|
* @return array<string, string> |
41
|
|
|
*/ |
42
|
|
|
public function getCustomFieldTypes(string $entity, Context $context): array |
43
|
|
|
{ |
44
|
|
|
if (\array_key_exists($entity, $this->customFieldsTypes)) { |
45
|
|
|
return $this->customFieldsTypes[$entity]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$mappingKey = sprintf('elasticsearch.%s.custom_fields_mapping', $entity); |
49
|
|
|
$customFieldsMapping = $this->parameterBag->has($mappingKey) ? $this->parameterBag->get($mappingKey) : []; |
50
|
|
|
|
51
|
|
|
/** @var array<string, string> $mappings */ |
52
|
|
|
$mappings = $this->connection->fetchAllKeyValue(' |
53
|
|
|
SELECT |
54
|
|
|
custom_field.`name`, |
55
|
|
|
custom_field.type |
56
|
|
|
FROM custom_field_set_relation |
57
|
|
|
INNER JOIN custom_field ON(custom_field.set_id = custom_field_set_relation.set_id) |
58
|
|
|
WHERE custom_field_set_relation.entity_name = :entity |
59
|
|
|
', ['entity' => $entity]) + $customFieldsMapping; |
60
|
|
|
|
61
|
|
|
$event = new ElasticsearchCustomFieldsMappingEvent($entity, $mappings, $context); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @deprecated tag:v6.6.0 - If statement will be removed |
65
|
|
|
*/ |
66
|
|
|
if (!Feature::isActive('v6.6.0.0') && $entity === ProductDefinition::ENTITY_NAME) { |
67
|
|
|
$this->eventDispatcher->dispatch(new ElasticsearchProductCustomFieldsMappingEvent($mappings, $context)); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->eventDispatcher->dispatch($event); |
71
|
|
|
|
72
|
|
|
$this->customFieldsTypes[$entity] = $event->getMappings(); |
73
|
|
|
|
74
|
|
|
return $this->customFieldsTypes[$entity]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @description strip html tags from text and truncate to 32766 characters |
79
|
|
|
*/ |
80
|
|
|
public static function stripText(string $text): string |
81
|
|
|
{ |
82
|
|
|
// Remove all html elements to save up space |
83
|
|
|
$text = strip_tags($text); |
84
|
|
|
|
85
|
|
|
if (mb_strlen($text) >= 32766) { |
86
|
|
|
return mb_substr($text, 0, 32766); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $text; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array<string, string> $record |
94
|
|
|
* |
95
|
|
|
* @throws \JsonException |
96
|
|
|
* |
97
|
|
|
* @return array<mixed> |
98
|
|
|
*/ |
99
|
|
|
public static function parseJson(array $record, string $field): array |
100
|
|
|
{ |
101
|
|
|
if (!\array_key_exists($field, $record)) { |
102
|
|
|
return []; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return json_decode($record[$field] ?? '[]', true, 512, \JSON_THROW_ON_ERROR); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|