GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RouteGenerator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 103
c 0
b 0
f 0
ccs 31
cts 32
cp 0.9688
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B generateRouteFromText() 0 15 5
A generateUniqueRoute() 0 20 3
A hasRoute() 0 10 3
A normalizeRoute() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Tadcka package.
5
 *
6
 * (c) Tadas Gliaubicas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tadcka\Component\Routing\Generator;
13
14
use Ferrandini\Urlizer;
15
use Tadcka\Component\Routing\Exception\RoutingRuntimeException;
16
use Tadcka\Component\Routing\Model\Manager\RouteManagerInterface;
17
use Tadcka\Component\Routing\Model\RouteInterface;
18
19
/**
20
 * @author Tadas Gliaubicas <[email protected]>
21
 *
22
 * @since 8/1/14 10:57 AM
23
 */
24
class RouteGenerator
25
{
26
    /**
27
     * @var RouteManagerInterface
28
     */
29
    private $routeManager;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param RouteManagerInterface $routeManager
35
     */
36 4
    public function __construct(RouteManagerInterface $routeManager)
37
    {
38 4
        $this->routeManager = $routeManager;
39 4
    }
40
41
    /**
42
     * Generate route from text.
43
     *
44
     * @param string $text
45
     * @param bool $withSlash
46
     *
47
     * @return string
48
     */
49 4
    public function generateRouteFromText($text, $withSlash = false)
50
    {
51 4
        if ($withSlash) {
52 1
            $result = '';
53 1
            foreach (explode('/', $text) as $value) {
54 1
                if ('' !== $value = trim($value)) {
55 1
                    $result .= '/' . Urlizer::urlize($value);
56 1
                }
57 1
            }
58
59 1
            return $result ?: '/';
60
        }
61
62 3
        return $this->normalizeRoute(Urlizer::urlize($text));
63
    }
64
65
    /**
66
     * Generate unique route.
67
     *
68
     * @param RouteInterface $route
69
     * @param bool $withSlash
70
     *
71
     * @return null|RouteInterface
72
     */
73 2
    public function generateUniqueRoute(RouteInterface $route, $withSlash = false)
74
    {
75 2
        $originalRoutePattern = $this->generateRouteFromText($route->getRoutePattern(), $withSlash);
76
77 2
        if ($originalRoutePattern) {
78 2
            $key = 0;
79 2
            $routePattern = $this->normalizeRoute($originalRoutePattern);
80
81 2
            while ($this->hasRoute($routePattern, $route->getName())) {
82 1
                $key++;
83 1
                $routePattern = $originalRoutePattern . '-' . $key;
84 1
            }
85
86 1
            $route->setRoutePattern($routePattern);
87
88 1
            return $route;
89
        }
90
91
        return null;
92
    }
93
94
    /**
95
     * Has route.
96
     *
97
     * @param string $routePattern
98
     * @param string $routeName
99
     *
100
     * @return bool
101
     *
102
     * @throws RoutingRuntimeException
103
     */
104 2
    private function hasRoute($routePattern, $routeName)
105
    {
106 2
        if (!$routeName) {
107 1
            throw new RoutingRuntimeException('Route name is empty!');
108
        }
109
110 1
        $route = $this->routeManager->findByRoutePattern($routePattern);
111
112 1
        return ((null !== $route) && ($routeName !== $route->getName()));
113
    }
114
115
    /**
116
     * Normalize route.
117
     *
118
     * @param string $route
119
     *
120
     * @return string
121
     */
122 3
    private function normalizeRoute($route)
123
    {
124 3
        return '/' . ltrim(trim($route), '/');
125
    }
126
}
127