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

PageTemplateConfigurationParser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 15.87 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 20
loc 126
ccs 36
cts 45
cp 0.8
rs 10
c 0
b 0
f 0

5 Methods

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