CreateElasticSearchIndex   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 23 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arun
5
 * Date: 2019-07-16
6
 * Time: 10:35
7
 */
8
9
namespace ArunFung\ScoutElasticSearch\Console\Commands;
10
11
use Elasticsearch\ClientBuilder as ElasticSearchBuilder;
12
use Illuminate\Console\Command;
13
use Exception;
14
use Illuminate\Support\Facades\Config;
15
16
/**
17
 * Class CreateElasticSearchIndex
18
 * @package App\Console\Commands
19
 */
20
class CreateElasticSearchIndex extends Command
21
{
22
    /**
23
     * The name and signature of the console command.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'es:create-index';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'create elasticSearch index';
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $elasticSearch = ElasticSearchBuilder::create()->setHosts(Config::get('elasticsearch.hosts'))->build()->indices();
44
45
        $index = Config::get('elasticsearch.index');
46
        if ($elasticSearch->exists(['index' => $index])) {
47
            $this->error(sprintf('The "%s" index already exists', $index));
48
            return;
49
        }
50
        $params = Config::get('elasticsearch.' . $index);
51
52
        if (!empty($params)) {
53
            try {
54
                $elasticSearch->create($params);
55
            } catch (Exception $e) {
56
                $this->error($e->getMessage());
57
                return;
58
            }
59
            $this->info(sprintf('"%s" index created successfully', $index));
60
            return;
61
        } else {
62
            $this->warn(sprintf('"%s" index configuration not found', $index));
63
            return;
64
        }
65
    }
66
}
67