SwitchToNewAndRemoveOldIndex::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 3
rs 9.9332
1
<?php
2
3
namespace Matchish\ScoutElasticSearch\Jobs\Stages;
4
5
use Elasticsearch\Client;
6
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
7
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Get;
8
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Update;
9
use Matchish\ScoutElasticSearch\Searchable\ImportSource;
10
11
/**
12
 * @internal
13
 */
14
final class SwitchToNewAndRemoveOldIndex
15
{
16
    /**
17
     * @var ImportSource
18
     */
19
    private $source;
20
    /**
21
     * @var Index
22
     */
23
    private $index;
24
25
    /**
26
     * @param ImportSource $source
27
     * @param Index $index
28
     */
29 16
    public function __construct(ImportSource $source, Index $index)
30
    {
31 16
        $this->source = $source;
32 16
        $this->index = $index;
33 16
    }
34
35 14
    public function handle(Client $elasticsearch): void
36
    {
37 14
        $source = $this->source;
38 14
        $params = Get::anyIndex($source->searchableAs());
39 14
        $response = $elasticsearch->indices()->getAlias($params->toArray());
40
41 14
        $params = new Update();
42 14
        foreach ($response as $indexName => $alias) {
43 14
            if ($indexName != $this->index->name()) {
44 2
                $params->removeIndex((string) $indexName);
45
            } else {
46 14
                $params->add((string) $indexName, $source->searchableAs());
47
            }
48
        }
49 14
        $elasticsearch->indices()->updateAliases($params->toArray());
50 14
    }
51
52 13
    public function estimate(): int
53
    {
54 13
        return 1;
55
    }
56
57 13
    public function title(): string
58
    {
59 13
        return 'Switching to the new index';
60
    }
61
}
62