ElasticsearchIndexFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Locastic\Loggastic\Bridge\Elasticsearch\Index;
4
5
use Locastic\Loggastic\Bridge\Elasticsearch\Context\ElasticsearchContextFactoryInterface;
6
use Locastic\Loggastic\Bridge\Elasticsearch\ElasticsearchClient;
7
8
final class ElasticsearchIndexFactory implements ElasticsearchIndexFactoryInterface
9
{
10
    public function __construct(private readonly ElasticsearchClient $elasticsearchClient, private readonly ElasticsearchContextFactoryInterface $elasticsearchContextFactory, private readonly ElasticsearchIndexConfigurationInterface $elasticsearchIndexConfiguration)
11
    {
12
    }
13
14
    public function recreateActivityLogIndex(string $className): void
15
    {
16
        $elasticContext = $this->elasticsearchContextFactory->create($className);
17
        $params = $this->elasticsearchIndexConfiguration->getActivityLogIndexConfig($elasticContext);
18
19
        $this->deleteIndex($elasticContext->getActivityLogIndex());
20
        $this->elasticsearchClient->getClient()->indices()->create($params);
21
    }
22
23
    public function recreateCurrentDataTrackerLogIndex(string $className): void
24
    {
25
        $elasticContext = $this->elasticsearchContextFactory->create($className);
26
        $params = $this->elasticsearchIndexConfiguration->getCurrentDataTrackerIndexConfig($elasticContext);
27
28
        $this->deleteIndex($elasticContext->getCurrentDataTrackerIndex());
29
        $this->elasticsearchClient->getClient()->indices()->create($params);
30
    }
31
32
    public function createActivityLogIndex(string $className): void
33
    {
34
        $elasticContext = $this->elasticsearchContextFactory->create($className);
35
36
        $params = $this->elasticsearchIndexConfiguration->getActivityLogIndexConfig($elasticContext);
37
38
        $this->elasticsearchClient->getClient()->indices()->create($params);
39
    }
40
41
    public function createCurrentDataTrackerLogIndex(string $className): void
42
    {
43
        $elasticContext = $this->elasticsearchContextFactory->create($className);
44
45
        $params = $this->elasticsearchIndexConfiguration->getCurrentDataTrackerIndexConfig($elasticContext);
46
47
        $this->elasticsearchClient->getClient()->indices()->create($params);
48
    }
49
50
    private function deleteIndex(string $index): void
51
    {
52
        try {
53
            $this->elasticsearchClient->getClient()->indices()->delete(['index' => $index]);
54
        } catch (\Exception $e) {
55
            // don't throw exception if index doesn't exist
56
            if (404 !== $e->getCode()) {
57
                throw $e;
58
            }
59
        }
60
    }
61
}
62