Completed
Push — master ( df0330...a615ca )
by Gabriel
06:40 queued 02:30
created

HasBaseRoutesTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
dl 0
loc 130
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generateIndexRoute() 0 18 1
A generateLiteralRoute() 0 13 2
A generateGenericRoute() 0 13 1
B generateRouteClassName() 0 24 6
A generateStandardRoute() 0 17 3
1
<?php
2
3
namespace Nip\Router\RouteFactory;
4
5
use Nip\Router\Route\LiteralRoute;
6
use Nip\Router\Route\Route;
7
use Nip\Router\Route\StandardRoute;
8
use Nip\Router\RouteCollection;
9
10
/**
11
 * Trait HasBaseRoutesTrait
12
 * @package Nip\Router\RouteFactory
13
 */
14
trait HasBaseRoutesTrait
15
{
16
17
    /**
18
     * @param RouteCollection $collection
19
     * @param $name
20
     * @param $class
21
     * @param string $mapPrefix
22
     * @param array $params
23
     * @return mixed
24
     */
25 10
    public static function generateIndexRoute(
26
        $collection,
27
        $name,
28
        $class = null,
29
        $mapPrefix = '',
30
        $params = []
31
    ) {
32 10
        $params["controller"] = "index";
33 10
        $params["action"] = "index";
34
35 10
        self::generateLiteralRoute(
36 10
            $collection, $name . '.slash', $class, $mapPrefix, '', $params
37
        );
38
39 10
        return self::generateLiteralRoute(
40 10
            $collection, $name, $class, $mapPrefix, '/', $params
41
        );
42
    }
43
44
    /**
45
     * @param RouteCollection $collection
46
     * @param $name
47
     * @param $class
48
     * @param string $mapPrefix
49
     * @param string $map
50
     * @param array $params
51
     * @return mixed
52
     */
53 16
    public static function generateLiteralRoute(
54
        $collection,
55
        $name,
56
        $class = null,
57
        $mapPrefix = '',
58
        $map = '/',
59
        $params = []
60
    ) {
61 16
        $map = $mapPrefix . $map;
62 16
        $class = empty($class) ? LiteralRoute::class : $class;
63
64 16
        return self::generateGenericRoute($collection, $name, $class, $map, $params);
65
    }
66
67
    /**
68
     * @param RouteCollection $collection
69
     * @param $name
70
     * @param $class
71
     * @param string $map
72
     * @param array $params
73
     * @return mixed
74
     */
75 28
    public static function generateGenericRoute(
76
        $collection,
77
        $name,
78
        $class = null,
79
        $map,
80
        $params = []
81
    ) {
82 28
        $map = str_replace('//', '/', $map);
83 28
        $class = static::generateRouteClassName($class);
84
85 28
        $route = new $class($map, $params);
86 28
        return $collection->addRoute($route, $name);
87
    }
88
89
    /**
90
     * @param RouteCollection $collection
91
     * @param $name
92
     * @param $class
93
     * @param string $mapPrefix
94
     * @param string $map
95
     * @param array $params
96
     */
97 21
    public static function generateStandardRoute(
98
        $collection,
99
        $name,
100
        $class = null,
101
        $mapPrefix = '',
102
        $map = null,
103
        $params = []
104
    ) {
105 21
        $class = empty($class) ? StandardRoute::class : $class;
106 21
        $map = $map ? $map : '/{controller}/{action?index}';
107
108 21
        self::generateGenericRoute($collection, $name, $class, $mapPrefix . $map, $params);
109
110 21
        $params['action'] = 'index';
111 21
        self::generateGenericRoute($collection, $name . '.index', $class, $mapPrefix . '/{controller}/',
112
            $params);
113 21
    }
114
115
    /**
116
     * @param null $class
117
     * @return string
118
     */
119 28
    protected static function generateRouteClassName($class = null)
120
    {
121 28
        if (empty($class)) {
122
            return Route::class;
123
        }
124
125 28
        if (class_exists($class)) {
126 24
            return $class;
127
        }
128
129
        $maps = [
130 4
            LiteralRoute::class => ['_Literal', '\Literal'],
131
            StandardRoute::class => ['_Standard', '\Standard']
132
        ];
133 4
        foreach ($maps as $routeClass => $needles) {
134 4
            foreach ($needles as $needle) {
135 4
                if (strpos($class, $needle) !== false) {
136 4
                    return $routeClass;
137
                }
138
            }
139
        }
140
141
        return Route::class;
142
    }
143
}
144