MigrationTypes::mapTypeToClass()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 36
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.0268

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 36
ccs 29
cts 31
cp 0.9355
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1
crap 10.0268

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Triadev\EsMigration\Business\Mapper;
3
4
use Triadev\EsMigration\Business\Migration\AbstractMigration;
5
6
use Triadev\EsMigration\Business\Migration\CreateIndex;
7
use Triadev\EsMigration\Business\Migration\DeleteAlias;
8
use Triadev\EsMigration\Business\Migration\DeleteByQuery;
9
use Triadev\EsMigration\Business\Migration\DeleteIndex;
10
use Triadev\EsMigration\Business\Migration\PutAlias;
11
use Triadev\EsMigration\Business\Migration\Reindex;
12
use Triadev\EsMigration\Business\Migration\UpdateByQuery;
13
use Triadev\EsMigration\Business\Migration\UpdateIndexMapping;
14
use Triadev\EsMigration\Business\Migration\UpdateIndexSetting;
15
16
class MigrationTypes
17
{
18
    const MIGRATION_TYPE_CREATE_INDEX = 'createIndex';
19
    const MIGRATION_TYPE_UPDATE_INDEX_MAPPING = 'updateIndexMapping';
20
    const MIGRATION_TYPE_UPDATE_INDEX_SETTING = 'updateIndexMappingSetting';
21
    const MIGRATION_TYPE_DELETE_INDEX = 'deleteIndex';
22
    const MIGRATION_TYPE_PUT_ALIAS = 'putAlias';
23
    const MIGRATION_TYPE_DELETE_ALIAS = 'deleteAlias';
24
    const MIGRATION_TYPE_DELETE_BY_QUERY = 'deleteByQuery';
25
    const MIGRATION_TYPE_UPDATE_BY_QUERY = 'updateByQuery';
26
    const MIGRATION_TYPE_REINDEX = 'reindex';
27
    
28
    /**
29
     * Is migration type valid
30
     *
31
     * @param string $type
32
     * @return bool
33
     */
34 16
    public function isMigrationTypeValid(string $type) : bool
35
    {
36 16
        if (in_array($type, [
37 16
            self::MIGRATION_TYPE_CREATE_INDEX,
38 16
            self::MIGRATION_TYPE_UPDATE_INDEX_MAPPING,
39 16
            self::MIGRATION_TYPE_UPDATE_INDEX_SETTING,
40 16
            self::MIGRATION_TYPE_DELETE_INDEX,
41 16
            self::MIGRATION_TYPE_PUT_ALIAS,
42 16
            self::MIGRATION_TYPE_DELETE_ALIAS,
43 16
            self::MIGRATION_TYPE_DELETE_BY_QUERY,
44 16
            self::MIGRATION_TYPE_UPDATE_BY_QUERY,
45 16
            self::MIGRATION_TYPE_REINDEX,
46
        ])) {
47 15
            return true;
48
        }
49
        
50 1
        return false;
51
    }
52
    
53
    /**
54
     * Map type to class
55
     *
56
     * @param string $type
57
     * @return null|AbstractMigration
58
     */
59 7
    public function mapTypeToClass(string $type) : ?AbstractMigration
60
    {
61
        switch ($type) {
62 7
            case MigrationTypes::MIGRATION_TYPE_CREATE_INDEX:
63 6
                $migrationClass = new CreateIndex();
64 6
                break;
65 5
            case MigrationTypes::MIGRATION_TYPE_UPDATE_INDEX_MAPPING:
66 3
                $migrationClass = new UpdateIndexMapping();
67 3
                break;
68 3
            case MigrationTypes::MIGRATION_TYPE_UPDATE_INDEX_SETTING:
69 1
                $migrationClass = new UpdateIndexSetting();
70 1
                break;
71 3
            case MigrationTypes::MIGRATION_TYPE_DELETE_INDEX:
72 3
                $migrationClass = new DeleteIndex();
73 3
                break;
74 1
            case MigrationTypes::MIGRATION_TYPE_PUT_ALIAS:
75 1
                $migrationClass = new PutAlias();
76 1
                break;
77 1
            case MigrationTypes::MIGRATION_TYPE_DELETE_ALIAS:
78 1
                $migrationClass = new DeleteAlias();
79 1
                break;
80 1
            case MigrationTypes::MIGRATION_TYPE_DELETE_BY_QUERY:
81 1
                $migrationClass = new DeleteByQuery();
82 1
                break;
83 1
            case MigrationTypes::MIGRATION_TYPE_UPDATE_BY_QUERY:
84 1
                $migrationClass = new UpdateByQuery();
85 1
                break;
86 1
            case MigrationTypes::MIGRATION_TYPE_REINDEX:
87 1
                $migrationClass = new Reindex();
88 1
                break;
89
            default:
90
                $migrationClass = null;
91
                break;
92
        }
93
        
94 7
        return $migrationClass;
95
    }
96
}
97