Passed
Push — trunk ( 325349...f71779 )
by Christian
12:37 queued 16s
created

LanguageSubscriber   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSubscribedEvents() 0 10 2
A onLanguageWritten() 0 25 6
A onSalesChannelWritten() 0 25 6
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Elasticsearch\Product;
4
5
use OpenSearch\Client;
6
use Shopware\Core\Content\Product\ProductDefinition;
7
use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
8
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
9
use Shopware\Core\Framework\Feature;
10
use Shopware\Core\Framework\Log\Package;
11
use Shopware\Elasticsearch\Framework\ElasticsearchHelper;
12
use Shopware\Elasticsearch\Framework\ElasticsearchRegistry;
13
use Shopware\Elasticsearch\Framework\Indexing\ElasticsearchLanguageIndexIteratorMessage;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\Messenger\MessageBusInterface;
16
17
/**
18
 * @internal
19
 * When an language is created, we need to trigger an indexing for that
20
 */
21
#[Package('core')]
22
class LanguageSubscriber implements EventSubscriberInterface
23
{
24
    public function __construct(
25
        private readonly ElasticsearchHelper $elasticsearchHelper,
26
        private readonly ElasticsearchRegistry $registry,
27
        private readonly Client $client,
28
        private readonly ProductDefinition $productDefinition,
29
        private readonly MessageBusInterface $bus
30
    ) {
31
    }
32
33
    public static function getSubscribedEvents(): array
34
    {
35
        if (Feature::isActive('ES_MULTILINGUAL_INDEX')) {
36
            return [
37
                'language.written' => 'onLanguageWritten',
38
            ];
39
        }
40
41
        return [
42
            'sales_channel_language.written' => 'onSalesChannelWritten',
43
        ];
44
    }
45
46
    /**
47
     * @deprecated tag:v6.6.0 - reason:remove-subscriber -  method will be removed
48
     */
49
    public function onSalesChannelWritten(EntityWrittenEvent $event): void
50
    {
51
        if (Feature::isActive('ES_MULTILINGUAL_INDEX')) {
52
            return;
53
        }
54
55
        if (!$this->elasticsearchHelper->allowIndexing()) {
56
            return;
57
        }
58
59
        foreach ($event->getWriteResults() as $writeResult) {
60
            if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
61
                continue;
62
            }
63
64
            $languageId = $writeResult->getProperty('languageId');
65
66
            $esIndex = $this->elasticsearchHelper->getIndexName($this->productDefinition, $languageId);
0 ignored issues
show
Deprecated Code introduced by
The function Shopware\Elasticsearch\F...hHelper::getIndexName() has been deprecated: tag:v6.6.0 - reason:new-optional-parameter - Parameter $languageId will be removed. ( Ignorable by Annotation )

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

66
            $esIndex = /** @scrutinizer ignore-deprecated */ $this->elasticsearchHelper->getIndexName($this->productDefinition, $languageId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
67
68
            // index exists, don't need to do anything
69
            if ($this->client->indices()->exists(['index' => $esIndex])) {
70
                continue;
71
            }
72
73
            $this->bus->dispatch(new ElasticsearchLanguageIndexIteratorMessage($languageId));
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Elasticsearch\F...ageIndexIteratorMessage has been deprecated: tag:v6.6.0 - will be removed ( Ignorable by Annotation )

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

73
            $this->bus->dispatch(/** @scrutinizer ignore-deprecated */ new ElasticsearchLanguageIndexIteratorMessage($languageId));
Loading history...
74
        }
75
    }
76
77
    public function onLanguageWritten(EntityWrittenEvent $event): void
78
    {
79
        if (!$this->elasticsearchHelper->allowIndexing()) {
80
            return;
81
        }
82
83
        $context = $event->getContext();
84
85
        foreach ($event->getWriteResults() as $writeResult) {
86
            if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
87
                continue;
88
            }
89
90
            foreach ($this->registry->getDefinitions() as $definition) {
91
                $indexName = $this->elasticsearchHelper->getIndexName($definition->getEntityDefinition());
0 ignored issues
show
Deprecated Code introduced by
The function Shopware\Elasticsearch\F...hHelper::getIndexName() has been deprecated: tag:v6.6.0 - reason:new-optional-parameter - Parameter $languageId will be removed. ( Ignorable by Annotation )

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

91
                $indexName = /** @scrutinizer ignore-deprecated */ $this->elasticsearchHelper->getIndexName($definition->getEntityDefinition());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
92
93
                // index doesn't exist, don't need to do anything
94
                if (!$this->client->indices()->exists(['index' => $indexName])) {
95
                    continue;
96
                }
97
98
                $this->client->indices()->putMapping([
99
                    'index' => $indexName,
100
                    'body' => [
101
                        'properties' => $definition->getMapping($context)['properties'],
102
                    ],
103
                ]);
104
            }
105
        }
106
    }
107
}
108