Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

PagePartConfigurationParser   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 120
Duplicated Lines 17.5 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 21
loc 120
ccs 42
cts 49
cp 0.8571
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C parse() 0 60 13
A getValue() 20 20 4

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
namespace Kunstmaan\PagePartBundle\PagePartConfigurationReader;
4
5
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfigurator;
6
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfiguratorInterface;
7
use Symfony\Component\HttpKernel\KernelInterface;
8
use Symfony\Component\Yaml\Yaml;
9
10
class PagePartConfigurationParser implements PagePartConfigurationParserInterface
11
{
12
    /**
13
     * @var KernelInterface
14
     */
15
    private $kernel;
16
17
    private $presets = [];
18
19
    private $stack = [];
20
21
    /**
22
     * @param KernelInterface $kernel
23
     * @param array           $presets
24
     */
25 4
    public function __construct(KernelInterface $kernel, array $presets = [])
26
    {
27 4
        $this->kernel = $kernel;
28 4
        $this->presets = $presets;
29 4
    }
30
31
    /**
32
     * This will read the $name file and parse it to the PageTemplate
33
     *
34
     * @param string                               $name
35
     * @param PagePartAdminConfiguratorInterface[] $existing
36
     *
37
     * @return PagePartAdminConfiguratorInterface
38
     *
39
     * @throws \Exception
40
     */
41 4
    public function parse($name, array $existing = [])
42
    {
43 4
        if (\in_array($name, $this->stack)) {
44 1
            throw new \RuntimeException(sprintf('Recursion detected when parsing %s -> %s', implode(' -> ', $this->stack), $name));
45
        }
46 4
        $this->stack[] = $name;
47
48 4
        $value = $this->getValue($name);
49
50 4
        if (!\array_key_exists('types', $value)) {
51 1
            $value['types'] = [];
52
        }
53
54 4
        if (\array_key_exists('extends', $value)) {
55 2
            $namespace = '';
56 2
            if (false !== strpos($name, ':')) {
57
                $namespace = substr($name, 0, strpos($name, ':') + 1);
58
            }
59
60 2
            foreach ((array) $value['extends'] as $extend) {
61 2
                if (false === strpos($extend, ':')) {
62 2
                    $extend = $namespace . $extend;
63
                }
64
65 2
                if (false === isset($existing[$extend])) {
66 2
                    $existing[$extend] = $this->parse($extend, $existing);
67
                }
68
69 1
                $value['types'] = array_merge($existing[$extend]->getPossiblePagePartTypes(), $value['types']);
70
            }
71
        }
72
73 3
        $types = [];
74 3
        foreach ($value['types'] as $type) {
75 3
            if ('' === (string) $type['class']) {
76
                unset($types[$type['name']]);
77
78
                continue;
79
            }
80
81 3
            $types[$type['name']] = ['name' => $type['name'], 'class' => $type['class'], 'preview' => \array_key_exists('preview', $type) ? $type['preview'] : ''];
82 3
            if (isset($type['pagelimit'])) {
83
                $types[$type['name']]['pagelimit'] = $type['pagelimit'];
84
            }
85
        }
86
87 3
        $result = new PagePartAdminConfigurator();
88 3
        $result->setName($value['name']);
89 3
        $result->setInternalName($name);
90 3
        $result->setPossiblePagePartTypes(array_values($types));
91 3
        $result->setContext($value['context']);
92
93 3
        if (isset($value['widget_template'])) {
94
            $result->setWidgetTemplate($value['widget_template']);
95
        }
96
97 3
        array_pop($this->stack);
98
99 3
        return $result;
100
    }
101
102
    /**
103
     * @param string $name
104
     *
105
     * @return array
106
     *
107
     * @throws \Exception
108
     */
109 4 View Code Duplication
    private function getValue(string $name)
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...
110
    {
111 4
        if (isset($this->presets[$name])) {
112 3
            return $this->presets[$name];
113
        }
114
115
        // if we use the old flow (sf3), the value can be stored in it's own yml file
116 1
        if (strpos($name, ':')) {
117 1
            $nameParts = explode(':', $name);
118 1
            if (2 !== \count($nameParts)) {
119
                throw new \Exception(sprintf('Malformed namespaced configuration name "%s" (expecting "namespace:pagename").', $name));
120
            }
121 1
            list($namespace, $name) = $nameParts;
122 1
            $path = $this->kernel->locateResource('@' . $namespace . '/Resources/config/pageparts/' . $name . '.yml');
123
124 1
            return Yaml::parse(file_get_contents($path));
125
        }
126
127
        throw new \Exception(sprintf('Non existing pageparts preset "%s".', $name));
128
    }
129
}
130