Completed
Pull Request — develop (#110)
by Sam
03:16
created

UpdateIndexSettingsCommand::putIndexSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Console;
4
5
use Illuminate\Console\Command;
6
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
7
8
/**
9
 * Class SetIndexSettingsCommand
10
 * @package namespace Nord\Lumen\Elasticsearch\Console
11
 */
12
class UpdateIndexSettingsCommand extends Command
13
{
14
15
    /**
16
     * @var string
17
     */
18
    protected $signature = 'elastic:index:settings:update
19
                            { index : The name of the index to update }
20
                            {--numReplicas= : The value of index.refresh_interval that should be set } 
21
                            {--refreshInterval= : The value of index.refresh_interval that should be set }';
22
23
    /**
24
     * @var string
25
     */
26
    protected $description = 'Updates specified dynamic index settings for the specified index';
27
28
    /**
29
     * @var ElasticsearchServiceContract
30
     */
31
    private $elasticsearchService;
32
33
    /**
34
     * SetIndexSettingsCommand constructor.
35
     *
36
     * @param ElasticsearchServiceContract $elasticsearchService
37
     */
38
    public function __construct(ElasticsearchServiceContract $elasticsearchService)
39
    {
40
        parent::__construct();
41
42
        $this->elasticsearchService = $elasticsearchService;
43
    }
44
45
    public function handle(): void
46
    {
47
        $index           = $this->input->getArgument('index');
48
        $numReplicas     = $this->input->getOption('numReplicas');
49
        $refreshInterval = $this->input->getOption('refreshInterval');
50
51
        $settings = [];
52
53
        if ($numReplicas !== null) {
54
            $settings['number_of_replicas'] = (int)$numReplicas;
55
        }
56
57
        if ($refreshInterval !== null) {
58
            $settings['refresh_interval'] = $refreshInterval;
59
        }
60
61
        if (!empty($settings)) {
62
            $this->putIndexSettings($index, $settings);
63
        }
64
    }
65
66
    /**
67
     * @param string $index
68
     * @param array  $settings
69
     */
70
    private function putIndexSettings(string $index, array $settings): void
71
    {
72
        $this->elasticsearchService->indices()->putSettings([
73
            'index' => $index,
74
            'body'  => $settings,
75
        ]);
76
    }
77
}
78