Test Failed
Push — master ( 7da8a2...7ea984 )
by n
02:25
created

MiddlewareResolver::resolve()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 29
Code Lines 17

Duplication

Lines 29
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 8

Importance

Changes 0
Metric Value
dl 29
loc 29
ccs 1
cts 1
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 8
nop 1
crap 8
1
<?php
2
3
namespace N1215\Jugoya\Resolver;
4
5
use Interop\Http\ServerMiddleware\MiddlewareInterface;
6
use N1215\Jugoya\Wrapper\CallableMiddleware;
7
use Psr\Container\NotFoundExceptionInterface;
8
use Psr\Container\ContainerExceptionInterface;
9
10 View Code Duplication
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...
11
{
12
13
    /**
14
     * @param string|callable|MiddlewareInterface $ref
15
     * @return MiddlewareInterface
16 10
     * @throws UnresolvedException
17
     */
18 10
    public function resolve($ref)
19
    {
20
        if ($ref instanceof MiddlewareInterface) {
21
            return $ref;
22
        }
23
24
        if (is_callable($ref)) {
25
            return new CallableMiddleware($ref);
26
        }
27
28
        if (!is_string($ref)) {
29
            throw new \InvalidArgumentException("Argument 1 \$ref must be one of an instance of MiddlewareInterface, a callable or string.");
30
        }
31
32
        try {
33
            $entry = $this->container->get($ref);
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
        } catch (NotFoundExceptionInterface $e) {
35
            throw new UnresolvedException('Could not found an entry from the container.', 0, $e);
36
        } catch (ContainerExceptionInterface $e) {
37
            throw new UnresolvedException('Something wrong with the container.', 0, $e);
38
        }
39
40
        if (!$entry instanceof MiddlewareInterface) {
41
            $type = is_object($entry) ? get_class($entry) : gettype($entry);
42
            throw new UnresolvedException("Expected container returns an instance of MiddlewareInterface, {$type} given.");
43
        }
44
45
        return $entry;
46
    }
47
}
48