Completed
Push — master ( 953dd8...277a69 )
by Colin
05:01 queued 03:46
created

AliasRemoveIndexCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 103
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 53 4
B argumentsAreValid() 0 26 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cviebrock\LaravelElasticsearch\Console\Command;
6
7
use Elasticsearch\Client;
8
use Illuminate\Console\Command;
9
use Throwable;
10
11
final class AliasRemoveIndexCommand extends Command
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $signature = 'laravel-elasticsearch:utils:alias-remove-index
17
                            {index-name : The index name}
18
                            {alias-name : The alias name}';
19
20
    /**
21
     * @var Client
22
     */
23
    private $client;
24
25
    public function __construct(
26
        Client $client
27
    ) {
28
        $this->client = $client;
29
30
        parent::__construct();
31
    }
32
33
    public function handle(): int
34
    {
35
        $indexName = $this->argument('index-name');
36
        $aliasName = $this->argument('alias-name');
37
38
        if (!$this->argumentsAreValid(
39
            $indexName,
40
            $aliasName
41
        )) {
42
            return self::FAILURE;
43
        }
44
45
        if (!$this->client->indices()->exists([
46
            'index' => $indexName,
47
        ])) {
48
            $this->output->writeln(
49
                sprintf(
50
                    '<error>Index %s doesn\'t exists and cannot be removed from alias.</error>',
51
                    $indexName
52
                )
53
            );
54
55
            return self::FAILURE;
56
        }
57
58
        try {
59
            $this->client->indices()->deleteAlias([
60
                'index' => $indexName,
61
                'name' => $aliasName,
62
            ]);
63
        } catch (Throwable $exception) {
64
            $this->output->writeln(
65
                sprintf(
66
                    '<error>Error removing index %s from alias %s, exception message: %s.</error>',
67
                    $indexName,
68
                    $aliasName,
69
                    $exception->getMessage()
70
                )
71
            );
72
73
            return self::FAILURE;
74
        }
75
76
        $this->output->writeln(
77
            sprintf(
78
                '<info>Index %s removed from alias %s.</info>',
79
                $indexName,
80
                $aliasName
81
            )
82
        );
83
84
        return self::SUCCESS;
85
    }
86
87
    private function argumentsAreValid($indexName, $aliasName): bool
88
    {
89
        if ($indexName === null ||
90
            !is_string($indexName) ||
91
            mb_strlen($indexName) === 0
92
        ) {
93
            $this->output->writeln(
94
                '<error>Argument index-name must be a non empty string.</error>'
95
            );
96
97
            return false;
98
        }
99
100
        if ($aliasName === null ||
101
            !is_string($aliasName) ||
102
            mb_strlen($aliasName) === 0
103
        ) {
104
            $this->output->writeln(
105
                '<error>Argument alias-name must be a non empty string.</error>'
106
            );
107
108
            return false;
109
        }
110
111
        return true;
112
    }
113
}
114