Completed
Push — 2.1 ( 7c8525...0afc41 )
by Alexander
21:05 queued 16:02
created

CompositeUrlRule   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 121
ccs 34
cts 36
cp 0.9444
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
createRules() 0 1 ?
A init() 0 5 1
A createUrl() 0 14 3
B iterateRules() 0 22 6
A getCreateUrlStatus() 0 4 1
B parseRequest() 0 19 5
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\Object;
12
13
/**
14
 * CompositeUrlRule is the base class for URL rule classes that consist of multiple simpler rules.
15
 *
16
 * @property null|int $createUrlStatus Status of the URL creation after the last [[createUrl()]] call. `null`
17
 * if rule does not provide info about create status. This property is read-only.
18
 *
19
 * @author Qiang Xue <[email protected]>
20
 * @since 2.0
21
 */
22
abstract class CompositeUrlRule extends Object implements UrlRuleInterface
23
{
24
    /**
25
     * @var UrlRuleInterface[] the URL rules contained in this composite rule.
26
     * This property is set in [[init()]] by the return value of [[createRules()]].
27
     */
28
    protected $rules = [];
29
    /**
30
     * @var int|null status of the URL creation after the last [[createUrl()]] call.
31
     * @since 2.0.12
32
     */
33
    protected $createStatus;
34
35
36
    /**
37
     * Creates the URL rules that should be contained within this composite rule.
38
     * @return UrlRuleInterface[] the URL rules
39
     */
40
    abstract protected function createRules();
41
42
    /**
43
     * @inheritdoc
44
     */
45 13
    public function init()
46
    {
47 13
        parent::init();
48 13
        $this->rules = $this->createRules();
49 13
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 1
    public function parseRequest($manager, $request)
55
    {
56 1
        foreach ($this->rules as $rule) {
57
            /* @var $rule UrlRule */
58 1
            $result = $rule->parseRequest($manager, $request);
59 1
            if (YII_DEBUG) {
60 1
                Yii::trace([
61 1
                    'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
62
                    'match' => $result !== false,
63
                    'parent' => self::class
64 1
                ], __METHOD__);
65
            }
66 1
            if ($result !== false) {
67 1
                return $result;
68
            }
69
        }
70
71 1
        return false;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 1
    public function createUrl($manager, $route, $params)
78
    {
79 1
        $this->createStatus = UrlRule::CREATE_STATUS_SUCCESS;
80 1
        $url = $this->iterateRules($this->rules, $manager, $route, $params);
81 1
        if ($url !== false) {
82 1
            return $url;
83
        }
84
85 1
        if ($this->createStatus === UrlRule::CREATE_STATUS_SUCCESS) {
86
            // create status was not changed - there is no rules configured
87
            $this->createStatus = UrlRule::CREATE_STATUS_PARSING_ONLY;
88
        }
89 1
        return false;
90
    }
91
92
    /**
93
     * Iterates through specified rules and calls [[createUrl()]] for each of them.
94
     *
95
     * @param UrlRuleInterface[] $rules rules to iterate.
96
     * @param UrlManager $manager the URL manager
97
     * @param string $route the route. It should not have slashes at the beginning or the end.
98
     * @param array $params the parameters
99
     * @return bool|string the created URL, or `false` if none of specified rules cannot be used for creating this URL.
100
     * @see createUrl()
101
     * @since 2.0.12
102
     */
103 10
    protected function iterateRules($rules, $manager, $route, $params)
104
    {
105
        /* @var $rule UrlRule */
106 10
        foreach ($rules as $rule) {
107 10
            $url = $rule->createUrl($manager, $route, $params);
108 10
            if ($url !== false) {
109 10
                $this->createStatus = UrlRule::CREATE_STATUS_SUCCESS;
110 10
                return $url;
111
            }
112
            if (
113 10
                $this->createStatus === null
114 10
                || !method_exists($rule, 'getCreateUrlStatus')
115 10
                || $rule->getCreateUrlStatus() === null
0 ignored issues
show
Bug introduced by
The method getCreateUrlStatus() does not exist on yii\web\UrlRuleInterface. Did you maybe mean createUrl()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
116
            ) {
117
                $this->createStatus = null;
118
            } else {
119 10
                $this->createStatus |= $rule->getCreateUrlStatus();
120
            }
121
        }
122
123 10
        return false;
124
    }
125
126
    /**
127
     * Returns status of the URL creation after the last [[createUrl()]] call.
128
     *
129
     * For multiple rules statuses will be combined by bitwise `or` operator
130
     * (e.g. `UrlRule::CREATE_STATUS_PARSING_ONLY | UrlRule::CREATE_STATUS_PARAMS_MISMATCH`).
131
     *
132
     * @return null|int Status of the URL creation after the last [[createUrl()]] call. `null` if rule does not provide
133
     * info about create status.
134
     * @see $createStatus
135
     * @see http://php.net/manual/en/language.operators.bitwise.php
136
     * @since 2.0.12
137
     */
138 3
    public function getCreateUrlStatus()
139
    {
140 3
        return $this->createStatus;
141
    }
142
}
143