MiddlewareResolver::resolve()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 16
loc 16
ccs 8
cts 8
cp 1
crap 4
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace N1215\Jugoya\Resolver;
4
5
use Psr\Http\Server\MiddlewareInterface;
6
use N1215\Jugoya\Wrapper\CallableMiddleware;
7
use Psr\Container\ContainerInterface;
8
9 View Code Duplication
final class MiddlewareResolver implements MiddlewareResolverInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    use ResolverTrait;
12
13
    /**
14
     * @var ContainerInterface
15
     */
16
    protected $container;
17
18
    /**
19
     * @param ContainerInterface $container
20
     */
21 15
    public function __construct(ContainerInterface $container)
22
    {
23 15
        $this->container = $container;
24 15
    }
25
26
    /**
27
     * @param string|callable|MiddlewareInterface $ref
28
     * @return MiddlewareInterface
29
     * @throws UnresolvedException
30
     */
31 13
    public function resolve($ref): MiddlewareInterface
32
    {
33 13
        if ($ref instanceof MiddlewareInterface) {
34 7
            return $ref;
35
        }
36
37 12
        if (is_callable($ref)) {
38 7
            return new CallableMiddleware($ref);
39
        }
40
41 11
        if (!is_string($ref)) {
42 1
            throw new \InvalidArgumentException('Argument 1 $ref must be one of an instance of MiddlewareInterface, a callable or string.');
43
        }
44
45 10
        return $this->resolveByContainer($ref, $this->container, MiddlewareInterface::class);
46
    }
47
}
48