Completed
Push — master ( 0a0841...454698 )
by Carlos
06:55
created

Composite   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 131
ccs 36
cts 45
cp 0.8
rs 10
c 0
b 0
f 0
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getNormalizer() 0 9 3
A parseRequest() 0 24 6
A hasNormalizer() 0 3 1
A createUrl() 0 9 2
A ensureRules() 0 4 2
A init() 0 13 4
A createNotFoundException() 0 3 1
1
<?php
2
3
namespace roaresearch\yii2\roa\urlRules;
4
5
use Yii;
6
use yii\{
7
    base\InvalidConfigException,
8
    web\NotFoundHttpException,
9
    web\UrlManager,
10
    web\UrlNormalizer
11
};
12
13
/**
14
 * Url rule that can call children rule when applicable.
15
 *
16
 * @author Angel (Faryshta) Guevara <[email protected]>
17
 */
18
abstract class Composite extends \yii\web\CompositeUrlRule
19
{
20
    /**
21
     * @var bool whether this rule must throw an `NotFoundHttpException` when
22
     * parse request fails.
23
     */
24
    public $strict = true;
25
26
    /**
27
     * @var UrlNormalizer|null
28
     */
29
    public $normalizer = null;
30
31
    /**
32
     * @var string message used to create the `NotFoundHttpException` when
33
     * `$strict` equals `true` and no children rules could parse the request.
34
     */
35
    public $notFoundMessage = 'Unknown route.';
36
37
    /**
38
     * @inheritdoc
39
     */
40 21
    public function init()
41
    {
42 21
        if (is_array($this->normalizer)) {
0 ignored issues
show
introduced by
The condition is_array($this->normalizer) is always false.
Loading history...
43
            $this->normalizer = Yii::createObject(array_merge(
44
                ['class' => UrlNormalizer::class],
45
                $this->normalizer
46
            ));
47
        }
48 21
        if (!empty($this->normalizer)
49 21
            && !$this->normalizer instanceof UrlNormalizer
0 ignored issues
show
introduced by
$this->normalizer is always a sub-type of yii\web\UrlNormalizer.
Loading history...
50
        ) {
51
            throw new InvalidConfigException(
52
                'Invalid config for `normalizer`.'
53
            );
54
        }
55 21
    }
56
57
    /**
58
     * Determines if this rule must parse the request using the children rules
59
     * or return `false` inmediately.
60
     *
61
     * @param string $route
62
     * @return bool
63
     */
64
    abstract protected function isApplicable(string $route): bool;
65
66
    /**
67
     * Ensures that `$rules` property is set
68
     */
69 21
    private function ensureRules()
70
    {
71 21
        if (empty($this->rules)) {
72 21
            $this->rules = $this->createRules();
73
        }
74 21
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 21
    public function parseRequest($manager, $request)
80
    {
81
        // only parse rules applicable rules
82 21
        if (!$this->isApplicable($request->pathInfo)) {
83 1
            return false;
84
        }
85 21
        $normalized = false;
86 21
        if ($this->hasNormalizer($manager)) {
87 21
            $request->pathInfo = $this->getNormalizer($manager)
88 21
                ->normalizePathInfo(
89 21
                    $request->pathInfo,
90 21
                    '',
91 21
                    $normalized
92
                );
93
        }
94 21
        $this->ensureRules();
95 21
        $result = parent::parseRequest($manager, $request);
96 21
        if ($result === false && $this->strict === true) {
97
            throw $this->createNotFoundException();
98
        }
99
100 21
        return $normalized
101 1
            ? $this->getNormalizer($manager)->normalizeRoute($result)
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type false; however, parameter $route of yii\web\UrlNormalizer::normalizeRoute() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

101
            ? $this->getNormalizer($manager)->normalizeRoute(/** @scrutinizer ignore-type */ $result)
Loading history...
102 21
            : $result;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 17
    public function createUrl($manager, $route, $params)
109
    {
110
        // only parse rules applicable rules
111 17
        if (!$this->isApplicable($route)) {
112 16
            return false;
113
        }
114 17
        $this->ensureRules();
115
116 17
        return parent::createUrl($manager, $route, $params);
117
    }
118
119
    /**
120
     * @param UrlManager $manager the URL manager
121
     * @return bool
122
     */
123 21
    protected function hasNormalizer($manager): bool
124
    {
125 21
        return null !== $this->getNormalizer($manager);
126
    }
127
128
    /**
129
     * @param UrlManager $manager the URL manager
130
     * @return ?UrlNormalizer
131
     */
132 21
    protected function getNormalizer(UrlManager $manager): ?UrlNormalizer
133
    {
134 21
        if ($this->normalizer === null
135 21
            && $manager->normalizer instanceof UrlNormalizer
136
        ) {
137
            return $manager->normalizer;
138
        }
139
140 21
        return $this->normalizer;
141
    }
142
143
    /**
144
     * @return NotFoundHttpException
145
     */
146
    protected function createNotFoundException(): NotFoundHttpException
147
    {
148
        return new NotFoundHttpException($this->notFoundMessage);
149
    }
150
}
151