Passed
Pull Request — master (#1149)
by Aleksei
23:32 queued 12:57
created

AbstractTarget::getConstrains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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