Completed
Push — master ( e223e2...f24f84 )
by Phil
21:43 queued 19:48
created

Route::getExecutionChain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2.0116
1
<?php
2
3
namespace League\Route;
4
5
use InvalidArgumentException;
6
use League\Route\ContainerAwareInterface;
7
use League\Route\ContainerAwareTrait;
8
use League\Route\Http\RequestAwareInterface;
9
use League\Route\Http\ResponseAwareInterface;
10
use League\Route\Middleware\ExecutionChain;
11
use League\Route\Middleware\StackAwareInterface as MiddlewareAwareInterface;
12
use League\Route\Middleware\StackAwareTrait as MiddlewareAwareTrait;
13
use League\Route\Strategy\StrategyAwareInterface;
14
use League\Route\Strategy\StrategyAwareTrait;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
18
class Route implements ContainerAwareInterface, MiddlewareAwareInterface, StrategyAwareInterface
19
{
20
    use ContainerAwareTrait;
21
    use MiddlewareAwareTrait;
22
    use RouteConditionTrait;
23
    use StrategyAwareTrait;
24
25
    /**
26
     * @var string|callable
27
     */
28
    protected $callable;
29
30
    /**
31
     * @var \League\Route\RouteGroup
32
     */
33
    protected $group;
34
35
    /**
36
     * @var string[]
37
     */
38
    protected $methods = [];
39
40
    /**
41
     * @var string
42
     */
43
    protected $path;
44
45
    /**
46
     * Dispatch the route via the attached strategy.
47
     *
48
     * @param array $vars
49
     *
50
     * @return \League\Route\Middleware\ExecutionChain
51
     */
52 15
    public function getExecutionChain(array $vars)
53
    {
54 15
        $callable = $this->getStrategy()->getCallable($this, $vars);
55
56 15
        $execChain = (new ExecutionChain)->middleware($callable);
57
58 15
        foreach ($this->getMiddlewareStack() as $middleware) {
59
            $execChain->middleware($middleware);
60 10
        }
61
62 15
        return $execChain;
63
    }
64
65
    /**
66
     * Get the callable.
67
     *
68
     * @throws \RuntimeException
69
     *
70
     * @return callable
71
     */
72 33
    public function getCallable()
73
    {
74 33
        $callable = $this->callable;
75
76 33
        if (is_string($callable) && strpos($callable, '::') !== false) {
77 6
            $callable = explode('::', $callable);
78 4
        }
79
80 33
        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
81 3
            $callable = [$callable[0], $callable[1]];
82 2
        }
83
84 33
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
85 6
            $class = ($this->getContainer()->has($callable[0]))
86 5
                   ? $this->getContainer()->get($callable[0])
87 6
                   : new $callable[0];
88
89 6
            $callable = [$class, $callable[1]];
90 4
        }
91
92 33
        if (! is_callable($callable)) {
93 3
            throw new InvalidArgumentException('Could not resolve a callable for this route');
94
        }
95
96 30
        return $callable;
97
    }
98
99
    /**
100
     * Set the callable.
101
     *
102
     * @param string|callable $callable
103
     *
104
     * @return \League\Route\Route
105
     */
106 48
    public function setCallable($callable)
107
    {
108 48
        $this->callable = $callable;
109
110 48
        return $this;
111
    }
112
113
    /**
114
     * Get the parent group.
115
     *
116
     * @return \League\Route\RouteGroup
117
     */
118 3
    public function getParentGroup()
119
    {
120 3
        return $this->group;
121
    }
122
123
    /**
124
     * Set the parent group.
125
     *
126
     * @param \League\Route\RouteGroup $group
127
     *
128
     * @return \League\Route\Route
129
     */
130 6
    public function setParentGroup(RouteGroup $group)
131
    {
132 6
        $this->group = $group;
133
134 6
        return $this;
135
    }
136
137
    /**
138
     * Get the path.
139
     *
140
     * @return string
141
     */
142 27
    public function getPath()
143
    {
144 27
        return $this->path;
145
    }
146
147
    /**
148
     * Set the path.
149
     *
150
     * @param string $path
151
     *
152
     * @return \League\Route\Route
153
     */
154 33
    public function setPath($path)
155
    {
156 33
        $this->path = $path;
157
158 33
        return $this;
159
    }
160
161
    /**
162
     * Get the methods.
163
     *
164
     * @return string[]
165
     */
166 27
    public function getMethods()
167
    {
168 27
        return $this->methods;
169
    }
170
171
    /**
172
     * Get the methods.
173
     *
174
     * @param string[] $methods
175
     *
176
     * @return \League\Route\Route
177
     */
178 33
    public function setMethods(array $methods)
179
    {
180 33
        $this->methods = $methods;
181
182 33
        return $this;
183
    }
184
}
185