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::hasRoute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
crap 3
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