Completed
Push — master ( 801d67...caf90e )
by Phil
01:30 queued 16s
created

Route::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 46
    public function __construct(string $method, string $path, $handler)
48
    {
49 46
        $this->method  = $method;
50 46
        $this->path    = $path;
51 46
        $this->handler = $handler;
52 46
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 16
    public function process(
58
        ServerRequestInterface $request,
59
        RequestHandlerInterface $requestHandler
60
    ) : ResponseInterface {
61 16
        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 30
    public function getCallable(?ContainerInterface $container = null) : callable
74
    {
75 30
        $callable = $this->handler;
76
77 30
        if (is_string($callable) && strpos($callable, '::') !== false) {
78 4
            $callable = explode('::', $callable);
79
        }
80
81 30
        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
82 2
            $callable = [$callable[0], $callable[1]];
83
        }
84
85 30
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
86 4
            $callable = [$this->resolveClass($container, $callable[0]), $callable[1]];
87
        }
88
89 30
        if (is_string($callable) && method_exists($callable, '__invoke')) {
90
            $callable = $this->resolveClass($container, $callable);
91
        }
92
93 30
        if (! is_callable($callable)) {
94 2
            throw new InvalidArgumentException('Could not resolve a callable for this route');
95
        }
96
97 28
        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)) {
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 18
    public function getVars() : array
123
    {
124 18
        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 18
    public function setVars(array $vars) : self
135
    {
136 18
        $this->vars = $vars;
137
138 18
        return $this;
139
    }
140
141
    /**
142
     * Get the parent group
143
     *
144
     * @return \League\Route\RouteGroup
145
     */
146 18
    public function getParentGroup() : ?RouteGroup
147
    {
148 18
        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 8
        $prefix = $group->getPrefix();
162 8
        $path = $this->getPath();
163 8
        if (strcmp($prefix, substr($path, 0, strlen($prefix))) != 0) {
164
            $path = $prefix . $path;
165
            $this->path = $path;
166
        }
167
168 8
        return $this;
169
    }
170
171
    /**
172
     * Get the path
173
     *
174
     * @return string
175
     */
176 24
    public function getPath() : string
177
    {
178 24
        return $this->path;
179
    }
180
181
    /**
182
     * Get the HTTP method
183
     *
184
     * @return string
185
     */
186 24
    public function getMethod() : string
187
    {
188 24
        return $this->method;
189
    }
190
}
191