Passed
Push — master ( 8e684f...3b52b9 )
by Arun
03:44
created

CreateElasticSearchIndex   A

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
15
/**
16
 * Class CreateElasticSearchIndex
17
 * @package App\Console\Commands
18
 */
19
class CreateElasticSearchIndex extends Command
20
{
21
    /**
22
     * The name and signature of the console command.
23
     *
24
     * @var string
25
     */
26
    protected $signature = 'es:create-index';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'create elasticSearch index';
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $elasticSearch = ElasticSearchBuilder::create()->setHosts(config('elasticsearch.hosts'))->build()->indices();
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $elasticSearch = ElasticSearchBuilder::create()->setHosts(/** @scrutinizer ignore-call */ config('elasticsearch.hosts'))->build()->indices();
Loading history...
43
44
        $index = config('elasticsearch.index');
45
        if ($elasticSearch->exists(['index' => $index])) {
46
            $this->error(sprintf('The "%s" index already exists', $index));
47
            return;
48
        }
49
        $params = config('elasticsearch.' . $index);
50
51
        if (!empty($params)) {
52
            try {
53
                $elasticSearch->create($params);
54
            } catch (Exception $e) {
55
                $this->error($e->getMessage());
56
                return;
57
            }
58
            $this->info(sprintf('"%s" index created successfully', $index));
59
            return;
60
        } else {
61
            $this->warn(sprintf('"%s" index configuration not found', $index));
62
            return;
63
        }
64
    }
65
}
66