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

ReferenceExecutor   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 162
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 27
loc 162
c 0
b 0
f 0
wmc 34
lcom 1
cbo 6
rs 9.2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 16 16 3
B set() 0 17 5
C load() 0 49 13
D save() 11 38 9
A dump() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
namespace Kaliop\eZMigrationBundle\Core\Executor;
5
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\Yaml\Yaml;
8
use Symfony\Component\VarDumper\VarDumper;
9
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
10
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface;
11
use Kaliop\eZMigrationBundle\API\EnumerableReferenceResolverInterface;
12
13
class ReferenceExecutor extends AbstractExecutor
14
{
15
    protected $supportedStepTypes = array('reference');
16
    protected $supportedActions = array('set', 'load', 'save', 'dump');
17
18
    protected $container;
19
    /** @var ReferenceResolverBagInterface $referenceResolver */
20
    protected $referenceResolver;
21
22
    public function __construct(ContainerInterface $container, ReferenceResolverBagInterface $referenceResolver)
23
    {
24
        $this->container = $container;
25
        $this->referenceResolver = $referenceResolver;
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
    protected function set($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...
51
    {
52
        if (!isset($dsl['identifier'])) {
53
            throw new \Exception("Invalid step definition: miss 'identifier' for setting reference");
54
        }
55
        if (!isset($dsl['value'])) {
56
            throw new \Exception("Invalid step definition: miss 'value' for setting reference");
57
        }
58
        $value = $dsl['value'];
59
        if (preg_match('/%.+%$/', $value)) {
60
            $value = $this->container->getParameter(trim($value, '%'));
61
        }
62
        $overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false;
63
        $this->referenceResolver->addReference($dsl['identifier'], $value, $overwrite);
64
65
        return $value;
66
    }
67
68
    protected function load($dsl, $context)
69
    {
70
        if (!isset($dsl['file'])) {
71
            throw new \Exception("Invalid step definition: miss 'file' for loading references");
72
        }
73
        $fileName = $dsl['file'];
74
75
        $overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false;
76
77
        $fileName = str_replace('{ENV}', $this->container->get('kernel')->getEnvironment(), $fileName);
78
79
        if (!is_file($fileName) && is_file(dirname($context['path']) . '/references/' . $fileName)) {
80
            $fileName = dirname($context['path']) . '/references/' . $fileName;
81
        }
82
83
        if (!is_file($fileName)) {
84
            throw new \Exception("Invalid step definition: invalid file '$fileName' for loading references");
85
        }
86
        $data = file_get_contents($fileName);
87
88
        $ext = pathinfo($dsl['file'], PATHINFO_EXTENSION);
89
        switch ($ext) {
90
            case 'json':
91
                $data = json_decode($data, true);
92
                break;
93
            case 'yml':
94
            case 'yaml':
95
                $data = Yaml::parse($data);
96
                break;
97
            default:
98
                throw new \Exception("Invalid step definition: unsupported file extension for loading references from");
99
        }
100
101
        if (!is_array($data)) {
102
            throw new \Exception("Invalid step definition: file does not contain an array of key/value pairs");
103
        }
104
105
        foreach ($data as $refName => $value) {
106
            if (preg_match('/%.+%$/', $value)) {
107
                $value = $this->container->getParameter(trim($value, '%'));
108
            }
109
110
            if (!$this->referenceResolver->addReference($refName, $value, $overwrite)) {
111
                throw new \Exception("Failed adding to Reference Resolver the reference: $refName");
112
            }
113
        }
114
115
        return $data;
116
    }
117
118
    /**
119
     * @todo find a smart way to allow saving the references file next to the current migration
120
     */
121
    protected function save($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...
122
    {
123
        if (!isset($dsl['file'])) {
124
            throw new \Exception("Invalid step definition: miss 'file' for saving references");
125
        }
126
        $fileName = $dsl['file'];
127
128
        $overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false;
129
130
        $fileName = str_replace('{ENV}', $this->container->get('kernel')->getEnvironment(), $fileName);
131
132
        if (is_file($fileName) && !$overwrite) {
133
            throw new \Exception("Invalid step definition: file '$fileName' for saving references already exists");
134
        }
135
136
        if (! $this->referenceResolver instanceof EnumerableReferenceResolverInterface) {
137
            throw new \Exception("Can not save references as resolver is not enumerable");
138
        }
139
140
        $data = $this->referenceResolver->listReferences();
141
142
        $ext = pathinfo($fileName, PATHINFO_EXTENSION);
143 View Code Duplication
        switch ($ext) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
144
            case 'json':
145
                $data = json_encode($data, JSON_PRETTY_PRINT);
146
                break;
147
            case 'yml':
148
            case 'yaml':
149
                $data = Yaml::dump($data);
150
                break;
151
            default:
152
                throw new \Exception("Invalid step definition: unsupported file extension for saving references to");
153
        }
154
155
        file_put_contents($fileName, $data);
156
157
        return $data;
158
    }
159
160
    protected function dump($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...
161
    {
162
        if (!isset($dsl['identifier'])) {
163
            throw new \Exception("Invalid step definition: miss 'identifier' for dumping reference");
164
        }
165
        if (!$this->referenceResolver->isReference($dsl['identifier'])) {
166
            throw new \Exception("Invalid step definition: identifier '{$dsl['identifier']}' is not a reference");
167
        }
168
        VarDumper::dump($dsl['identifier']);
169
        $value = $this->referenceResolver->resolveReference($dsl['identifier']);
170
        VarDumper::dump($value);
171
172
        return $value;
173
    }
174
}
175