RewriteurlRule::isMatching()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
cc 7
nc 7
nop 2
1
<?php
2
3
namespace RewriteUrl\Model;
4
5
use RewriteUrl\Model\Base\RewriteurlRule as BaseRewriteurlRule;
6
use Thelia\Model\Tools\PositionManagementTrait;
7
8
class RewriteurlRule extends BaseRewriteurlRule
9
{
10
    use PositionManagementTrait;
11
12
13
    /** @var string  */
14
    const TYPE_REGEX = "regex";
15
16
    /** @var string  */
17
    const TYPE_GET_PARAMS = "params";
18
19
20
    protected $rewriteUrlParamCollection = null;
21
22
23
    public function getRewriteUrlParamCollection()
24
    {
25
        if ($this->rewriteUrlParamCollection == null) {
26
            $this->rewriteUrlParamCollection = RewriteurlRuleParamQuery::create()->filterByIdRule($this->getId())->find();
27
        }
28
        return $this->rewriteUrlParamCollection;
29
    }
30
31
    public function isMatching($url, $getParamArray)
32
    {
33
        if ($this->getRuleType() == self::TYPE_REGEX) {
34
            if (!empty($this->getValue())) {
35
                return preg_match("/" . $this->getValue() . "/", $url) === 1;
36
            }
37
        } elseif ($this->getRuleType() == self::TYPE_GET_PARAMS) {
38
            if ($this->getRewriteUrlParamCollection()->count() > 0) {
39
                foreach ($this->getRewriteUrlParamCollection() as $rewriteUrlParam) {
40
                    if (!$rewriteUrlParam->isMatching($getParamArray)) {
41
                        return false;
42
                    }
43
                }
44
                return true;
45
            }
46
        }
47
        return false;
48
    }
49
50
    public function deleteAllRelatedParam()
51
    {
52
        RewriteurlRuleParamQuery::create()->filterByIdRule($this->getId())->find()->delete();
53
    }
54
}
55