Passed
Push — master ( a4037f...e3e9e9 )
by Christopher
01:15
created

src/Console/Commands/Index/Create.php (2 issues)

Labels
Severity
1
<?php
2
namespace Triadev\Es\Console\Commands\Index;
3
4
use Triadev\Es\Business\Config\ConfigFacade;
5
use Triadev\Es\Contract\ElasticsearchIndexContract;
6
use Triadev\Es\Exception\Index\IndexFoundException;
7
use Illuminate\Console\Command;
8
use Log;
9
10
class Create extends Command
11
{
12
    use ConfigFacade;
13
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'triadev:es:index:create
20
                            {index : Index (_all for all)}
21
                            {version : Version}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Create one or more indices.';
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @param ElasticsearchIndexContract $elasticsearchAlias
34
     */
35 15
    public function handle(ElasticsearchIndexContract $elasticsearchAlias)
36
    {
37 15
        $version = $this->argument('version');
38
39 15
        $indices = $this->getIndices();
40
41 15
        if ($this->argument('index') == '_all') {
42
            $index = array_keys($indices);
43
        } else {
44 15
            $index = explode(',', $this->argument('index'));
0 ignored issues
show
It seems like $this->argument('index') can also be of type string[]; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

44
            $index = explode(',', /** @scrutinizer ignore-type */ $this->argument('index'));
Loading history...
45
        }
46
47 15
        foreach ($index as $i) {
48 15
            if (!array_key_exists($i, $indices)) {
49
                Log::info(sprintf("The index could not be found: %s", $i));
50
                continue;
51
            }
52
53
            try {
54 15
                $result = $elasticsearchAlias->createIndex(
55 15
                    $i,
56
                    [
57 15
                        'body' => $indices[$i]
58
                    ],
59 15
                    $version
0 ignored issues
show
It seems like $version can also be of type string[]; however, parameter $version of Triadev\Es\Contract\Elas...Contract::createIndex() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

59
                    /** @scrutinizer ignore-type */ $version
Loading history...
60
                );
61
62 12
                Log::info('The indices could be created.', $result);
63 3
            } catch (IndexFoundException $e) {
64 3
                Log::error($e->getMessage());
65
            } catch (\Exception $e) {
66
                Log::error(sprintf(
67
                    "The indices could not be created: %s",
68
                    $e->getMessage()
69 15
                ), $index);
70
            }
71
        }
72 15
    }
73
}
74