Test Failed
Push — master ( ebe86f...4efdc4 )
by mcfog
02:46
created

BoltStubResolver::resolve()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 14.2702

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 10
nop 1
dl 0
loc 31
ccs 8
cts 17
cp 0.4706
crap 14.2702
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Bolt\Router;
6
7
use Lit\Air\ContainerStub;
0 ignored issues
show
Bug introduced by
The type Lit\Air\ContainerStub 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...
8
use Lit\Air\Factory;
9
use Lit\Nimo\Handlers\CallableHandler;
10
use Lit\Nimo\Handlers\FixedResponseHandler;
11
use Lit\Voltage\Interfaces\RouterStubResolverInterface;
12
use Psr\Container\ContainerInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
16
/**
17
 * Standard stub resolver using DI container to resolve stubs
18
 */
19
class BoltStubResolver implements RouterStubResolverInterface
20
{
21
    /**
22
     * @var ContainerInterface
23
     */
24
    protected $container;
25
    /**
26
     * @var mixed
27
     */
28
    protected $notFound;
29
30 1
    public function __construct(ContainerInterface $container, $notFound = null)
31
    {
32 1
        $this->container = $container;
33 1
        $this->notFound = $notFound;
34 1
    }
35
36
    /**
37
     * Resolve the stub and return a RequestHandler
38
     *
39
     * @param mixed $stub The stub value to be resolved.
40
     * @return RequestHandlerInterface
41
     */
42 1
    public function resolve($stub): RequestHandlerInterface
43
    {
44 1
        if ($stub === null && $this->notFound) {
45
            $stub = $this->notFound;
46
        }
47
48 1
        if (is_callable($stub)) {
49
            return new CallableHandler($stub);
50
        }
51
52 1
        if ($stub instanceof RequestHandlerInterface) {
53 1
            return $stub;
54
        }
55
56 1
        if ($stub instanceof ResponseInterface) {
57 1
            return FixedResponseHandler::wrap($stub);
58
        }
59
60 1
        $containerStub = ContainerStub::tryParse($stub);
61
        if (!$containerStub) {
62
            /** @var StubResolveException $exception */
63
            $exception = Factory::of($this->container)->instantiate(StubResolveException::class, [
64
                'stub' => $stub,
65
            ]);
66
67
            throw $exception;
68
        }
69
70
        $handler = $containerStub->instantiateFrom($this->container);
71
        assert($handler instanceof RequestHandlerInterface);
72
        return $handler;
73
    }
74
}
75