UrlHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 2
cbo 2
dl 0
loc 145
ccs 23
cts 23
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 7 2
A setRequest() 0 7 2
A getRequest() 0 4 1
A getRoute() 0 4 1
A getFailedRoute() 0 4 1
A generate() 0 4 1
A generateRaw() 0 4 1
1
<?php
2
/**
3
 * View Helper for Aura\Router
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  ViewHelper
13
 * @package   Jnjxp\UrlHelper
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      http://github.com/jnjxp/jnjxp.urlhelper
18
 */
19
20
namespace Jnjxp\UrlHelper;
21
22
use Aura\Router\Generator;
23
use Aura\Router\Matcher;
24
25
use Psr\Http\Message\ServerRequestInterface as Request;
26
27
/**
28
 * UrlHelper
29
 *
30
 * @category ViewHelper
31
 * @package  Jnjxp\UrlHelper
32
 * @author   Jake Johns <[email protected]>
33
 * @license  http://jnj.mit-license.org/ MIT License
34
 * @link     http://github.com/jnjxp/jnjxp.urlhelper
35
 */
36
class UrlHelper
37
{
38
    /**
39
     * Generates URL paths from routes.
40
     *
41
     * @var Generator
42
     *
43
     * @access protected
44
     */
45
    protected $generator;
46
47
    /**
48
     * Contains Matched and Failed route after routing
49
     *
50
     * @var Matcher
51
     *
52
     * @access protected
53
     */
54
    protected $matcher;
55
56
    /**
57
     * Request
58
     *
59
     * @var Request
60
     *
61
     * @access protected
62
     */
63
    protected $request;
64
65
    /**
66
     * __construct
67
     *
68
     * @param Generator $generator route generator
69
     * @param Matcher   $matcher   route matcher
70
     *
71
     * @access public
72
     */
73 10
    public function __construct(Generator $generator, Matcher $matcher)
74
    {
75 10
        $this->generator = $generator;
76 10
        $this->matcher = $matcher;
77 10
    }
78
79
    /**
80
     * Generate URL or return helper
81
     *
82
     * @param string $name route name
83
     * @param array  $data array of params
84
     *
85
     * @return $this|string
86
     *
87
     * @access public
88
     */
89 2
    public function __invoke($name = null, $data = [])
90
    {
91 2
        if (null === $name) {
92 1
            return $this;
93
        }
94 1
        return $this->generate($name, $data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->generate($name, $data); of type string|false adds false to the return on line 94 which is incompatible with the return type documented by Jnjxp\UrlHelper\UrlHelper::__invoke of type Jnjxp\UrlHelper\UrlHelper|string. It seems like you forgot to handle an error condition.
Loading history...
95
    }
96
97
    /**
98
     * Set current request
99
     *
100
     * @param Request $request current request
101
     *
102
     * @return Request
103
     * @throws Exception if request already set
104
     *
105
     * @access public
106
     */
107 2
    public function setRequest(Request $request)
108
    {
109 2
        if (null !== $this->request) {
110 1
            throw new \Exception('Request already set');
111
        }
112 2
        $this->request = $request;
113 2
    }
114
115
    /**
116
     * Get current request
117
     *
118
     * @return Request
119
     *
120
     * @access public
121
     */
122 1
    public function getRequest()
123
    {
124 1
        return $this->request;
125
    }
126
127
    /**
128
     * Get current route
129
     *
130
     * @return \Aura\Router\Route|false
131
     *
132
     * @access public
133
     */
134 1
    public function getRoute()
135
    {
136 1
        return $this->matcher->getMatchedRoute();
137
    }
138
139
    /**
140
     * Get failed route
141
     *
142
     * @return \Aura\Router\Route
143
     *
144
     * @access public
145
     */
146 1
    public function getFailedRoute()
147
    {
148 1
        return $this->matcher->getFailedRoute();
149
    }
150
151
    /**
152
     * Generate URL
153
     *
154
     * @param string $name name of route
155
     * @param array  $data params
156
     *
157
     * @return string
158
     *
159
     * @access public
160
     */
161 2
    public function generate($name, $data = [])
162
    {
163 2
        return $this->generator->generate($name, $data);
164
    }
165
166
    /**
167
     * Generate raw URL
168
     *
169
     * @param string $name name of route
170
     * @param array  $data params
171
     *
172
     * @return string
173
     *
174
     * @access public
175
     */
176 1
    public function generateRaw($name, $data = [])
177
    {
178 1
        return $this->generator->generateRaw($name, $data);
179
    }
180
}
181