RouteBuilder   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 170
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 14 3
A getParser() 0 7 2
A setParser() 0 5 1
A getData() 0 16 3
A getRouteFactory() 0 7 2
A setRouteFactory() 0 5 1
A getYmlData() 0 9 2
A setMapDefaults() 0 9 3
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Router;
11
12
use Aura\Router\Map;
13
use Slick\Mvc\Exception\RoutesFileNotFoundException;
14
use Slick\Mvc\Exception\RoutesFileParseException;
15
use Slick\Mvc\Router\Builder\RouteFactory;
16
use Symfony\Component\Yaml\Exception\ParseException;
17
use Symfony\Component\Yaml\Parser;
18
19
/**
20
 * Route Builder
21
 *
22
 * @package Slick\Mvc\Router
23
 * @author  Filipe Silva <[email protected]>
24
 */
25
class RouteBuilder
26
{
27
28
    /**
29
     * @var Map
30
     */
31
    protected $map;
32
33
    /**
34
     * @var string
35
     */
36
    protected $routesFile;
37
38
    /**
39
     * @var Parser
40
     */
41
    protected $parser;
42
43
    /**
44
     * @var mixed
45
     */
46
    protected $data;
47
48
    /**
49
     * @var RouteFactory
50
     */
51
    protected $routeFactory;
52
53
    /**
54
     * RouteBuilder constructor.
55
     *
56
     * @param string $routesFile
57
     */
58 14
    public function __construct($routesFile)
59
    {
60 14
        $this->routesFile = $routesFile;
61 14
    }
62
63
    /**
64
     * Map builder handler
65
     *
66
     * @see http://auraphp.com/packages/Aura.Router/custom-maps.html#2.5.3
67
     *
68
     * @param Map $map
69
     *
70
     * @return $this|self|RouteBuilder
71
     */
72 2
    public function build(Map $map)
73
    {
74 2
        $this->map = $map;
75 2
        $data = $this->getData();
76 2
        $this->setMapDefaults($data);
77 2
        $routes = (array_key_exists('routes', $data))
78 2
            ? $data['routes']
79 2
            : [];
80 2
        foreach ($routes as $name => $definition) {
81 2
            $this->getRouteFactory()
82 2
                ->parse($name, $definition, $this->map);
83 1
        }
84 2
        return $this;
85
    }
86
87
    /**
88
     * Gets the YML parser. Creates it if its null.
89
     *
90
     * @return Parser
91
     */
92 8
    public function getParser()
93
    {
94 8
        if (is_null($this->parser)) {
95 8
            $this->setParser(new Parser());
96 4
        }
97 8
        return $this->parser;
98
    }
99
100
    /**
101
     * Sets the YML parser
102
     *
103
     * @param Parser $parser
104
     *
105
     * @return $this|self|RouteBuilder
106
     */
107 8
    public function setParser(Parser $parser)
108
    {
109 8
        $this->parser = $parser;
110 8
        return $this;
111
    }
112
113
    /**
114
     * Returns parsed data from YML
115
     *
116
     * If no data is present the YML parser parses it and its values are
117
     * assigned to the data field.
118
     *
119
     * @return mixed
120
     */
121 8
    public function getData()
122
    {
123 8
        if (is_null($this->data)) {
124
            try {
125 8
                $this->data = $this->getParser()->parse($this->getYmlData());
126 6
            } catch (ParseException $exp) {
127 2
                throw new RoutesFileParseException(
128
                    "Fail to parse routes file: ".
129 2
                    $exp->getMessage(),
130 2
                    0,
131
                    $exp
132 1
                );
133
            }
134 2
        }
135 4
        return $this->data;
136
    }
137
138
    /**
139
     * Get route factory
140
     *
141
     * @return RouteFactory
142
     */
143 4
    public function getRouteFactory()
144
    {
145 4
        if (null == $this->routeFactory) {
146 2
            $this->setRouteFactory(new RouteFactory());
147 1
        }
148 4
        return $this->routeFactory;
149
    }
150
151
    /**
152
     * Set route factory
153
     *
154
     * @param RouteFactory $routeFactory
155
     *
156
     * @return RouteBuilder|self|$this
157
     */
158 4
    public function setRouteFactory(RouteFactory $routeFactory)
159
    {
160 4
        $this->routeFactory = $routeFactory;
161 4
        return $this;
162
    }
163
164
165
    /**
166
     * Gets routes files definition content
167
     *
168
     * @return string
169
     */
170 8
    protected function getYmlData()
171
    {
172 8
        if (!is_file($this->routesFile)) {
173 2
            throw new RoutesFileNotFoundException(
174 2
                "Routes file '{$this->routesFile}' is not found."
175 1
            );
176
        }
177 6
        return file_get_contents($this->routesFile);
178
    }
179
180
    /**
181
     * Gets the YML parser data and sets the map default defined
182
     *
183
     * @param array $data
184
     */
185 2
    protected function setMapDefaults(array $data)
186
    {
187 2
        $defaults = ['tokens', 'defaults', 'host', 'accepts'];
188 2
        foreach ($data as $name => $value) {
189 2
            if (in_array($name, $defaults)) {
190 2
                $this->map->$name($value);
191 1
            }
192 1
        }
193
    }
194
}