Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

PageTemplate/PageTemplateConfigurationParser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\PagePartBundle\PageTemplate;
4
5
use Symfony\Component\HttpKernel\KernelInterface;
6
use Symfony\Component\Yaml\Yaml;
7
8
class PageTemplateConfigurationParser implements PageTemplateConfigurationParserInterface
9
{
10
    /**
11
     * @var KernelInterface
12
     */
13
    private $kernel;
14
15
    private $presets = [];
16
17 2
    public function __construct(KernelInterface $kernel, array $presets = [])
18
    {
19 2
        $this->kernel = $kernel;
20 2
        $this->presets = $presets;
21 2
    }
22
23
    /**
24
     * This will read the $name file and parse it to the PageTemplate
25
     *
26
     * @param string $name
27
     *
28
     * @return PageTemplateInterface
29
     *
30
     * @throws \Exception
31
     */
32 2
    public function parse($name)
33
    {
34 2
        $rawData = $this->getRawData($name);
35
36 2
        $result = new PageTemplate();
37 2
        $result->setName($rawData['name']);
38 2
        $rows = [];
39 2
        foreach ($rawData['rows'] as $rawRow) {
40 2
            $regions = [];
41 2
            foreach ($rawRow['regions'] as $rawRegion) {
42 2
                $region = $this->buildRegion($rawRegion);
43 2
                $regions[] = $region;
44
            }
45 2
            $rows[] = new Row($regions);
46
        }
47
48 2
        $result->setRows($rows);
49 2
        $result->setTemplate($rawData['template']);
50
51 2
        return $result;
52
    }
53
54
    /**
55
     * This builds a Region out of the rawRegion from the Yaml
56
     *
57
     * @param array $rawRegion
58
     *
59
     * @return Region
60
     */
61 2
    private function buildRegion($rawRegion)
62
    {
63 2
        $children = [];
64 2
        $rows = [];
65 2
        $rawRegion = array_replace(['regions' => [], 'rows' => []], $rawRegion);
66
67 2
        foreach ($rawRegion['regions'] as $child) {
68
            $children[] = $this->buildRegion($child);
69
        }
70
71 2
        foreach ($rawRegion['rows'] as $row) {
72
            $rows[] = $this->buildRow($row);
73
        }
74
75 2
        $rawRegion = array_replace([
76 2
            'name' => null,
77
            'span' => 12,
78
            'template' => null,
79
        ], $rawRegion);
80
81 2
        return new Region($rawRegion['name'], $rawRegion['span'], $rawRegion['template'], $children, $rows);
82
    }
83
84
    /**
85
     * This builds a Row out of the rawRow from the Yaml
86
     *
87
     * @param array $rawRow
88
     *
89
     * @return Row
90
     */
91
    private function buildRow($rawRow)
92
    {
93
        $regions = [];
94
95
        foreach ($rawRow as $region) {
96
            $regions[] = $this->buildRegion($region);
97
        }
98
99
        return new Row($regions);
100
    }
101
102
    /**
103
     * @param $name
104
     *
105
     * @return array
106
     *
107
     * @throws \Exception
108
     */
109 2 View Code Duplication
    private function getRawData($name)
0 ignored issues
show
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 2
        if (isset($this->presets[$name])) {
112 1
            return $this->presets[$name];
113
        }
114
115
        // if we use the old flow (sf3), the raw data can be stored in it's own yml file
116 1
        if (strpos($name, ':')) {
117 1
            $nameParts = explode(':', $name, 2);
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/pagetemplates/' . $name . '.yml');
123
124 1
            return Yaml::parse(file_get_contents($path));
125
        }
126
127
        throw new \Exception(sprintf('Non existing template "%s".', $name));
128
    }
129
}
130