Passed
Pull Request — master (#1132)
by Aleksei
24:22 queued 12:06
created

AbstractTarget::coreHandler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

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