Passed
Pull Request — master (#1095)
by Aleksei
11:10
created

AbstractTarget   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 85.29%

Importance

Changes 0
Metric Value
wmc 9
eloc 32
dl 0
loc 100
ccs 29
cts 34
cp 0.8529
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaults() 0 3 1
A getConstrains() 0 3 1
A __construct() 0 7 1
A withCore() 0 7 1
A withHandler() 0 7 1
A getHandler() 0 7 1
A coreHandler() 0 18 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 442
    public function __construct(
33
        private array $defaults,
34
        private array $constrains,
35
        int $options = 0,
36
        private string $defaultAction = 'index'
37
    ) {
38 442
        $this->verbActions = ($options & self::RESTFUL) === self::RESTFUL;
39
    }
40
41 419
    public function getDefaults(): array
42
    {
43 419
        return $this->defaults;
44
    }
45
46 414
    public function getConstrains(): array
47
    {
48 414
        return $this->constrains;
49
    }
50
51
    /**
52
     * @mutation-free
53
     * @deprecated Use {@see withHandler()} instead.
54
     */
55 325
    public function withCore(HandlerInterface|CoreInterface $core): TargetInterface
56
    {
57 325
        $target = clone $this;
58 325
        $target->pipeline = $core;
59 325
        $target->handler = null;
60
61 325
        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 56
    public function getHandler(ContainerInterface $container, array $matches): Handler
77
    {
78 56
        return $this->coreHandler($container)->withContext(
79 56
            $this->resolveController($matches),
80 56
            $this->resolveAction($matches) ?? $this->defaultAction,
81 56
            $matches
82 56
        )->withVerbActions($this->verbActions);
83
    }
84
85 56
    protected function coreHandler(ContainerInterface $container): CoreHandler
86
    {
87 56
        if ($this->handler !== null) {
88 16
            return $this->handler;
89
        }
90
91
        try {
92
            // construct on demand
93 56
            $this->handler = new CoreHandler(
94 56
                $this->pipeline ?? $container->get(HandlerInterface::class),
95 56
                $container->get(ScopeInterface::class),
96 56
                $container->get(ResponseFactoryInterface::class),
97 56
                $container->get(TracerInterface::class)
98 56
            );
99
100 55
            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