Passed
Push — master ( 035a69...ef3bdc )
by Kirill
03:22
created

AbstractTarget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Router\Target;
13
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\ContainerInterface;
16
use Psr\Http\Message\ResponseFactoryInterface;
17
use Psr\Http\Server\RequestHandlerInterface as Handler;
18
use Spiral\Core\CoreInterface;
19
use Spiral\Core\ScopeInterface;
20
use Spiral\Router\CoreHandler;
21
use Spiral\Router\Exception\TargetException;
22
use Spiral\Router\TargetInterface;
23
24
abstract class AbstractTarget implements TargetInterface
25
{
26
    // Automatically prepend HTTP verb to all action names.
27
    public const RESTFUL = 1;
28
29
    /** @var array */
30
    private $defaults = [];
31
32
    /** @var array */
33
    private $constrains = [];
34
35
    /** @var CoreInterface */
36
    private $core;
37
38
    /** @var CoreHandler */
39
    private $handler;
40
41
    /** @var bool */
42
    private $verbActions;
43
44
    /** @var string */
45
    private $defaultAction;
46
47
    /**
48
     * @param array  $defaults
49
     * @param array  $constrains
50
     * @param int    $options
51
     * @param string $defaultAction
52
     */
53
    public function __construct(array $defaults, array $constrains, int $options = 0, string $defaultAction = 'index')
54
    {
55
        $this->defaults = $defaults;
56
        $this->constrains = $constrains;
57
        $this->verbActions = ($options & self::RESTFUL) === self::RESTFUL;
58
        $this->defaultAction = $defaultAction;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function getDefaults(): array
65
    {
66
        return $this->defaults;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getConstrains(): array
73
    {
74
        return $this->constrains;
75
    }
76
77
    /**
78
     * @param CoreInterface $core
79
     * @return TargetInterface|$this
80
     */
81
    public function withCore(CoreInterface $core): TargetInterface
82
    {
83
        $target = clone $this;
84
        $target->core = $core;
85
        $target->handler = null;
86
87
        return $target;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function getHandler(ContainerInterface $container, array $matches): Handler
94
    {
95
        return $this->coreHandler($container)->withContext(
96
            $this->resolveController($matches),
97
            $this->resolveAction($matches) ?? $this->defaultAction,
98
            $matches
99
        )->withVerbActions($this->verbActions);
100
    }
101
102
    /**
103
     * @param ContainerInterface $container
104
     * @return CoreHandler
105
     */
106
    protected function coreHandler(ContainerInterface $container): CoreHandler
107
    {
108
        if ($this->handler !== null) {
109
            return $this->handler;
110
        }
111
112
        try {
113
            // construct on demand
114
            $this->handler = new CoreHandler(
115
                $this->core ?? $container->get(CoreInterface::class),
116
                $container->get(ScopeInterface::class),
117
                $container->get(ResponseFactoryInterface::class)
118
            );
119
120
            return $this->handler;
121
        } catch (ContainerExceptionInterface $e) {
122
            throw new TargetException($e->getMessage(), $e->getCode(), $e);
0 ignored issues
show
Bug introduced by
The method getCode() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
            throw new TargetException($e->getMessage(), $e->/** @scrutinizer ignore-call */ getCode(), $e);
Loading history...
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
            throw new TargetException($e->/** @scrutinizer ignore-call */ getMessage(), $e->getCode(), $e);
Loading history...
123
        }
124
    }
125
126
    /**
127
     * Return controller class name.
128
     *
129
     * @param array $matches
130
     * @return string
131
     *
132
     * @throws TargetException
133
     */
134
    abstract protected function resolveController(array $matches): string;
135
136
    /**
137
     * Return target controller action.
138
     *
139
     * @param array $matches
140
     * @return string|null
141
     *
142
     * @throws TargetException
143
     */
144
    abstract protected function resolveAction(array $matches): ?string;
145
}
146