Elasticsearch7MigrationAdapter::getIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/elasticsearch7-adapter project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Elasticsearch7\Migration;
10
11
use Daikon\Dbal\Connector\ConnectorInterface;
12
use Daikon\Dbal\Migration\MigrationAdapterInterface;
13
use Daikon\Dbal\Migration\MigrationList;
14
use Daikon\Elasticsearch7\Connector\Elasticsearch7Connector;
15
use DateTimeImmutable;
16
use Elasticsearch\Common\Exceptions\Missing404Exception;
17
18
final class Elasticsearch7MigrationAdapter implements MigrationAdapterInterface
19
{
20
    private Elasticsearch7Connector $connector;
21
22
    private array $settings;
23
24
    public function __construct(Elasticsearch7Connector $connector, array $settings = [])
25
    {
26
        $this->connector = $connector;
27
        $this->settings = $settings;
28
    }
29
30
    public function read(string $identifier): MigrationList
31
    {
32
        $client = $this->connector->getConnection();
33
34
        try {
35
            $result = $client->get([
36
                'index' => $this->getIndex(),
37
                'id' => $identifier
38
            ]);
39
        } catch (Missing404Exception $error) {
40
            return new MigrationList;
41
        }
42
43
        return $this->createMigrationList($result['_source']['migrations']);
44
    }
45
46
    public function write(string $identifier, MigrationList $migrationList): void
47
    {
48
        if ($migrationList->isEmpty()) {
49
            return;
50
        }
51
52
        $client = $this->connector->getConnection();
53
        $client->index([
54
            'index' => $this->getIndex(),
55
            'id' => $identifier,
56
            'body' => [
57
                'target' => $identifier,
58
                'migrations' => $migrationList->toNative()
59
            ]
60
        ]);
61
    }
62
63
    public function getConnector(): ConnectorInterface
64
    {
65
        return $this->connector;
66
    }
67
68
    private function createMigrationList(array $migrationData): MigrationList
69
    {
70
        $migrations = [];
71
        foreach ($migrationData as $migration) {
72
            $migrationClass = $migration['@type'];
73
            $migrations[] = new $migrationClass(new DateTimeImmutable($migration['executedAt']));
74
        }
75
        return (new MigrationList($migrations))->sortByVersion();
76
    }
77
78
    private function getIndex(): string
79
    {
80
        return $this->settings['index'] ?? $this->connector->getSettings()['index'];
81
    }
82
}
83