Passed
Push — trunk ( ff423b...970276 )
by Christian
13:19 queued 19s
created

ElasticsearchIndexingUtils::parseJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
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));
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Elasticsearch\P...ustomFieldsMappingEvent has been deprecated: tag:v6.6.0 - Will be removed, use \Shopware\Elasticsearch\Product\Event\ElasticsearchCustomFieldsMappingEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
            $this->eventDispatcher->dispatch(/** @scrutinizer ignore-deprecated */ new ElasticsearchProductCustomFieldsMappingEvent($mappings, $context));
Loading history...
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