Passed
Pull Request — master (#1104)
by Aleksei
26:20
created

AbstractTarget::coreHandler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Router\Target;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Psr\Http\Message\ResponseFactoryInterface;
10
use Psr\Http\Server\RequestHandlerInterface as Handler;
11
use Spiral\Core\CoreInterface;
12
use Spiral\Core\ScopeInterface;
13
use Spiral\Interceptors\HandlerInterface;
14
use Spiral\Router\CoreHandler;
15
use Spiral\Router\Exception\TargetException;
16
use Spiral\Router\TargetInterface;
17
use Spiral\Router\UriHandler;
18
use Spiral\Telemetry\TracerInterface;
19
20
/**
21
 * @psalm-import-type Matches from UriHandler
22
 */
23
abstract class AbstractTarget implements TargetInterface
24
{
25
    // Automatically prepend HTTP verb to all action names.
26
    public const RESTFUL = 1;
27
28
    private HandlerInterface|CoreInterface|null $pipeline = null;
29
    private ?CoreHandler $handler = null;
30
    private bool $verbActions;
31
32 478
    public function __construct(
33
        private array $defaults,
34
        private array $constrains,
35
        int $options = 0,
36
        private string $defaultAction = 'index'
37
    ) {
38 478
        $this->verbActions = ($options & self::RESTFUL) === self::RESTFUL;
39
    }
40
41 455
    public function getDefaults(): array
42
    {
43 455
        return $this->defaults;
44
    }
45
46 450
    public function getConstrains(): array
47
    {
48 450
        return $this->constrains;
49
    }
50
51
    /**
52
     * @mutation-free
53
     * @deprecated Use {@see withHandler()} instead.
54
     */
55 361
    public function withCore(HandlerInterface|CoreInterface $core): TargetInterface
56
    {
57 361
        $target = clone $this;
58 361
        $target->pipeline = $core;
59 361
        $target->handler = null;
60
61 361
        return $target;
62
    }
63
64
    /**
65
     * @mutation-free
66
     */
67
    public function withHandler(HandlerInterface $handler): TargetInterface
68
    {
69
        $target = clone $this;
70
        $target->pipeline = $handler;
71
        $target->handler = null;
72
73
        return $target;
74
    }
75
76 57
    public function getHandler(ContainerInterface $container, array $matches): Handler
77
    {
78 57
        return $this->coreHandler($container)->withContext(
79 57
            $this->resolveController($matches),
80 57
            $this->resolveAction($matches) ?? $this->defaultAction,
81 57
            $matches
82 57
        )->withVerbActions($this->verbActions);
83
    }
84
85 57
    protected function coreHandler(ContainerInterface $container): CoreHandler
86
    {
87 57
        if ($this->handler !== null) {
88 17
            return $this->handler;
89
        }
90
91
        try {
92
            // construct on demand
93 57
            $this->handler = new CoreHandler(
94 57
                $this->pipeline ?? $container->get(HandlerInterface::class),
95 57
                $container->get(ScopeInterface::class),
96 57
                $container->get(ResponseFactoryInterface::class),
97 57
                $container->get(TracerInterface::class)
98 57
            );
99
100 56
            return $this->handler;
101 1
        } catch (ContainerExceptionInterface $e) {
102 1
            throw new TargetException($e->getMessage(), $e->getCode(), $e);
103
        }
104
    }
105
106
    /**
107
     * Return controller class name.
108
     *
109
     * @param Matches $matches
0 ignored issues
show
Bug introduced by
The type Spiral\Router\Target\Matches 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...
110
     *
111
     * @throws TargetException
112
     */
113
    abstract protected function resolveController(array $matches): string;
114
115
    /**
116
     * Return target controller action.
117
     *
118
     * @param Matches $matches
119
     *
120
     * @throws TargetException
121
     */
122
    abstract protected function resolveAction(array $matches): ?string;
123
}
124