IndexCreator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createAliasIfNotExisting() 0 13 2
A indexExists() 0 3 1
A aliasExists() 0 3 1
A createIndex() 0 27 2
A __construct() 0 17 6
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Elasticsearch\Framework\Indexing;
4
5
use OpenSearch\Client;
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use Shopware\Core\Framework\Context;
8
use Shopware\Core\Framework\Log\Package;
9
use Shopware\Elasticsearch\Framework\AbstractElasticsearchDefinition;
10
use Shopware\Elasticsearch\Framework\Indexing\Event\ElasticsearchIndexConfigEvent;
11
use Shopware\Elasticsearch\Framework\Indexing\Event\ElasticsearchIndexCreatedEvent;
12
13
#[Package('core')]
14
class IndexCreator
15
{
16
    /**
17
     * @var array<mixed>
18
     */
19
    private readonly array $config;
20
21
    /**
22
     * @internal
23
     *
24
     * @param array<mixed> $config
25
     */
26
    public function __construct(
27
        private readonly Client $client,
28
        array $config,
29
        private readonly IndexMappingProvider $mappingProvider,
30
        private readonly EventDispatcherInterface $eventDispatcher
31
    ) {
32
        if (isset($config['settings']['index'])) {
33
            if (\array_key_exists('number_of_shards', $config['settings']['index']) && $config['settings']['index']['number_of_shards'] === null) {
34
                unset($config['settings']['index']['number_of_shards']);
35
            }
36
37
            if (\array_key_exists('number_of_replicas', $config['settings']['index']) && $config['settings']['index']['number_of_replicas'] === null) {
38
                unset($config['settings']['index']['number_of_replicas']);
39
            }
40
        }
41
42
        $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config is declared read-only in Shopware\Elasticsearch\F...k\Indexing\IndexCreator.
Loading history...
43
    }
44
45
    public function createIndex(AbstractElasticsearchDefinition $definition, string $index, string $alias, Context $context): void
46
    {
47
        // NEXT-21735 - does not execute if there's no index yet
48
        // @codeCoverageIgnoreStart
49
        if ($this->indexExists($index)) {
50
            $this->client->indices()->delete(['index' => $index]);
51
        }
52
        // @codeCoverageIgnoreEnd
53
54
        $mapping = $this->mappingProvider->build($definition, $context);
55
56
        $body = array_merge(
57
            $this->config,
58
            ['mappings' => $mapping]
59
        );
60
61
        $event = new ElasticsearchIndexConfigEvent($index, $body, $definition, $context);
62
        $this->eventDispatcher->dispatch($event);
63
64
        $this->client->indices()->create([
65
            'index' => $index,
66
            'body' => $event->getConfig(),
67
        ]);
68
69
        $this->createAliasIfNotExisting($index, $alias);
70
71
        $this->eventDispatcher->dispatch(new ElasticsearchIndexCreatedEvent($index, $definition));
72
    }
73
74
    public function aliasExists(string $alias): bool
75
    {
76
        return $this->client->indices()->existsAlias(['name' => $alias]);
77
    }
78
79
    private function indexExists(string $index): bool
80
    {
81
        return $this->client->indices()->exists(['index' => $index]);
82
    }
83
84
    private function createAliasIfNotExisting(string $index, string $alias): void
85
    {
86
        $exist = $this->client->indices()->existsAlias(['name' => $alias]);
87
88
        if ($exist) {
89
            return;
90
        }
91
92
        $this->client->indices()->refresh([
93
            'index' => $index,
94
        ]);
95
96
        $this->client->indices()->putAlias(['index' => $index, 'name' => $alias]);
97
    }
98
}
99