Completed
Push — feature/menu-types ( 831fc6...84933c )
by Nicolas
15:41
created

MenuItemUriGenerator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 86
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C generateUri() 0 28 8
A getPageSlug() 0 11 2
A getParentUri() 0 8 2
1
<?php
2
3
namespace Modules\Menu\Services;
4
5
use Modules\Menu\Repositories\MenuItemRepository;
6
use Modules\Page\Repositories\PageRepository;
7
8
final class MenuItemUriGenerator
9
{
10
    /**
11
     * @var MenuItemRepository
12
     */
13
    private $menuItem;
14
    /**
15
     * @var PageRepository
16
     */
17
    private $page;
18
19
    public function __construct(MenuItemRepository $menuItem, PageRepository $page)
20
    {
21
        $this->menuItem = $menuItem;
22
        $this->page = $page;
23
    }
24
25
    /**
26
     * Generate a URI based of the given page and and the parent id recursively
27
     * @param string $pageId
28
     * @param string $parentId
29
     * @param string $lang
30
     * @return string
31
     */
32
    public function generateUri($pageId, $parentId, $lang)
33
    {
34
        $linkPathArray = [];
35
36
        $linkPathArray[] = $this->getPageSlug($pageId, $lang);
37
38
        if ($parentId !== '') {
39
            $hasParentItem = !(is_null($parentId)) ? true : false;
40
            while ($hasParentItem) {
41
                $parentItemId = isset($parentItem) ? $parentItem->parent_id : $parentId;
42
                $parentItem = $this->menuItem->find($parentItemId);
43
44
                if ((int) $parentItem->is_root === 0) {
45
                    if ($parentItem->page_id != '') {
46
                        $linkPathArray[] = $this->getPageSlug($parentItem->page_id, $lang);
47
                    } else {
48
                        $linkPathArray[] = $this->getParentUri($parentItem, $linkPathArray);
49
                    }
50
                    $hasParentItem = !is_null($parentItem->parent_id) ? true : false;
51
                } else {
52
                    $hasParentItem = false;
53
                }
54
            }
55
        }
56
        $parentLinkPath = implode('/', array_reverse($linkPathArray));
57
58
        return $parentLinkPath;
59
    }
60
61
    /**
62
     * Get page slug
63
     * @param $id
64
     * @param $lang
65
     * @return string
66
     */
67
    private function getPageSlug($id, $lang)
68
    {
69
        $page = $this->page->find($id);
70
        $translation = $page->translate($lang);
71
72
        if ($translation === null) {
73
            return $page->translate(config('app.fallback_locale'))->slug;
74
        }
75
76
        return $translation->slug;
77
    }
78
79
    /**
80
     * Get parent uri
81
     *
82
     * @params $pageId, $lang
83
     * @return string
84
     */
85
    private function getParentUri($item, $linkPathArray)
86
    {
87
        if ($item->uri === null) {
88
            return implode('/', $linkPathArray);
89
        }
90
91
        return $item->uri;
92
    }
93
}
94