Completed
Push — development ( 67765c...7029e6 )
by Andrij
18:12
created

Route::getRouteUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace core\models;
4
5
use core\models\Base\Route as BaseRoute;
6
use core\src\CoreConfiguration;
7
use core\src\CoreFactory;
8
use Propel\Runtime\ActiveQuery\Criteria;
9
use Propel\Runtime\Collection\ObjectCollection;
10
11
/**
12
 * Skeleton subclass for representing a row from the 'route' table.
13
 *
14
 *
15
 *
16
 * You should add additional methods to this class to meet the
0 ignored issues
show
introduced by
There must be exactly one blank line between descriptions in a doc comment
Loading history...
17
 * application requirements.  This class will only be generated as
18
 * long as it does not already exist in the output directory.
19
 *
20
 */
0 ignored issues
show
introduced by
Additional blank lines found at end of doc comment
Loading history...
21
class Route extends BaseRoute
22
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
23
    const TYPE_SHOP_CATEGORY = 'shop_category';
24
    const TYPE_PRODUCT = 'product';
25
    const TYPE_CATEGORY = 'category';
26
    const TYPE_PAGE = 'page';
27
    const TYPE_MAIN = 'main';
28
    const TYPE_MODULE = 'module';
29
30
    /**
31
     * @param string $newParentUrl
32
     * @param string $newUrl
33
     */
34
    public function updateUrlRecursive($newParentUrl, $newUrl) {
35
36
        $oldUrl = $this->getFullUrl();
37
38
        if ($oldUrl !== $newParentUrl . '/' . $newUrl) {
39
            $this->setParentUrl($newParentUrl);
40
            $this->setUrl($newUrl);
41
            $this->save();
42
            RouteQuery::create()->updateParentUrl($oldUrl, $this->getFullUrl());
43
        }
44
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getRouteUrl() {
51
        return self::createRouteUrl($this->getUrl(), $this->getParentUrl(), $this->getType());
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getFullUrl() {
58
        $url = $this->getParentUrl() ? $this->getParentUrl() . '/' . $this->getUrl() : $this->getUrl();
59
        return $url;
60
    }
61
62
    /**
63
     * @return Route[]|ObjectCollection
64
     */
65
    public function getPathRoutes() {
66
        $parent = $this->getParentUrl();
67
68
        $collection = new ObjectCollection();
69
        $collection->setModel(get_class($this));
70
71
        if ($parent) {
72
            $segments = explode('/', $parent);
73
            $collection = RouteQuery::create()->orderByParentUrl(Criteria::ASC)->filterByUrl($segments, Criteria::IN)->find();
0 ignored issues
show
Documentation introduced by
$segments is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
        }
75
76
        $collection->append($this);
77
78
        return $collection;
79
    }
80
81
    /**
82
     * @param string $url
83
     * @param string $parentUrl
84
     * @param int $type
85
     * @return string
86
     */
87
    public static function createRouteUrl($url, $parentUrl, $type) {
88
        $urlConfiguration = CoreFactory::getConfiguration()->getUrlRules();
89
90
        if (array_key_exists($type, $urlConfiguration)) {
91
            $rules = $urlConfiguration[$type];
92
        } else {
93
            $rules = [
94
                      'prefix' => '',
95
                      'parent' => true,
96
                     ];
97
        }
98
99
        if ($rules['parent'] && $parentUrl) {
100
            $url = $parentUrl . '/' . $url;
101
        }
102
103
        if ($rules['prefix']) {
104
            $url = $rules['prefix'] . '/' . $url;
105
        }
106
107
        return $url;
108
    }
109
110
}