Completed
Pull Request — master (#41)
by Vladimir
02:31
created

MenuManager::getSiteMenu()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Manager;
9
10
use allejo\stakx\Object\JailObject;
11
use allejo\stakx\Object\PageView;
12
13
class MenuManager extends BaseManager
14
{
15
    /** @var PageView */
16
    private $siteMenu;
17
18 8
    public function __construct()
19
    {
20 8
        parent::__construct();
21
22 8
        $this->siteMenu = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<allejo\stakx\Object\PageView> of property $siteMenu.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23 8
    }
24
25
    /**
26
     * An array representing the website's menu structure with children and grandchildren made from static PageViews.
27
     *
28
     * @return JailObject[]
29
     */
30 8
    public function getSiteMenu()
31
    {
32 8
        $jailedMenu = array();
33
34 8
        foreach ($this->siteMenu as $key => $value)
0 ignored issues
show
Bug introduced by
The expression $this->siteMenu of type object<allejo\stakx\Object\PageView> is not traversable.
Loading history...
35
        {
36
            // If it's an array, it means the parent is hidden from the site menu therefore its children should be too
37 7
            if (is_array($this->siteMenu[$key]))
38 7
            {
39 1
                continue;
40
            }
41
42 7
            $jailedMenu[$key] = $value->createJail();
43 8
        }
44
45 8
        return $jailedMenu;
46
    }
47
48
    /**
49
     * @param PageView[] $pageViews
50
     */
51 8
    public function buildFromPageViews($pageViews)
52
    {
53 8
        foreach ($pageViews as &$pageView)
54
        {
55 8
            $this->addToSiteMenu($pageView);
56 8
        }
57 8
    }
58
59
    /**
60
     * @param PageView $pageView
61
     */
62 8
    public function addToSiteMenu(&$pageView)
63
    {
64 8
        $frontMatter = $pageView->getFrontMatter();
65
66 8
        if (isset($frontMatter['menu']) && !$frontMatter['menu'])
67 8
        {
68 1
            return;
69
        }
70
71 8
        $url = trim($pageView->getPermalink(), '/');
72
73
        // @TODO in the next breaking release, remove this check and allow the homepage to be indexed as '.'
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
74 8
        if (empty($url))
75 8
        {
76 1
            return;
77
        }
78
79 7
        $root = &$this->siteMenu;
80 7
        $dirs = explode('/', $url);
81
82 7
        while (count($dirs) > 0)
83
        {
84 7
            $name = array_shift($dirs);
85 7
            $name = (!empty($name)) ? $name : '.';
86
87 7
            if (!is_null($name) && count($dirs) == 0)
88 7
            {
89 7
                if (isset($root[$name]) && is_array($root[$name]))
90 7
                {
91 1
                    $children = &$pageView->getChildren();
92 1
                    $children = $root[$name]['children'];
93 1
                }
94
95 7
                $root[$name] = &$pageView;
96 7
            }
97
            else
98
            {
99 4
                if (!isset($root[$name]))
100 4
                {
101 2
                    $root[$name]['children'] = array();
102 2
                    $root = &$root[$name]['children'];
103 2
                }
104 4
                elseif (isset($root[$name]) && is_array($root[$name]))
105
                {
106 2
                    $root = &$root[$name]['children'];
107 2
                }
108
                else
109
                {
110 2
                    $root = &$root[$name]->getChildren();
111
                }
112
            }
113 7
        }
114 7
    }
115
}
116