Completed
Pull Request — master (#98)
by
unknown
01:42
created

AliasSwitchIndexCommand::handle()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 8.829
c 0
b 0
f 0
cc 4
nc 5
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 AliasSwitchIndexCommand extends Command
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $signature = 'laravel-elasticsearch:utils:alias-switch-index
17
                            {new-index-name : The new index name}
18
                            {old-index-name : The old index name}
19
                            {alias-name : The alias name}';
20
21
    /**
22
     * @var Client
23
     */
24
    private $client;
25
26
    public function __construct(
27
        Client $client
28
    ) {
29
        $this->client = $client;
30
31
        parent::__construct();
32
    }
33
34
    public function handle(): int
35
    {
36
        $newIndexName = $this->argument('new-index-name');
37
        $oldIndexName = $this->argument('old-index-name');
38
        $aliasName = $this->argument('alias-name');
39
40
        if (!$this->argumentsAreValid(
41
            $newIndexName,
42
            $oldIndexName,
43
            $aliasName
44
        )) {
45
            return self::FAILURE;
46
        }
47
48
        if (!$this->client->indices()->exists([
49
            'index' => $newIndexName,
50
        ])) {
51
            $this->output->writeln(
52
                sprintf(
53
                    '<error>Index %s cannot be linked to alias because doesn\'t exists.</error>',
54
                    $newIndexName
55
                )
56
            );
57
58
            return self::FAILURE;
59
        }
60
61
        try {
62
            $this->client->indices()->putAlias([
63
                'index' => $newIndexName,
64
                'name' => $aliasName,
65
            ]);
66
67
            $this->client->indices()->deleteAlias([
68
                'index' => $oldIndexName,
69
                'name' => $aliasName,
70
            ]);
71
        } catch (Throwable $exception) {
72
            $this->output->writeln(
73
                sprintf(
74
                    '<error>Error switching indexes - new index: %s, old index: %s in alias %s, exception message: %s.</error>',
75
                    $newIndexName,
76
                    $oldIndexName,
77
                    $aliasName,
78
                    $exception->getMessage()
79
                )
80
            );
81
82
            return self::FAILURE;
83
        }
84
85
        $this->output->writeln(
86
            sprintf(
87
                '<info>New index %s linked and old index %s removed from alias %s.</info>',
88
                $newIndexName,
89
                $oldIndexName,
90
                $aliasName
91
            )
92
        );
93
94
        return self::SUCCESS;
95
    }
96
97
    private function argumentsAreValid(
98
        $newIndexName,
99
        $oldIndexName,
100
        $aliasName
101
    ): bool {
102
        if ($newIndexName === null ||
103
            !is_string($newIndexName) ||
104
            mb_strlen($newIndexName) === 0
105
        ) {
106
            $this->output->writeln(
107
                '<error>Argument new-index-name must be a non empty string.</error>'
108
            );
109
110
            return false;
111
        }
112
113
        if ($oldIndexName === null ||
114
            !is_string($oldIndexName) ||
115
            mb_strlen($oldIndexName) === 0
116
        ) {
117
            $this->output->writeln(
118
                '<error>Argument old-index-name must be a non empty string.</error>'
119
            );
120
121
            return false;
122
        }
123
124
        if ($aliasName === null ||
125
            !is_string($aliasName) ||
126
            mb_strlen($aliasName) === 0
127
        ) {
128
            $this->output->writeln(
129
                '<error>Argument alias-name must be a non empty string.</error>'
130
            );
131
132
            return false;
133
        }
134
135
        return true;
136
    }
137
}
138