CleanUp   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 44
ccs 20
cts 20
cp 1
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 16 6
A estimate() 0 3 1
A title() 0 3 1
1
<?php
2
3
namespace Matchish\ScoutElasticSearch\Jobs\Stages;
4
5
use Elasticsearch\Client;
6
use Elasticsearch\Common\Exceptions\Missing404Exception;
7
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Get as GetAliasParams;
8
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Delete as DeleteIndexParams;
9
use Matchish\ScoutElasticSearch\Searchable\ImportSource;
10
11
/**
12
 * @internal
13
 */
14
final class CleanUp
15
{
16
    /**
17
     * @var ImportSource
18
     */
19
    private $source;
20
21
    /**
22
     * @param ImportSource $source
23
     */
24 16
    public function __construct(ImportSource $source)
25
    {
26 16
        $this->source = $source;
27 16
    }
28
29 14
    public function handle(Client $elasticsearch): void
30
    {
31 14
        $source = $this->source;
32 14
        $params = GetAliasParams::anyIndex($source->searchableAs());
33
        try {
34
            /** @var array $response */
35 14
            $response = $elasticsearch->indices()->getAlias($params->toArray());
36 13
        } catch (Missing404Exception $e) {
37 13
            $response = [];
38
        }
39 14
        foreach ($response as $indexName => $data) {
40 2
            foreach ($data['aliases'] as $alias => $config) {
41 2
                if (array_key_exists('is_write_index', $config) && $config['is_write_index']) {
42 1
                    $params = new DeleteIndexParams((string) $indexName);
43 1
                    $elasticsearch->indices()->delete($params->toArray());
44 1
                    continue 2;
45
                }
46
            }
47
        }
48 14
    }
49
50 13
    public function title(): string
51
    {
52 13
        return 'Clean up';
53
    }
54
55 13
    public function estimate(): int
56
    {
57 13
        return 1;
58
    }
59
}
60