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

LoopExecutor::execute()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 6.2727
c 0
b 0
f 0
cc 9
eloc 25
nc 9
nop 1

How to fix   Long Method   

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
    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