Completed
Push — master ( 727d36...751bcb )
by Gaetano
15:50
created

MigrationDefinitionExecutor   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 113
Duplicated Lines 24.78 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 5
dl 28
loc 113
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 16 16 3
D generate() 0 34 10
A setReferences() 0 15 4
A getGeneratingExecutors() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
namespace Kaliop\eZMigrationBundle\Core\Executor;
5
6
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
7
use Kaliop\eZMigrationBundle\API\LanguageAwareInterface;
8
use Kaliop\eZMigrationBundle\API\MatcherInterface;
9
use Kaliop\eZMigrationBundle\API\ReferenceBagInterface;
10
use Kaliop\eZMigrationBundle\API\MigrationGeneratorInterface;
11
use JmesPath\Env as JmesPath;
12
13
class MigrationDefinitionExecutor extends AbstractExecutor
14
{
15
    protected $supportedStepTypes = array('migration_definition');
16
    protected $supportedActions = array('generate');
17
18
    /** @var \Kaliop\eZMigrationBundle\Core\MigrationService $migrationService */
19
    protected $migrationService;
20
    /** @var ReferenceBagInterface $referenceResolver */
21
    protected $referenceResolver;
22
23
    public function __construct($migrationService, ReferenceBagInterface $referenceResolver)
24
    {
25
        $this->migrationService = $migrationService;
26
        $this->referenceResolver = $referenceResolver;
27
    }
28
29
    /**
30
     * @param MigrationStep $step
31
     * @return mixed
32
     * @throws \Exception
33
     */
34 View Code Duplication
    public function execute(MigrationStep $step)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        parent::execute($step);
37
38
        if (!isset($step->dsl['mode'])) {
39
            throw new \Exception("Invalid step definition: missing 'mode'");
40
        }
41
42
        $action = $step->dsl['mode'];
43
44
        if (!in_array($action, $this->supportedActions)) {
45
            throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'");
46
        }
47
48
        return $this->$action($step->dsl, $step->context);
49
    }
50
51
    /**
52
     * @todo allow to save to disk
53
     * @param array $dsl
54
     * @param array $context
55
     * @return array
56
     * @throws \Exception
57
     */
58
    protected function generate($dsl, $context) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
        if (!isset($dsl['migration_type'])) {
60
            throw new \Exception("Invalid step definition: miss 'migration_type'");
61
        }
62
        $migrationType = $dsl['migration_type'];
63
        if (!isset($dsl['migration_mode'])) {
64
            throw new \Exception("Invalid step definition: miss 'migration_mode'");
65
        }
66
        $migrationMode = $dsl['migration_mode'];
67
        if (!isset($dsl['match']) || !is_array($dsl['match'])) {
68
            throw new \Exception("Invalid step definition: miss 'match' to determine what to generate migration definition for");
69
        }
70
        $match = $dsl['match'];
71
72
        $executors = $this->getGeneratingExecutors();
73
        if (!in_array($migrationType, $executors)) {
74
            throw new \Exception("It is not possible to generate a migration of type '$migrationType': executor not found or not a generator");
75
        }
76
        $executor = $this->migrationService->getExecutor($migrationType);
77
78
        if (isset($dsl['lang']) && $executor instanceof LanguageAwareInterface) {
79
            $executor->setLanguageCode($dsl['lang']);
80
        }
81
82
        $matchCondition = array($match['type'] => $match['value']);
83
        if (isset($match['except']) && $match['except']) {
84
            $matchCondition = array(MatcherInterface::MATCH_NOT => $matchCondition);
85
        }
86
        $result = $executor->generateMigration($matchCondition, $migrationMode);
87
88
        $this->setReferences($result, $dsl);
89
90
        return $result;
91
    }
92
93
    protected function setReferences($result, $dsl)
94
    {
95
        if (!array_key_exists('references', $dsl)) {
96
            return false;
97
        }
98
99
        foreach ($dsl['references'] as $reference) {
100
            if (!isset($reference['json_path'])) {
101
                throw new \InvalidArgumentException('MigrationDefinition Executor does not support setting references if not using a json_path expression');
102
            }
103
104
            $value = JmesPath::search($reference['json_path'], $result);
105
            $this->referenceResolver->addReference($reference['identifier'], $value);
106
        }
107
    }
108
109
    /**
110
     * @todo cache this for faster acccess
111
     * @return array
112
     */
113 View Code Duplication
    protected function getGeneratingExecutors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $migrationService = $this->migrationService;
116
        $executors = $migrationService->listExecutors();
117
        foreach($executors as $key => $name) {
118
            $executor = $migrationService->getExecutor($name);
119
            if (!$executor instanceof MigrationGeneratorInterface) {
120
                unset($executors[$key]);
121
            }
122
        }
123
        return $executors;
124
    }
125
}
126