Completed
Push — master ( bed826...e1fc17 )
by Gaetano
08:02
created

LoopExecutor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C execute() 0 51 9
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
    public function __construct($migrationService, $loopResolver)
22
    {
23
        $this->migrationService = $migrationService;
24
        $this->loopResolver = $loopResolver;
25
    }
26
27
    /**
28
     * @param MigrationStep $step
29
     * @return mixed
30
     * @throws \Exception
31
     */
32
    public function execute(MigrationStep $step)
33
    {
34
        parent::execute($step);
35
36
        if (!isset($step->dsl['repeat']) || $step->dsl['repeat'] < 0) {
37
            throw new \Exception("Invalid step definition: missing 'repeat' or not a positive integer");
38
        }
39
40
        if (!isset($step->dsl['steps']) || !is_array($step->dsl['steps'])) {
41
            throw new \Exception("Invalid step definition: missing 'steps' or not an array");
42
        }
43
44
        // no need for a 'mode' for now
45
        /*$action = $step->dsl['mode'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
47
        if (!in_array($action, $this->supportedActions)) {
48
            throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'");
49
        }*/
50
51
        $this->skipStepIfNeeded($step);
52
53
        // before engaging in the loop, check that all steps are valid
54
        $stepExecutors = array();
55
        foreach ($step->dsl['steps'] as $i => $stepDef) {
56
            $type = $stepDef['type'];
57
            try {
58
                $stepExecutors[$i] = $this->migrationService->getExecutor($type);
59
            } catch (\InvalidArgumentException $e) {
60
                throw new \InvalidArgumentException($e->getMessage() . " in sub-step of a loop step");
61
            }
62
        }
63
64
        $this->loopResolver->beginLoop();
65
        $result = null;
66
67
        // NB: we are *not* firing events for each pass of the loop... it might be worth making that optionally happen ?
68
        for ($i = 0; $i < $step->dsl['repeat']; $i++) {
69
70
            $this->loopResolver->loopStep();
71
72
            foreach ($step->dsl['steps'] as $j => $stepDef) {
73
                $type = $stepDef['type'];
74
                unset($stepDef['type']);
75
                $subStep = new MigrationStep($type, $stepDef, array_merge($step->context, array()));
76
                $result = $stepExecutors[$j]->execute($subStep);
77
            }
78
        }
79
80
        $this->loopResolver->endLoop();
81
        return $result;
82
    }
83
84
}
85