Completed
Push — l10n_master ( 453465...12070f )
by Jeroen
15:21 queued 08:31
created

PagePartConfigurationParser::getValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 9
cts 12
cp 0.75
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.25
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
    private function getValue(string $name)
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").',
120
                    $name));
121
            }
122 1
            list($namespace, $name) = $nameParts;
123 1
            $path = $this->kernel->locateResource('@' . $namespace . '/Resources/config/pageparts/' . $name . '.yml');
124
125 1
            return Yaml::parse(file_get_contents($path));
126
        }
127
128
        throw new \Exception(sprintf('Non existing pageparts preset "%s".', $name));
129
    }
130
}
131