Completed
Push — master ( 6a929a...bc9a97 )
by Gaetano
07:56
created

LoopExecutor::execute()   C

Complexity

Conditions 16
Paths 13

Size

Total Lines 66

Duplication

Lines 21
Ratio 31.82 %

Code Coverage

Tests 21
CRAP Score 16.5

Importance

Changes 0
Metric Value
dl 21
loc 66
ccs 21
cts 24
cp 0.875
rs 5.5666
c 0
b 0
f 0
cc 16
nc 13
nop 1
crap 16.5

How to fix   Long Method    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
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
6
use Kaliop\eZMigrationBundle\Core\MigrationService;
7
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\LoopResolver;
8
9
class LoopExecutor extends AbstractExecutor
10
{
11
    use IgnorableStepExecutorTrait;
12
13
    protected $supportedStepTypes = array('loop');
14
15
    /** @var MigrationService $migrationService */
16
    protected $migrationService;
17
18
    /** @var LoopResolver $loopResolver */
19
    protected $loopResolver;
20
21 79
    public function __construct($migrationService, $loopResolver)
22
    {
23 79
        $this->migrationService = $migrationService;
24 79
        $this->loopResolver = $loopResolver;
25 79
    }
26
27
    /**
28
     * @param MigrationStep $step
29
     * @return mixed
30
     * @throws \Exception
31
     */
32 1
    public function execute(MigrationStep $step)
33
    {
34 1
        parent::execute($step);
35
36 1 View Code Duplication
        if (!isset($step->dsl['repeat']) && !isset($step->dsl['over'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
37
            throw new \Exception("Invalid step definition: missing 'repeat' or 'over'");
38
        }
39
40 1 View Code Duplication
        if (isset($step->dsl['repeat']) && isset($step->dsl['over'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41
            throw new \Exception("Invalid step definition: can not have both 'repeat' and 'over'");
42
        }
43
44 View Code Duplication
        if (isset($step->dsl['repeat']) && $step->dsl['repeat'] < 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
            throw new \Exception("Invalid step definition: 'repeat' is not a positive integer");
46
        }
47
48
        if (!isset($step->dsl['steps']) || !is_array($step->dsl['steps'])) {
49
            throw new \Exception("Invalid step definition: missing 'steps' or not an array");
50
        }
51 1
52
        $this->skipStepIfNeeded($step);
53
54 1
        // before engaging in the loop, check that all steps are valid
55 1
        $stepExecutors = array();
56 1
        foreach ($step->dsl['steps'] as $i => $stepDef) {
57
            $type = $stepDef['type'];
58 1
            try {
59
                $stepExecutors[$i] = $this->migrationService->getExecutor($type);
60 1
            } catch (\InvalidArgumentException $e) {
61
                throw new \InvalidArgumentException($e->getMessage() . " in sub-step of a loop step");
62
            }
63
        }
64 1
65 1
        $this->loopResolver->beginLoop();
66
        $result = null;
67
68 1
        if (isset($step->dsl['over'])) {
69
            foreach ($step->dsl['over'] as $key => $value) {
70 1
71
                $this->loopResolver->loopStep($key, $value);
72 1
73 1 View Code Duplication
                foreach ($step->dsl['steps'] as $j => $stepDef) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74 1
                    $type = $stepDef['type'];
75 1
                    unset($stepDef['type']);
76 1
                    $subStep = new MigrationStep($type, $stepDef, array_merge($step->context, array()));
77
                    $result = $stepExecutors[$j]->execute($subStep);
78
                }
79
            }
80 1
        } else {
81 1
            // NB: we are *not* firing events for each pass of the loop... it might be worth making that optionally happen ?
82
            for ($i = 0; $i < $step->dsl['repeat']; $i++) {
83
84
                $this->loopResolver->loopStep();
85
86 View Code Duplication
                foreach ($step->dsl['steps'] as $j => $stepDef) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
                    $type = $stepDef['type'];
88
                    unset($stepDef['type']);
89
                    $subStep = new MigrationStep($type, $stepDef, array_merge($step->context, array()));
90
                    $result = $stepExecutors[$j]->execute($subStep);
91
                }
92
            }
93
        }
94
95
        $this->loopResolver->endLoop();
96
        return $result;
97
    }
98
99
}
100