Completed
Pull Request — master (#96)
by
unknown
01:27
created

AliasCreateCommand::handle()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 7.1208
c 0
b 0
f 0
cc 9
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 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
37
        if ($indexName === null ||
38
            !is_string($indexName) ||
39
            mb_strlen($indexName) === 0
40
        ) {
41
            $this->output->writeln(
42
                '<error>Argument index-name must be a non empty string.</error>'
43
            );
44
45
            return self::FAILURE;
46
        }
47
48
        $aliasName = $this->argument('alias-name');
49
50
        if ($aliasName === null ||
51
            !is_string($aliasName) ||
52
            mb_strlen($aliasName) === 0
53
        ) {
54
            $this->output->writeln(
55
                '<error>Argument alias-name must be a non empty string.</error>'
56
            );
57
58
            return self::FAILURE;
59
        }
60
61
        if (!$this->client->indices()->exists([
62
            'index' => $indexName,
63
        ])) {
64
            $this->output->writeln(
65
                sprintf(
66
                    '<error>Index %s doesn\'t exists and alias cannot be created.</error>',
67
                    $indexName
68
                )
69
            );
70
71
            return self::FAILURE;
72
        }
73
74
        try {
75
            $this->client->indices()->putAlias([
76
                'index' => $indexName,
77
                'name' => $aliasName,
78
            ]);
79
        } catch (Throwable $exception) {
80
            $this->output->writeln(
81
                sprintf(
82
                    '<error>Error creating alias %s for index %s, exception message: %s.</error>',
83
                    $aliasName,
84
                    $indexName,
85
                    $exception->getMessage()
86
                )
87
            );
88
89
            return self::FAILURE;
90
        }
91
92
        $this->output->writeln(
93
            sprintf(
94
                '<info>Alias %s created for index %s.</info>',
95
                $aliasName,
96
                $indexName
97
            )
98
        );
99
100
        return self::SUCCESS;
101
    }
102
}
103