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

LoopExecutor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
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