Issues (16)

src/Console/UpdateIndexSettingsCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Console;
4
5
use Illuminate\Console\Command;
6
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
7
use Nord\Lumen\Elasticsearch\IndexNamePrefixer;
8
9
/**
10
 * Class SetIndexSettingsCommand
11
 * @package namespace Nord\Lumen\Elasticsearch\Console
12
 */
13
class UpdateIndexSettingsCommand extends Command
14
{
15
16
    /**
17
     * @var string
18
     */
19
    protected $signature = 'elastic:index:settings:update
20
                            { index : The name of the index to update }
21
                            {--numReplicas= : The value of index.refresh_interval that should be set } 
22
                            {--refreshInterval= : The value of index.refresh_interval that should be set }';
23
24
    /**
25
     * @var string
26
     */
27
    protected $description = 'Updates specified dynamic index settings for the specified index';
28
29
    /**
30
     * @var ElasticsearchServiceContract
31
     */
32
    private $elasticsearchService;
33
34
    /**
35
     * SetIndexSettingsCommand constructor.
36
     *
37
     * @param ElasticsearchServiceContract $elasticsearchService
38
     */
39
    public function __construct(ElasticsearchServiceContract $elasticsearchService)
40
    {
41
        parent::__construct();
42
43
        $this->elasticsearchService = $elasticsearchService;
44
    }
45
46
    public function handle(): void
47
    {
48
        $index           = IndexNamePrefixer::getPrefixedIndexName($this->input->getArgument('index'));
0 ignored issues
show
It seems like $this->input->getArgument('index') can also be of type null and string[]; however, parameter $indexName of Nord\Lumen\Elasticsearch...:getPrefixedIndexName() 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

48
        $index           = IndexNamePrefixer::getPrefixedIndexName(/** @scrutinizer ignore-type */ $this->input->getArgument('index'));
Loading history...
49
        $numReplicas     = $this->input->getOption('numReplicas');
50
        $refreshInterval = $this->input->getOption('refreshInterval');
51
52
        $settings = [];
53
54
        if ($numReplicas !== null) {
55
            $settings['number_of_replicas'] = (int)$numReplicas;
56
        }
57
58
        if ($refreshInterval !== null) {
59
            $settings['refresh_interval'] = $refreshInterval;
60
        }
61
62
        if (!empty($settings)) {
63
            $this->putIndexSettings($index, $settings);
64
        }
65
    }
66
67
    /**
68
     * @param string $index
69
     * @param array  $settings
70
     */
71
    private function putIndexSettings(string $index, array $settings): void
72
    {
73
        $this->elasticsearchService->indices()->putSettings([
74
            'index' => $index,
75
            'body'  => $settings,
76
        ]);
77
    }
78
}
79