Completed
Push — master ( b3457c...c694d9 )
by Phil
02:30
created

Route   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 0
loc 173
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A process() 0 6 1
C getCallable() 0 26 12
A resolveClass() 0 8 3
A getVars() 0 4 1
A setVars() 0 6 1
A getParentGroup() 0 4 1
A setParentGroup() 0 6 1
A getPath() 0 4 1
A getMethod() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace League\Route;
4
5
use InvalidArgumentException;
6
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
7
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait};
8
use Psr\Container\ContainerInterface;
9
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
10
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
11
12
class Route implements
13
    MiddlewareInterface,
14
    MiddlewareAwareInterface,
15
    RouteConditionHandlerInterface,
16
    StrategyAwareInterface
17
{
18
    use MiddlewareAwareTrait;
19
    use RouteConditionHandlerTrait;
20
    use StrategyAwareTrait;
21
22
    /**
23
     * @var callable|string
24
     */
25
    protected $handler;
26
27
    /**
28
     * @var \League\Route\RouteGroup
29
     */
30
    protected $group;
31
32
    /**
33
     * @var string
34
     */
35
    protected $method;
36
37
    /**
38
     * @var string
39
     */
40
    protected $path;
41
42
    /**
43
     * @var array
44
     */
45
    protected $vars = [];
46
47 44
    public function __construct(string $method, string $path, $handler)
48
    {
49 44
        $this->method  = $method;
50 44
        $this->path    = $path;
51 44
        $this->handler = $handler;
52 44
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 14
    public function process(
58
        ServerRequestInterface $request,
59
        RequestHandlerInterface $requestHandler
60
    ) : ResponseInterface {
61 14
        return $this->getStrategy()->invokeRouteCallable($this, $request);
62
    }
63
64
    /**
65
     * Get the controller callable
66
     *
67
     * @param \Psr\Container\ContainerInterface|null $container
68
     *
69
     * @throws \InvalidArgumentException
70
     *
71
     * @return callable
72
     */
73 28
    public function getCallable(?ContainerInterface $container = null) : callable
74
    {
75 28
        $callable = $this->handler;
76
77 28
        if (is_string($callable) && strpos($callable, '::') !== false) {
78 4
            $callable = explode('::', $callable);
79
        }
80
81 28
        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
82 2
            $callable = [$callable[0], $callable[1]];
83
        }
84
85 28
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
86 4
            $callable = [$this->resolveClass($container, $callable[0]), $callable[1]];
87
        }
88
89 28
        if (is_string($callable) && method_exists($callable, '__invoke')) {
90
            $callable = $this->resolveClass($container, $callable);
91
        }
92
93 28
        if (! is_callable($callable)) {
94 2
            throw new InvalidArgumentException('Could not resolve a callable for this route');
95
        }
96
97 26
        return $callable;
98
    }
99
100
    /**
101
     * Get an object instance from a class name
102
     *
103
     * @param \Psr\Container\ContainerInterface|null $container
104
     * @param string $class
105
     *
106
     * @return object
107
     */
108 4
    protected function resolveClass(?ContainerInterface $container = null, string $class)
109
    {
110 4
        if ($container instanceof ContainerInterface && $container->has($class)) {
1 ignored issue
show
Bug introduced by Woody Gilk
The class Psr\Container\ContainerInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
111 2
            return $container->get($class);
112
        }
113
114 2
        return new $class();
115
    }
116
117
    /**
118
     * Return variables to be passed to route callable
119
     *
120
     * @return array
121
     */
122 16
    public function getVars() : array
123
    {
124 16
        return $this->vars;
125
    }
126
127
    /**
128
     * Set variables to be passed to route callable
129
     *
130
     * @param array $vars
131
     *
132
     * @return \League\Route\Route
133
     */
134 16
    public function setVars(array $vars) : self
135
    {
136 16
        $this->vars = $vars;
137
138 16
        return $this;
139
    }
140
141
    /**
142
     * Get the parent group
143
     *
144
     * @return \League\Route\RouteGroup
145
     */
146 16
    public function getParentGroup() : ?RouteGroup
147
    {
148 16
        return $this->group;
149
    }
150
151
    /**
152
     * Set the parent group
153
     *
154
     * @param \League\Route\RouteGroup $group
155
     *
156
     * @return \League\Route\Route
157
     */
158 8
    public function setParentGroup(RouteGroup $group) : self
159
    {
160 8
        $this->group = $group;
161
162 8
        return $this;
163
    }
164
165
    /**
166
     * Get the path
167
     *
168
     * @return string
169
     */
170 24
    public function getPath() : string
171
    {
172 24
        return $this->path;
173
    }
174
175
    /**
176
     * Get the HTTP method
177
     *
178
     * @return string
179
     */
180 24
    public function getMethod() : string
181
    {
182 24
        return $this->method;
183
    }
184
}
185