MiddlewareFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 15 4
1
<?php
2
3
/*
4
 * This file is part of the jade/jade package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jade\Middleware;
13
14
use Psr\Http\Server\MiddlewareInterface;
15
use Zend\Stratigility\Middleware\CallableMiddlewareDecorator;
16
17
final class MiddlewareFactory
18
{
19
    /**
20
     * 创建middleware
21
     *
22
     * @param MiddlewareInterface|string|callable $middleware
23
     * @return MiddlewareInterface
24
     */
25
    public function create($middleware)
26
    {
27
        // middleware 实例直接返回
28
        if ($middleware instanceof MiddlewareInterface) {
29
            return $middleware;
30
        }
31
        // middleware 类
32
        if (is_subclass_of($middleware, MiddlewareInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Psr\Http\Server\MiddlewareInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
33
            return new $middleware;
34
        }
35
        if (is_callable($middleware)) {
36
            return new CallableMiddlewareDecorator($middleware);
37
        }
38
        throw new \InvalidArgumentException('Invalid middleware format');
39
    }
40
}