Passed
Pull Request — master (#2)
by Dmitriy
10:29
created

MiddlewareFactory.php$0 ➔ __construct()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
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;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Http\Server\MiddlewareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\MiddlewareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\RequestHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Injector\Injector;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Injector\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
final class MiddlewareFactory implements MiddlewareFactoryInterface
16
{
17
    private ContainerInterface $container;
18
19
    public function __construct(ContainerInterface $container)
20
    {
21
        $this->container = $container;
22
    }
23
24
    public function create($middlewareDefinition): MiddlewareInterface
25
    {
26
        return $this->createMiddleware($middlewareDefinition);
27
    }
28
29
    /**
30
     * @param callable|string|array $middlewareDefinition
31
     * @return MiddlewareInterface
32
     */
33
    private function createMiddleware($middlewareDefinition): MiddlewareInterface
34
    {
35
        $this->validateMiddleware($middlewareDefinition);
36
37
        if (is_string($middlewareDefinition)) {
38
            return $this->container->get($middlewareDefinition);
39
        }
40
41
        return $this->wrapCallable($middlewareDefinition);
42
    }
43
44
    private function wrapCallable($callback): MiddlewareInterface
45
    {
46
        if (is_array($callback) && !is_object($callback[0])) {
47
            [$controller, $action] = $callback;
48
            return new class($controller, $action, $this->container) implements MiddlewareInterface {
49
                private string $class;
50
                private string $method;
51
                private ContainerInterface $container;
52
53
                public function __construct(string $class, string $method, ContainerInterface $container)
54
                {
55
                    $this->class = $class;
56
                    $this->method = $method;
57
                    $this->container = $container;
58
                }
59
60
                public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
61
                {
62
                    $controller = $this->container->get($this->class);
63
                    return (new Injector($this->container))->invoke([$controller, $this->method], [$request, $handler]);
64
                }
65
            };
66
        }
67
68
        return new class($callback, $this->container) implements MiddlewareInterface {
69
            private ContainerInterface $container;
70
            private $callback;
71
72
            public function __construct(callable $callback, ContainerInterface $container)
73
            {
74
                $this->callback = $callback;
75
                $this->container = $container;
76
            }
77
78
            public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
79
            {
80
                $response = (new Injector($this->container))->invoke($this->callback, [$request, $handler]);
81
                return $response instanceof MiddlewareInterface ? $response->process($request, $handler) : $response;
82
            }
83
        };
84
    }
85
86
    /**
87
     * @param callable|string|array $middlewareDefinition
88
     */
89
    private function validateMiddleware($middlewareDefinition): void
90
    {
91
        if (is_string($middlewareDefinition) && is_subclass_of($middlewareDefinition, MiddlewareInterface::class)) {
92
            return;
93
        }
94
95
        if ($this->isCallable($middlewareDefinition) && (!is_array($middlewareDefinition) || !is_object($middlewareDefinition[0]))) {
96
            return;
97
        }
98
99
        throw new InvalidArgumentException('Parameter should be either PSR middleware class name or a callable.');
100
    }
101
102
    private function isCallable($definition): bool
103
    {
104
        if (is_callable($definition)) {
105
            return is_object($definition)
106
                ? !$definition instanceof MiddlewareInterface
107
                : true;
108
        }
109
110
        return is_array($definition)
111
            && array_keys($definition) === [0, 1]
112
            && in_array(
113
                $definition[1],
114
                class_exists($definition[0]) ? get_class_methods($definition[0]) : [],
115
                true
116
            );
117
    }
118
}
119