Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

MigrationExecutor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
6
use Kaliop\eZMigrationBundle\API\ExecutorInterface;
7
use Kaliop\eZMigrationBundle\API\Exception\MigrationAbortedException;
8
use Kaliop\eZMigrationBundle\API\Exception\MigrationSuspendedException;
9
10
class MigrationExecutor extends AbstractExecutor
11
{
12
    protected $supportedStepTypes = array('migration');
13
    protected $supportedActions = array('cancel', 'suspend');
14
15
    protected $referenceMatcher;
16
    protected $contentManager;
17
    protected $locationManager;
18
    protected $contentTypeManager;
19
20
    public function __construct($referenceMatcher, ExecutorInterface $contentManager, ExecutorInterface $locationManager, ExecutorInterface $contentTypeManager)
21
    {
22
        $this->referenceMatcher = $referenceMatcher;
23
        $this->contentManager = $contentManager;
24
        $this->locationManager = $locationManager;
25
        $this->contentTypeManager = $contentTypeManager;
26
    }
27
28
    /**
29
     * @param MigrationStep $step
30
     * @return mixed
31
     * @throws \Exception
32
     */
33 View Code Duplication
    public function execute(MigrationStep $step)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
34
    {
35
        parent::execute($step);
36
37
        if (!isset($step->dsl['mode'])) {
38
            throw new \Exception("Invalid step definition: missing 'mode'");
39
        }
40
41
        $action = $step->dsl['mode'];
42
43
        if (!in_array($action, $this->supportedActions)) {
44
            throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'");
45
        }
46
47
        return $this->$action($step->dsl, $step->context);
48
    }
49
50
    /**
51
     * @param array $dsl
52
     * @param array $context
53
     * @return true
54
     * @throws \Exception
55
     */
56
    protected function cancel($dsl, $context)
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        $message = isset($dsl['message']) ? $dsl['message'] : '';
59
60
        if (isset($dsl['if'])) {
61
            if (!$this->matchConditions($dsl['if'])) {
62
                // q: return timestamp, matched condition or ... ?
63
                return true;
64
            }
65
        }
66
67
        throw new MigrationAbortedException($message);
68
    }
69
70
    /**
71
     * @param array $dsl
72
     * @param array $context
73
     * @return true
74
     * @throws \Exception
75
     */
76
    protected function suspend($dsl, $context)
77
    {
78
        $message = isset($dsl['message']) ? $dsl['message'] : '';
79
80
        if (!isset($dsl['until'])) {
81
            throw new \Exception("An until condition is required to suspend a migration");
82
        }
83
84
        if (isset($dsl['load'])) {
85
            $this->loadEntity($dsl['load'], $context);
86
        }
87
88
        if ($this->matchConditions($dsl['until'])) {
89
            // the time has come to resume!
90
            // q: return timestamp, matched condition or ... ?
91
            return true;
92
        }
93
94
        throw new MigrationSuspendedException($message);
95
    }
96
97
    protected function loadEntity($dsl, $context) {
98
        if (!isset($dsl['type']) || !isset($dsl['match'])) {
99
            throw new \Exception("A 'type' and a 'match' are required to load entities when suspending a migration");
100
        }
101
102
        $dsl['mode'] = 'load';
103
        // be kind to users and allow them not to specify this explicitly
104
        if (isset($dsl['references'])) {
105
            foreach($dsl['references'] as &$refDef) {
0 ignored issues
show
Bug introduced by
The expression $dsl['references'] of type string is not traversable.
Loading history...
106
                $refDef['overwrite'] = true;
107
            }
108
        }
109
        $step = new MigrationStep($dsl['type'], $dsl, $context);
110
111
        switch($dsl['type']) {
112
            case 'content':
113
                return $this->contentManager->execute($step);
114
            case 'location':
115
                return $this->locationManager->execute($step);
116
            case 'content_type':
117
                return $this->contentTypeManager->execute($step);
118
        }
119
    }
120
121
    protected function matchConditions($conditions)
122
    {
123
        foreach ($conditions as $key => $values) {
124
125
            /*if (!is_array($values)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
126
                $values = array($values);
127
            }*/
128
129
            switch ($key) {
130
                case 'date':
131
                    return time() >= $values;
132
133
                case 'match':
134
                    $match = $this->referenceMatcher->match($values);
135
                    return reset($match);
136
137
                default:
138
                    throw new \Exception("Unknown until condition: '$key' when suspending a migration");
139
            }
140
        }
141
    }
142
}
143