Completed
Push — master ( 937e4c...633401 )
by Gaetano
10:25
created

ReferenceExecutor   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 4
dl 0
loc 93
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 16 3
B set() 0 14 5
D load() 0 39 10
1
<?php
2
3
4
namespace Kaliop\eZMigrationBundle\Core\Executor;
5
6
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
7
use Kaliop\eZMigrationBundle\API\ReferenceBagInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\Yaml\Yaml;
10
11
class ReferenceExecutor extends AbstractExecutor
12
{
13
    protected $supportedStepTypes = array('reference');
14
    protected $supportedActions = array('set', 'load');
15
16
    protected $container;
17
    /** @var ReferenceResolverInterface $referenceResolver */
18
    protected $referenceResolver;
19
20
    public function __construct(ContainerInterface $container, ReferenceBagInterface $referenceResolver)
21
    {
22
        $this->container = $container;
23
        $this->referenceResolver = $referenceResolver;
0 ignored issues
show
Documentation Bug introduced by
It seems like $referenceResolver of type object<Kaliop\eZMigratio...\ReferenceBagInterface> is incompatible with the declared type object<Kaliop\eZMigratio...renceResolverInterface> of property $referenceResolver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24
    }
25
26
    /**
27
     * @param MigrationStep $step
28
     * @return void
29
     * @throws \Exception if migration step is not for this type of db
30
     */
31
    public function execute(MigrationStep $step)
32
    {
33
        parent::execute($step);
34
35
        if (!isset($step->dsl['mode'])) {
36
            throw new \Exception("Invalid step definition: missing 'mode'");
37
        }
38
39
        $action = $step->dsl['mode'];
40
41
        if (!in_array($action, $this->supportedActions)) {
42
            throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'");
43
        }
44
45
        $this->$action($step->dsl);
46
    }
47
48
    protected function set($dsl) {
49
        if (!isset($dsl['identifier'])) {
50
            throw new \Exception("Invalid step definition: miss 'identifier' for setting reference");
51
        }
52
        if (!isset($dsl['value'])) {
53
            throw new \Exception("Invalid step definition: miss 'value' for setting reference");
54
        }
55
        $value = $dsl['value'];
56
        if (preg_match('/%.+%$/', $value)) {
57
            $value = $this->container->getParameter(trim($value, '%'));
58
        }
59
        $overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false;
60
        $this->referenceResolver->addReference($dsl['identifier'], $value, $overwrite);
61
    }
62
63
    protected function load($dsl)
64
    {
65
        if (!isset($dsl['file'])) {
66
            throw new \Exception("Invalid step definition: miss 'file' for loading references");
67
        }
68
        $fileName = $dsl['file'];
69
70
        $overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false;
71
72
        $fileName = str_replace('{ENV}', $this->container->get('kernel')->getEnvironment(), $fileName);
73
        if (!is_file($fileName)) {
74
            throw new \Exception("Invalid step definition: invalid 'file' for loading references");
75
        }
76
        $data = file_get_contents($fileName);
77
78
        $ext = pathinfo($dsl['file'], PATHINFO_EXTENSION);
79
        switch ($ext) {
80
            case 'json':
81
                $data = json_decode($data, true);
82
                break;
83
            case 'yml':
84
            case 'yaml':
85
                $data = Yaml::parse($data);
86
                break;
87
            default:
88
                throw new \Exception("Invalid step definition: unsupported file extension for loading references from");
89
        }
90
91
        if (!is_array($data)) {
92
            throw new \Exception("Invalid step definition: file does not contain an array of key/value pairs");
93
        }
94
95
        foreach ($data as $refName => $value) {
96
            if (preg_match('/%.+%$/', $value)) {
97
                $value = $this->container->getParameter(trim($value, '%'));
98
            }
99
            $this->referenceResolver->addReference($refName, $value, $overwrite);
100
        }
101
    }
102
103
}
104