Passed
Push — master ( e7d330...e6cdc9 )
by Aleksei
11:08 queued 15s
created

AbstractTarget   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 28
c 0
b 0
f 0
dl 0
loc 87
ccs 29
cts 29
cp 1
rs 10

6 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 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\Router\CoreHandler;
14
use Spiral\Router\Exception\TargetException;
15
use Spiral\Router\TargetInterface;
16
use Spiral\Router\UriHandler;
17
use Spiral\Telemetry\TracerInterface;
18
19
/**
20
 * @psalm-import-type Matches from UriHandler
21
 */
22
abstract class AbstractTarget implements TargetInterface
23
{
24
    // Automatically prepend HTTP verb to all action names.
25
    public const RESTFUL = 1;
26
27
    private ?CoreInterface $core = null;
28
    private ?CoreHandler $handler = null;
29
    private bool $verbActions;
30
31 444
    public function __construct(
32
        private array $defaults,
33
        private array $constrains,
34
        int $options = 0,
35
        private string $defaultAction = 'index'
36
    ) {
37 444
        $this->verbActions = ($options & self::RESTFUL) === self::RESTFUL;
38
    }
39
40 421
    public function getDefaults(): array
41
    {
42 421
        return $this->defaults;
43
    }
44
45 416
    public function getConstrains(): array
46
    {
47 416
        return $this->constrains;
48
    }
49
50
    /**
51
     * @mutation-free
52
     */
53 327
    public function withCore(CoreInterface $core): TargetInterface
54
    {
55 327
        $target = clone $this;
56 327
        $target->core = $core;
57 327
        $target->handler = null;
58
59 327
        return $target;
60
    }
61
62 56
    public function getHandler(ContainerInterface $container, array $matches): Handler
63
    {
64 56
        return $this->coreHandler($container)->withContext(
65 56
            $this->resolveController($matches),
66 56
            $this->resolveAction($matches) ?? $this->defaultAction,
67 56
            $matches
68 56
        )->withVerbActions($this->verbActions);
69
    }
70
71 56
    protected function coreHandler(ContainerInterface $container): CoreHandler
72
    {
73 56
        if ($this->handler !== null) {
74 16
            return $this->handler;
75
        }
76
77
        try {
78
            // construct on demand
79 56
            $this->handler = new CoreHandler(
80 56
                $this->core ?? $container->get(CoreInterface::class),
81 56
                $container->get(ScopeInterface::class),
82 56
                $container->get(ResponseFactoryInterface::class),
83 56
                $container->get(TracerInterface::class)
84 56
            );
85
86 55
            return $this->handler;
87 1
        } catch (ContainerExceptionInterface $e) {
88 1
            throw new TargetException($e->getMessage(), $e->getCode(), $e);
89
        }
90
    }
91
92
    /**
93
     * Return controller class name.
94
     *
95
     * @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...
96
     *
97
     * @throws TargetException
98
     */
99
    abstract protected function resolveController(array $matches): string;
100
101
    /**
102
     * Return target controller action.
103
     *
104
     * @param Matches $matches
105
     *
106
     * @throws TargetException
107
     */
108
    abstract protected function resolveAction(array $matches): ?string;
109
}
110