Completed
Push — master ( ccd9cb...ab24cf )
by
unknown
16s
created

CompositeUrlRule::iterateRules()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 14
nc 4
nop 4
crap 6.0208
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
 * @author Qiang Xue <[email protected]>
17
 * @since 2.0
18
 */
19
abstract class CompositeUrlRule extends Object implements UrlRuleInterface
20
{
21
    /**
22
     * @var UrlRuleInterface[] the URL rules contained in this composite rule.
23
     * This property is set in [[init()]] by the return value of [[createRules()]].
24
     */
25
    protected $rules = [];
26
    /**
27
     * @var int|null status of the URL creation after the last [[createUrl()]] call.
28
     * @since 2.0.12
29
     */
30
    protected $createStatus;
31
32
33
    /**
34
     * Creates the URL rules that should be contained within this composite rule.
35
     * @return UrlRuleInterface[] the URL rules
36
     */
37
    abstract protected function createRules();
38
39
    /**
40
     * @inheritdoc
41
     */
42 13
    public function init()
43
    {
44 13
        parent::init();
45 13
        $this->rules = $this->createRules();
46 13
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 1
    public function parseRequest($manager, $request)
52
    {
53 1
        foreach ($this->rules as $rule) {
54
            /* @var $rule UrlRule */
55 1
            $result = $rule->parseRequest($manager, $request);
56 1
            if (YII_DEBUG) {
57 1
                Yii::trace([
58 1
                    'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
59
                    'match' => $result !== false,
60 1
                    'parent' => self::className()
61 1
                ], __METHOD__);
62
            }
63 1
            if ($result !== false) {
64 1
                return $result;
65
            }
66
        }
67
68 1
        return false;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 1
    public function createUrl($manager, $route, $params)
75
    {
76 1
        $this->createStatus = UrlRule::CREATE_STATUS_SUCCESS;
77 1
        $url = $this->iterateRules($this->rules, $manager, $route, $params);
78 1
        if ($url !== false) {
79 1
            return $url;
80
        }
81
82 1
        if ($this->createStatus === UrlRule::CREATE_STATUS_SUCCESS) {
83
            // create status was not changed - there is no rules configured
84
            $this->createStatus = UrlRule::CREATE_STATUS_PARSING_ONLY;
85
        }
86 1
        return false;
87
    }
88
89
    /**
90
     * Iterates through specified rules and calls [[createUrl()]] for each of them.
91
     *
92
     * @param UrlRuleInterface[] $rules rules to iterate.
93
     * @param UrlManager $manager the URL manager
94
     * @param string $route the route. It should not have slashes at the beginning or the end.
95
     * @param array $params the parameters
96
     * @return bool|string the created URL, or `false` if none of specified rules cannot be used for creating this URL.
97
     * @see createUrl()
98
     * @since 2.0.12
99
     */
100 10
    protected function iterateRules($rules, $manager, $route, $params)
101
    {
102
        /* @var $rule UrlRule */
103 10
        foreach ($rules as $rule) {
104 10
            $url = $rule->createUrl($manager, $route, $params);
105 10
            if ($url !== false) {
106 10
                $this->createStatus = UrlRule::CREATE_STATUS_SUCCESS;
107 10
                return $url;
108
            }
109
            if (
110 10
                $this->createStatus === null
111 10
                || !method_exists($rule, 'getCreateUrlStatus')
112 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...
113
            ) {
114
                $this->createStatus = null;
115
            } else {
116 10
                $this->createStatus |= $rule->getCreateUrlStatus();
117
            }
118
        }
119
120 10
        return false;
121
    }
122
123
    /**
124
     * Returns status of the URL creation after the last [[createUrl()]] call.
125
     *
126
     * For multiple rules statuses will be combined by bitwise `or` operator
127
     * (e.g. `UrlRule::CREATE_STATUS_PARSING_ONLY | UrlRule::CREATE_STATUS_PARAMS_MISMATCH`).
128
     *
129
     * @return null|int Status of the URL creation after the last [[createUrl()]] call. `null` if rule does not provide
130
     * info about create status.
131
     * @see $createStatus
132
     * @see http://php.net/manual/en/language.operators.bitwise.php
133
     * @since 2.0.12
134
     */
135 3
    public function getCreateUrlStatus()
136
    {
137 3
        return $this->createStatus;
138
    }
139
}
140