Completed
Push — master ( 5ec713...1c1906 )
by Colin
11s
created

IndexExistsCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
10
final class IndexExistsCommand extends Command
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $signature = 'laravel-elasticsearch:utils:index-exists
16
                            {index-name : The index name}';
17
18
    /**
19
     * @var Client
20
     */
21
    private $client;
22
23
    public function __construct(
24
        Client $client
25
    ) {
26
        $this->client = $client;
27
28
        parent::__construct();
29
    }
30
31
    public function handle(): int
32
    {
33
        $indexName = $this->argument('index-name');
34
35
        if ($indexName === null ||
36
            !is_string($indexName) ||
37
            mb_strlen($indexName) === 0
38
        ) {
39
            $this->output->writeln(
40
                '<error>Argument index-name must be a non empty string.</error>'
41
            );
42
43
            return self::FAILURE;
44
        }
45
46
        if ($this->client->indices()->exists([
47
            'index' => $indexName,
48
        ])) {
49
            $this->output->writeln(
50
                sprintf(
51
                    '<info>Index %s exists.</info>',
52
                    $indexName
53
                )
54
            );
55
56
            return self::SUCCESS;
57
        } else {
58
            $this->output->writeln(
59
                sprintf(
60
                    '<comment>Index %s doesn\'t exists.</comment>',
61
                    $indexName
62
                )
63
            );
64
65
            return self::SUCCESS;
66
        }
67
    }
68
}
69