Completed
Push — master ( b17b5c...953dd8 )
by Colin
03:13 queued 02:01
created

AliasCreateCommand::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 9.0254
c 0
b 0
f 0
cc 4
nc 4
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 AliasCreateCommand extends Command
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $signature = 'laravel-elasticsearch:utils:alias-create
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 alias cannot be created.</error>',
51
                    $indexName
52
                )
53
            );
54
55
            return self::FAILURE;
56
        }
57
58
        try {
59
            $this->client->indices()->putAlias([
60
                'index' => $indexName,
61
                'name' => $aliasName,
62
            ]);
63
        } catch (Throwable $exception) {
64
            $this->output->writeln(
65
                sprintf(
66
                    '<error>Error creating alias %s for index %s, exception message: %s.</error>',
67
                    $aliasName,
68
                    $indexName,
69
                    $exception->getMessage()
70
                )
71
            );
72
73
            return self::FAILURE;
74
        }
75
76
        $this->output->writeln(
77
            sprintf(
78
                '<info>Alias %s created for index %s.</info>',
79
                $aliasName,
80
                $indexName
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