RouteFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 84
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 5 1
A simpleRoute() 0 7 2
A createRoute() 0 13 3
A setRouteProperties() 0 11 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\Builder;
11
12
use Aura\Router\Map;
13
use Aura\Router\Route;
14
15
/**
16
 * RouteFactory
17
 *
18
 * @package Slick\Mvc\Router\Builder
19
 * @author  Filipe Silva <[email protected]>
20
 */
21
class RouteFactory implements FactoryInterface
22
{
23
24
    /**
25
     * @var Map
26
     */
27
    protected $map;
28
29
    /**
30
     * Receives an array with parameters to create a route or route group
31
     *
32
     * @param string       $name The route name
33
     * @param string|array $data Meta data fo the route
34
     * @param Map          $map  The route map to populate
35
     *
36
     * @return Route
37
     */
38 12
    public function parse($name, $data, Map $map)
39
    {
40 12
        $this->map = $map;
41 12
        return $this->simpleRoute($name, $data);
42
    }
43
44
    /**
45
     * Check if the data is a simple string, create a get with it
46
     *
47
     * If not a string pass the data to the construction chain where the route
48
     * will be set with the data array passed
49
     *
50
     * @param string       $name The route name
51
     * @param string|array $data Meta data fo the route
52
     *
53
     * @return Route
54
     */
55 12
    protected function simpleRoute($name, $data)
56
    {
57 12
        if (is_string($data)) {
58 2
            return $this->map->get($name, $data);
59
        }
60 10
        return $this->createRoute($name, $data);
61
    }
62
63
    /**
64
     * Route construct chain start
65
     *
66
     * @param string $name The route name
67
     * @param array  $data Meta data fo the route
68
     *
69
     * @return Route
70
     */
71 10
    protected function createRoute($name, array $data)
72
    {
73 10
        $route = $this->map->route($name, $data['path']);
74 10
        $allows = array_key_exists('allows', $data)
75 8
            ? $data['allows']
76 10
            : [];
77 10
        $method = ['GET'];
78 10
        if (array_key_exists('method', $data)) {
79 4
            $method = [$data['method']];
80 2
        }
81 10
        $route->allows(array_merge($allows, $method));
82 10
        return $this->setRouteProperties($route, $data);
83
    }
84
85
    /**
86
     * Parses data array to set route properties
87
     *
88
     * @param Route $route
89
     * @param array $data
90
     *
91
     * @return Route
92
     */
93 10
    protected function setRouteProperties(Route $route, array $data)
94
    {
95 10
        $methods = get_class_methods(Route::class);
96 10
        $methods = array_diff($methods, ["allows", "path"]);
97 10
        foreach($data as $method => $args) {
98 10
            if (in_array($method, $methods)) {
99 6
                $route->$method($args);
100 1
            }
101 5
        }
102 10
        return $route;
103
    }
104
}