Passed
Push — master ( 559900...ef8c69 )
by Alexey
10:10
created

RouteDispatcher::next()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.3197

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 4
cts 11
cp 0.3636
crap 5.3197
rs 9.4285
c 2
b 0
f 1
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Routing;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Venta\Contracts\Adr\Payload;
8
use Venta\Contracts\Container\Invoker;
9
use Venta\Contracts\Routing\Route as RouteContract;
10
use Venta\Contracts\Routing\RouteDispatcher as RouteDispatcherContract;
11
12
/**
13
 * Class RouteDispatcher
14
 *
15
 * @package Venta\Routing
16
 */
17
final class RouteDispatcher implements RouteDispatcherContract
18
{
19
20
    /**
21
     * @var Invoker
22
     */
23
    private $invoker;
24
25
    /**
26
     * @var RouteContract
27
     */
28
    private $route;
29
30
    /**
31
     * RouteDispatcher constructor.
32
     *
33
     * @param Invoker $invoker
34
     * @param RouteContract $route
35
     */
36 3
    public function __construct(Invoker $invoker, RouteContract $route)
37
    {
38 3
        $this->invoker = $invoker;
39 3
        $this->route = $route;
40 3
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 1
    public function next(ServerRequestInterface $request): ResponseInterface
46
    {
47
        // Add current route to the request.
48 1
        $request = $request->withAttribute('route', $this->route);
49
50 1
        if ($this->invoker->isCallable($this->route->domain())) {
51
            if ($this->invoker->isCallable([$this->route->input(), 'process'])) {
52
                $arguments = $this->invoker->call([$this->route->input(), 'process'], [$request]);
53
            }
54
            /** @var Payload $payload */
55
            $payload = $this->invoker->call($this->route->domain(), $arguments ?? []);
56
        }
57 1
        return $this->invoker->call([$this->route->responder(), 'run'], [$request, $payload ?? null]);
58
    }
59
60
}