AbstractMigration::migrate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
namespace Triadev\EsMigration\Business\Migration;
3
4
use Elasticsearch\Client;
5
use Illuminate\Support\Facades\Log;
6
use Triadev\EsMigration\Exception\MigrationStepValidation;
7
use Triadev\EsMigration\Models\MigrationStep;
8
use Illuminate\Support\Facades\Validator;
9
10
abstract class AbstractMigration
11
{
12
    /**
13
     * Migrate
14
     *
15
     * @param Client $esClient
16
     * @param MigrationStep $migrationStep
17
     *
18
     * @throws MigrationStepValidation
19
     * @throws \Exception
20
     */
21 34
    public function migrate(Client $esClient, MigrationStep $migrationStep)
22
    {
23 34
        $params = $migrationStep->getParams();
24
        
25 34
        $validator = Validator::make($params, $this->getValidationRules());
26
        
27 34
        if ($validator->fails()) {
28 11
            Log::error("The migration step validation failed.", [
29 11
                'id' => $migrationStep->getId(),
30 11
                'type' => $migrationStep->getType()
31
            ]);
32
            
33 11
            throw new MigrationStepValidation(sprintf(
34 11
                "The migration step validation failed: %s",
35 11
                $migrationStep->getId()
36
            ));
37
        }
38
        
39 24
        $this->preCheck($esClient, $params);
40
        
41 14
        $this->startMigration($esClient, $params);
42
        
43 13
        $this->postCheck($esClient, $params);
44 13
    }
45
    
46
    /**
47
     * Get validation rules
48
     *
49
     * @return array
50
     */
51
    abstract public function getValidationRules() : array;
52
    
53
    /**
54
     * Pre check
55
     *
56
     * @param Client $esClient
57
     * @param array $params
58
     *
59
     * @throws \Exception
60
     */
61
    abstract public function preCheck(Client $esClient, array $params);
62
    
63
    /**
64
     * Start migration
65
     *
66
     * @param Client $esClient
67
     * @param array $params
68
     */
69
    abstract public function startMigration(Client $esClient, array $params);
70
    
71
    /**
72
     * Post check
73
     *
74
     * @param Client $esClient
75
     * @param array $params
76
     *
77
     * @throws \Exception
78
     */
79
    abstract public function postCheck(Client $esClient, array $params);
80
}
81