Completed
Push — master ( 5d51b4...87fdd9 )
by Paul
10s
created

UrlBuilder::buildUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Helper;
4
5
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
6
7
/**
8
 * ref: victoire_core.url_builder.
9
 */
10
class UrlBuilder
11
{
12
    /**
13
     * Builds the page's url by get all page parents slugs and implode them with "/".
14
     *
15
     * @param WebViewInterface $view
16
     *
17
     * @return string $url
18
     */
19
    public function buildUrl(WebViewInterface $view)
20
    {
21
        $slug = [];
22
        // build url binded with parents url
23
        if (!(method_exists($view, 'isHomepage') && $view->isHomepage())) {
24
            $slug = [$view->getSlug()];
25
        }
26
27
        //get the slug of the parents
28
        $url = $this->getParentSlugs($view, $slug);
29
30
        //reorder the list of slugs
31
        $url = array_reverse($url);
32
        //build an url based on the slugs
33
        $url = implode('/', $url);
34
35
        return $url;
36
    }
37
38
    /**
39
     * Get the array of slugs of the parents.
40
     *
41
     * @param WebViewInterface $view
42
     * @param string[]         $slugs
43
     *
44
     * @return string[]
45
     */
46
    protected function getParentSlugs(WebViewInterface $view, array $slugs)
47
    {
48
        $parent = $view->getParent();
49
50
        if ($parent !== null) {
51
            if (!(method_exists($parent, 'isHomepage') && $parent->isHomepage())) {
52
                array_push($slugs, $parent->getSlug());
53
            }
54
55
            if ($parent->getParent() !== null) {
56
                $slugs = $this->getParentSlugs($parent, $slugs);
57
            }
58
        }
59
60
        return array_unique($slugs);
61
    }
62
}
63