Passed
Push — master ( 03dfdf...d55943 )
by Gaetano
10:43
created

AbstractExecutor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 44
ccs 5
cts 6
cp 0.8333
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportedTypes() 0 3 1
A execute() 0 4 2
A parseReferenceDefinition() 0 9 6
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException;
6
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
7
use Kaliop\eZMigrationBundle\API\ExecutorInterface;
8
9
abstract class AbstractExecutor implements ExecutorInterface
10
{
11
    protected $supportedStepTypes = array();
12 96
13
    public function supportedTypes()
14 96
    {
15
        return $this->supportedStepTypes;
16
    }
17
18
    /**
19
     * IT IS MANDATORY TO OVERRIDE THIS IN SUBCLASSES
20
     * @param MigrationStep $step
21
     * @return mixed
22
     * @throws InvalidStepDefinitionException
23 47
     */
24
    public function execute(MigrationStep $step)
25 47
    {
26
        if (!in_array($step->type, $this->supportedStepTypes)) {
27
            throw new InvalidStepDefinitionException("Something is wrong! Executor can not work on step of type '{$step->type}'");
28 47
        }
29
    }
30
31
    /**
32
     * Allows to have refs defined using two syntax variants:
33
     *   - { identifier: xxx, attribute: yyy, overwrite: bool }
34
     * or
35
     *   identifier: attribute
36
     * @param $key
37
     * @param $value
38
     * @return array
39
     * @throws InvalidStepDefinitionException
40
     *
41
     * @todo move to a referenceSetter trait ?
42
     * @todo should we resolve references in ref identifiers, attributes and overwrite? Inception! :-D
43
     */
44
    protected function parseReferenceDefinition($key, $value)
45
    {
46
        if (is_string($key) && is_string($value)) {
47
            return array('identifier' => $key, 'attribute' => $value);
48
        }
49
        if (!is_array($value) || !isset($value['identifier']) || ! isset($value['attribute'])) {
50
            throw new InvalidStepDefinitionException("Invalid reference definition for reference number $key");
51
        }
52
        return $value;
53
    }
54
}
55