EsIndexSwitcher   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.87%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 187
ccs 46
cts 47
cp 0.9787
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A getClient() 0 4 1
A getAlias() 0 4 1
A getIndexPrefix() 0 4 1
A getIndexSuffix() 0 4 1
A getNewIndexName() 0 4 1
A createNewIndex() 0 10 1
A finish() 0 17 1
A updateAlias() 0 19 1
A fetchCurrentIndices() 0 8 1
A removeOldIndices() 0 19 3
1
<?php
2
namespace EsIndexSwitcher;
3
4
use Elasticsearch\Client;
5
use DateTime;
6
7
class EsIndexSwitcher
8
{
9
10
    /**
11
     *
12
     * @var Client
13
     */
14
    private $client;
15
16
    /**
17
     *
18
     * @var string
19
     */
20
    private $alias;
21
22
    /**
23
     *
24
     * @var string
25
     */
26
    private $indexPrefix;
27
28
    /**
29
     *
30
     * @var string
31
     */
32
    private $indexSuffix;
33
34 8
    public function __construct(Client $client, string $alias, string $indexPrefix, string $indexSuffix = null)
35
    {
36 8
        $this->client = $client;
37
        
38 8
        $this->alias = $alias;
39
        
40 8
        if ($indexSuffix === null) {
41 7
            $indexSuffix = new DateTime();
42 7
            $indexSuffix = $indexSuffix->format('Y-m-d_H-i-s');
43
        }
44
        
45 8
        $this->indexPrefix = $indexPrefix;
46 8
        $this->indexSuffix = $indexSuffix;
47 8
    }
48
49
    /**
50
     *
51
     * @return Client
52
     */
53 8
    public function getClient()
54
    {
55 8
        return $this->client;
56
    }
57
58
    /**
59
     *
60
     * @return string
61
     */
62 3
    public function getAlias()
63
    {
64 3
        return $this->alias;
65
    }
66
67
    /**
68
     *
69
     * @return string
70
     */
71 8
    public function getIndexPrefix()
72
    {
73 8
        return $this->indexPrefix;
74
    }
75
76
    /**
77
     *
78
     * @return string
79
     */
80 7
    public function getIndexSuffix()
81
    {
82 7
        return $this->indexSuffix;
83
    }
84
85
    /**
86
     *
87
     * @return string
88
     */
89 7
    public function getNewIndexName()
90
    {
91 7
        return $this->getIndexPrefix() . '_' . $this->getIndexSuffix();
92
    }
93
94
    /**
95
     *
96
     * @param array $indexBody
97
     * @return array
98
     */
99 2
    public function createNewIndex(array $indexBody = [])
100
    {
101 2
        $client = $this->getClient();
102
        
103
        $params = [
104 2
            'index' => $this->getNewIndexName(),
105 2
            'body' => $indexBody
106
        ];
107 2
        return $client->indices()->create($params);
108
    }
109
110
    /**
111
     *
112
     * @return void
113
     */
114 1
    public function finish()
115
    {
116
        /*
117
         * Step 2) Update now the alias
118
         */
119 1
        $response = $this->updateAlias();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
120
        
121
        /*
122
         * Step 3) list all indices with the given prefix
123
         */
124 1
        $indices = $this->fetchCurrentIndices();
125
        
126
        /*
127
         * Step 3) Remove old indices
128
         */
129 1
        $response = $this->removeOldIndices($indices);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130 1
    }
131
132
    /**
133
     *
134
     * @return array
135
     */
136 2
    private function updateAlias()
137
    {
138 2
        $client = $this->getClient();
139
        
140
        $params = [
141
            'body' => [
142
                'actions' => [
143
                    [
144
                        'add' => [
145 2
                            'index' => $this->getNewIndexName(),
146 2
                            'alias' => $this->getAlias()
147
                        ]
148
                    ]
149
                ]
150
            ]
151
        ];
152
        
153 2
        return $client->indices()->updateAliases($params);
154
    }
155
156
    /**
157
     *
158
     * @return array
159
     */
160 2
    private function fetchCurrentIndices()
161
    {
162 2
        $client = $this->getClient();
163
        
164 2
        return $client->cat()->indices([
165 2
            'index' => $this->getIndexPrefix() . '*'
166
        ]);
167
    }
168
169
    /**
170
     *
171
     * @param array $indices
172
     * @return array
173
     */
174 2
    private function removeOldIndices(array $indices)
175
    {
176 2
        $client = $this->getClient();
177
        
178 2
        $response = [];
179 2
        foreach ($indices as $index) {
180
            // do not delete the just new created index
181 2
            if ($index['index'] === $this->getNewIndexName()) {
182
                continue;
183
            }
184
            
185
            // but all other
186 2
            $response[$index['index']] = $client->indices()->delete([
187 2
                'index' => $index['index']
188
            ]);
189
        }
190
        
191 2
        return $response;
192
    }
193
}
194