Passed
Pull Request — master (#14)
by Sergei
06:49
created

MiddlewareFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 41
c 1
b 0
f 0
dl 0
loc 114
ccs 43
cts 43
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 3 1
A hp$1 ➔ __construct() 0 4 1
A hp$1 ➔ wrapCallable() 0 38 2
A createMiddleware() 0 9 2
A hp$1 ➔ process() 0 4 2
wrapCallable() 0 38 ?
A hp$0 ➔ process() 0 4 1
A hp$0 ➔ __construct() 0 5 1
isCallable() 0 14 ?
A hp$1 ➔ isCallable() 0 14 6
validateMiddleware() 0 11 ?
A hp$1 ➔ validateMiddleware() 0 11 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Middleware\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
15
final class MiddlewareFactory implements MiddlewareFactoryInterface
16
{
17
    private ContainerInterface $container;
18
19 15
    public function __construct(ContainerInterface $container)
20
    {
21 15
        $this->container = $container;
22 15
    }
23
24 15
    public function create($middlewareDefinition): MiddlewareInterface
25
    {
26 15
        return $this->createMiddleware($middlewareDefinition);
27
    }
28
29
    /**
30
     * @param array|callable|string $middlewareDefinition A name of PSR-15 middleware, a callable with
31
     * `function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface` signature or
32
     * a handler action (an array of [handlerClass, handlerMethod]). For handler action and callable typed parameters
33
     * are automatically injected using dependency injection container passed to the route.
34
     * Current request and handler could be obtained by type-hinting for {@see ServerRequestInterface}
35
     * and {@see RequestHandlerInterface}.
36
     *
37
     * @return MiddlewareInterface
38
     */
39 15
    private function createMiddleware($middlewareDefinition): MiddlewareInterface
40
    {
41 15
        $this->validateMiddleware($middlewareDefinition);
42
43 9
        if (is_string($middlewareDefinition)) {
44 1
            return $this->container->get($middlewareDefinition);
45
        }
46
47 8
        return $this->wrapCallable($middlewareDefinition);
48
    }
49
50 8
    private function wrapCallable($callback): MiddlewareInterface
51
    {
52 8
        if (is_array($callback) && !is_object($callback[0])) {
53 3
            [$controller, $action] = $callback;
54 3
            return new class($controller, $action, $this->container) implements MiddlewareInterface {
55
                private string $class;
56
                private string $method;
57
                private ContainerInterface $container;
58
59
                public function __construct(string $class, string $method, ContainerInterface $container)
60
                {
61 3
                    $this->class = $class;
62 3
                    $this->method = $method;
63 3
                    $this->container = $container;
64 3
                }
65
66
                public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
67
                {
68 2
                    $controller = $this->container->get($this->class);
69 2
                    return (new Injector($this->container))->invoke([$controller, $this->method], [$request, $handler]);
70
                }
71
            };
72
        }
73
74 5
        return new class($callback, $this->container) implements MiddlewareInterface {
75
            private ContainerInterface $container;
76
            private $callback;
77
78
            public function __construct(callable $callback, ContainerInterface $container)
79
            {
80 5
                $this->callback = $callback;
81 5
                $this->container = $container;
82 5
            }
83
84
            public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
85
            {
86 5
                $response = (new Injector($this->container))->invoke($this->callback, [$request, $handler]);
87 5
                return $response instanceof MiddlewareInterface ? $response->process($request, $handler) : $response;
88
            }
89
        };
90
    }
91
92
    /**
93
     * @param array|callable|string $middlewareDefinition A name of PSR-15 middleware, a callable with
94
     * `function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface` signature or
95
     * a handler action (an array of [handlerClass, handlerMethod]). For handler action and callable typed parameters
96
     * are automatically injected using dependency injection container passed to the route.
97
     * Current request and handler could be obtained by type-hinting for {@see ServerRequestInterface}
98
     * and {@see RequestHandlerInterface}.
99
     *
100
     * @throws InvalidMiddlewareDefinitionException
101
     */
102 15
    private function validateMiddleware($middlewareDefinition): void
103
    {
104 15
        if (is_string($middlewareDefinition) && is_subclass_of($middlewareDefinition, MiddlewareInterface::class)) {
105 1
            return;
106
        }
107
108 14
        if ($this->isCallable($middlewareDefinition) && (!is_array($middlewareDefinition) || !is_object($middlewareDefinition[0]))) {
109 8
            return;
110
        }
111
112 6
        throw new InvalidMiddlewareDefinitionException($middlewareDefinition);
113
    }
114
115 14
    private function isCallable($definition): bool
116
    {
117 14
        if (is_callable($definition)) {
118 8
            return is_object($definition)
119 5
                ? !$definition instanceof MiddlewareInterface
120 8
                : true;
121
        }
122
123 6
        return is_array($definition)
124 6
            && array_keys($definition) === [0, 1]
125 1
            && in_array(
126 1
                $definition[1],
127 1
                class_exists($definition[0]) ? get_class_methods($definition[0]) : [],
128 6
                true
129
            );
130
    }
131
}
132