CreateIndex::startMigration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace Triadev\EsMigration\Business\Migration;
3
4
use Elasticsearch\Client;
5
6
class CreateIndex extends AbstractMigration
7
{
8
    /**
9
     * Get validation rules
10
     *
11
     * @return array
12
     */
13 8
    public function getValidationRules() : array
14
    {
15
        return [
16 8
            'index' => 'required|string',
17
            'body' => 'required|array',
18
            'body.mappings' => 'array',
19
            'body.settings' => 'array'
20
        ];
21
    }
22
    
23
    /**
24
     * Pre check
25
     *
26
     * @param Client $esClient
27
     * @param array $params
28
     *
29
     * @throws \Exception
30
     */
31 7
    public function preCheck(Client $esClient, array $params)
32
    {
33 7
        $index = array_get($params, 'index');
34
        
35 7
        if ($esClient->indices()->exists(['index' => $index])) {
36 1
            throw new \Exception(sprintf("Index already exist: %s", $index));
37
        };
38 5
    }
39
    
40
    /**
41
     * Start migration
42
     *
43
     * @param Client $esClient
44
     * @param array $params
45
     */
46 5
    public function startMigration(Client $esClient, array $params)
47
    {
48 5
        $esClient->indices()->create($params);
49 5
    }
50
    
51
    /**
52
     * Post check
53
     *
54
     * @param Client $esClient
55
     * @param array $params
56
     *
57
     * @throws \Exception
58
     */
59 5
    public function postCheck(Client $esClient, array $params)
60
    {
61 5
        $index = array_get($params, 'index');
62
    
63 5
        if (!$esClient->indices()->exists(['index' => $index])) {
64
            throw new \Exception(sprintf("Index not exist: %s", $index));
65
        };
66 5
    }
67
}
68