Completed
Push — master ( 65e33a...5980e7 )
by Gaetano
06:47
created

LoopExecutor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface;
9
use Kaliop\eZMigrationBundle\API\Exception\MigrationStepSkippedException;
10
11
class LoopExecutor extends AbstractExecutor
12
{
13
    use IgnorableStepExecutorTrait;
14
15
    protected $supportedStepTypes = array('loop');
16
17
    /** @var MigrationService $migrationService */
18
    protected $migrationService;
19
20
    /** @var LoopResolver $loopResolver */
21 80
    protected $loopResolver;
22
23 80
    protected $referenceResolver;
24 80
25 80
    public function __construct($migrationService, $loopResolver, ReferenceResolverInterface $referenceResolver)
26
    {
27
        $this->migrationService = $migrationService;
28
        $this->loopResolver = $loopResolver;
29
        $this->referenceResolver = $referenceResolver;
30
    }
31
32 1
    /**
33
     * @param MigrationStep $step
34 1
     * @return mixed
35
     * @throws \Exception
36 1
     */
37
    public function execute(MigrationStep $step)
38
    {
39
        parent::execute($step);
40 1
41 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...
42
            throw new \Exception("Invalid step definition: missing 'repeat' or 'over'");
43
        }
44 1
45 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...
46
            throw new \Exception("Invalid step definition: can not have both 'repeat' and 'over'");
47
        }
48 1
49 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...
50
            throw new \Exception("Invalid step definition: 'repeat' is not a positive integer");
51
        }
52 1
53
        if (!isset($step->dsl['steps']) || !is_array($step->dsl['steps'])) {
54
            throw new \Exception("Invalid step definition: missing 'steps' or not an array");
55 1
        }
56 1
57 1
        $this->skipStepIfNeeded($step);
58
59 1
        // before engaging in the loop, check that all steps are valid
60
        $stepExecutors = array();
61 1
        foreach ($step->dsl['steps'] as $i => $stepDef) {
62
            $type = $stepDef['type'];
63
            try {
64
                $stepExecutors[$i] = $this->migrationService->getExecutor($type);
65 1
            } catch (\InvalidArgumentException $e) {
66 1
                throw new \InvalidArgumentException($e->getMessage() . " in sub-step of a loop step");
67
            }
68 1
        }
69 1
70
        $this->loopResolver->beginLoop();
71 1
        $result = null;
72
        if (isset($step->dsl['over'])) {
73 1
            $over = $this->referenceResolver->resolveReference($step->dsl['over']);
0 ignored issues
show
Documentation introduced by
$step->dsl['over'] is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74 1
            foreach ($over as $key => $value) {
75 1
                $this->loopResolver->loopStep($key, $value);
76 1
77 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...
78
                    $type = $stepDef['type'];
79
                    unset($stepDef['type']);
80
                    $subStep = new MigrationStep($type, $stepDef, array_merge($step->context, array()));
81
                    try {
82 1
                        $result = $stepExecutors[$j]->execute($subStep);
83
                    } catch(MigrationStepSkippedException $e) {
84 1
                        // all ok, continue the loop
85
                    }
86 1
                }
87 1
            }
88 1
        } else {
89 1
            // NB: we are *not* firing events for each pass of the loop... it might be worth making that optionally happen ?
90 1
            for ($i = 0; $i < $step->dsl['repeat']; $i++) {
91
92
                $this->loopResolver->loopStep();
93
94 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...
95 1
                    $type = $stepDef['type'];
96 1
                    unset($stepDef['type']);
97
                    $subStep = new MigrationStep($type, $stepDef, array_merge($step->context, array()));
98
                    try {
99
                        $result = $stepExecutors[$j]->execute($subStep);
100
                    } catch(MigrationStepSkippedException $e) {
101
                        // all ok, continue the loop
102
                    }
103
                }
104
            }
105
        }
106
107
        $this->loopResolver->endLoop();
108
        return $result;
109
    }
110
111
}
112