Completed
Push — master ( b9d832...421ea6 )
by Phil
06:19 queued 04:14
created

Route::getParentGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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