Passed
Pull Request — master (#60)
by
unknown
10:40
created

DefaultDispatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 5

11 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __construct() 0 5 1
A handle() 0 7 1
B prepareHandler() 0 28 9
A hp$1 ➔ process() 0 4 2
wrapCallable() 0 38 ?
A hp$1 ➔ wrapCallable() 0 38 2
A hp$0 ➔ process() 0 4 1
A shiftHandler() 0 9 2
A __construct() 0 3 1
A hp$1 ➔ __construct() 0 4 1
A isCallable() 0 9 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\Dispatcher;
6
7
use InvalidArgumentException;
8
use Psr\Container\ContainerInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\MiddlewareInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use Yiisoft\Injector\Injector;
14
use Yiisoft\Router\Handler\HandlerAwareTrait;
15
16
use function array_keys;
17
use function array_shift;
18
use function get_class_methods;
19
use function in_array;
20
use function is_array;
21
use function is_callable;
22
use function is_object;
23
use function is_string;
24
25
final class DefaultDispatcher implements DispatcherInterface
26
{
27
    use HandlerAwareTrait;
28
29
    private ?ContainerInterface $container;
30
31
    public function __construct(?ContainerInterface $container)
32
    {
33
        $this->container = $container;
34
    }
35
36
    public function handle(ServerRequestInterface $request): ResponseInterface
37
    {
38
        // TODO: validate middlewares before preparing, or maybe even validate each middleware before preparing
39
        // so that if some middlewares in the stack are not called, they are note validated either.
40
        $middleware = $this->shiftHandler();
41
42
        return $middleware->process($request, $this);
43
    }
44
45
    private function shiftHandler(): MiddlewareInterface
46
    {
47
        $handlers = [...$this->getHandlers()];
48
        $handler = array_shift($handlers);
49
        if ($handler === null) {
50
            throw new \Exception('There must be at least one middleware.');
51
        }
52
53
        return $this->prepareHandler($handler);
54
    }
55
56
    private function prepareHandler($handler): MiddlewareInterface
57
    {
58
        if ($handler instanceof MiddlewareInterface) {
59
            return $handler;
60
        }
61
62
        if (is_string($handler)) {
63
            if ($this->container === null) {
64
                throw new InvalidArgumentException('Route container must not be null for lazy loaded middleware.');
65
            }
66
            return $this->container->get($handler);
67
        }
68
69
        if (is_array($handler) && !is_object($handler[0])) {
70
            if ($this->container === null) {
71
                throw new InvalidArgumentException('Route container must not be null for handler action.');
72
            }
73
            return $this->wrapCallable($handler);
74
        }
75
76
        if ($this->isCallable($handler)) {
77
            if ($this->container === null) {
78
                throw new InvalidArgumentException('Route container must not be null for callable.');
79
            }
80
            return $this->wrapCallable($handler);
81
        }
82
83
        return $handler;
84
    }
85
86
    private function isCallable($definition): bool
87
    {
88
        if (is_callable($definition)) {
89
            return true;
90
        }
91
92
        // This additional check is done for PHP 8, as callable types were changed
93
        // @see https://wiki.php.net/rfc/consistent_callables
94
        return is_array($definition) && array_keys($definition) === [0, 1] && in_array($definition[1], get_class_methods($definition[0]) ?? [], true);
95
    }
96
97
    private function wrapCallable($callback)
98
    {
99
        if (is_array($callback) && !is_object($callback[0])) {
100
            [$controller, $action] = $callback;
101
            return new class($controller, $action, $this->container) implements MiddlewareInterface {
0 ignored issues
show
Bug introduced by
It seems like $this->container can also be of type null; however, parameter $container of anonymous//src/Dispatche...er.php$0::__construct() does only seem to accept Psr\Container\ContainerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
            return new class($controller, $action, /** @scrutinizer ignore-type */ $this->container) implements MiddlewareInterface {
Loading history...
102
                private string $class;
103
                private string $method;
104
                private ContainerInterface $container;
105
106
                public function __construct(string $class, string $method, ContainerInterface $container)
107
                {
108
                    $this->class = $class;
109
                    $this->method = $method;
110
                    $this->container = $container;
111
                }
112
113
                public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
114
                {
115
                    $controller = $this->container->get($this->class);
116
                    return (new Injector($this->container))->invoke([$controller, $this->method], [$request, $handler]);
117
                }
118
            };
119
        }
120
121
        return new class($callback, $this->container) implements MiddlewareInterface {
0 ignored issues
show
Bug introduced by
It seems like $this->container can also be of type null; however, parameter $container of anonymous//src/Dispatche...er.php$1::__construct() does only seem to accept Psr\Container\ContainerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        return new class($callback, /** @scrutinizer ignore-type */ $this->container) implements MiddlewareInterface {
Loading history...
122
            private ContainerInterface $container;
123
            private $callback;
124
125
            public function __construct(callable $callback, ContainerInterface $container)
126
            {
127
                $this->callback = $callback;
128
                $this->container = $container;
129
            }
130
131
            public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
132
            {
133
                $response = (new Injector($this->container))->invoke($this->callback, [$request, $handler]);
134
                return $response instanceof MiddlewareInterface ? $response->process($request, $handler) : $response;
135
            }
136
        };
137
    }
138
}
139