Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

MenuManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 89.66%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 117
ccs 52
cts 58
cp 0.8966
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A compileManager() 0 9 2
A getSiteMenu() 0 17 3
A buildFromPageViews() 0 7 2
C addToSiteMenu() 0 53 13
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\Document\JailedDocument;
11
use allejo\stakx\Document\StaticPageView;
12
13
class MenuManager extends BaseManager
14
{
15
    /** @var StaticPageView */
16
    private $siteMenu = [];
17
    private $manager;
18
19 8
    public function __construct(PageManager $manager = null)
20
    {
21 8
        parent::__construct();
22
23 8
        $this->manager = $manager;
24 8
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function compileManager()
30
    {
31
        if ($this->manager === null)
32
        {
33
            return;
34
        }
35
36
        $this->buildFromPageViews($this->manager->getStaticPageViews());
37
    }
38
39
    /**
40
     * An array representing the website's menu structure with children and grandchildren made from static PageViews.
41
     *
42
     * @return JailedDocument[]
43
     */
44 8
    public function getSiteMenu()
45
    {
46 8
        $jailedMenu = array();
47
48 8
        foreach ($this->siteMenu as $key => $value)
49
        {
50
            // If it's an array, it means the parent is hidden from the site menu therefore its children should be too
51 7
            if (is_array($this->siteMenu[$key]))
52 7
            {
53 1
                continue;
54
            }
55
56 7
            $jailedMenu[$key] = $value->createJail();
57 8
        }
58
59 8
        return $jailedMenu;
60
    }
61
62
    /**
63
     * @param StaticPageView[] $pageViews
64
     */
65 8
    public function buildFromPageViews($pageViews)
66
    {
67 8
        foreach ($pageViews as $pageView)
68
        {
69 8
            $this->addToSiteMenu($pageView);
70 8
        }
71 8
    }
72
73
    /**
74
     * @param StaticPageView $pageView
75
     */
76 8
    public function addToSiteMenu($pageView)
77
    {
78 8
        $frontMatter = $pageView->getFrontMatter();
79
80 8
        if (isset($frontMatter['menu']) && !$frontMatter['menu'])
81 8
        {
82 1
            return;
83
        }
84
85 8
        $url = trim($pageView->getPermalink(), '/');
86
87
        // @TODO in the next breaking release, remove this check and allow the homepage to be indexed as '.'
88 8
        if (empty($url))
89 8
        {
90 1
            return;
91
        }
92
93 7
        $root = &$this->siteMenu;
94 7
        $dirs = explode('/', $url);
95
96 7
        while (count($dirs) > 0)
97
        {
98 7
            $name = array_shift($dirs);
99 7
            $name = (!empty($name)) ? $name : '.';
100
101 7
            if (!is_null($name) && count($dirs) == 0)
102 7
            {
103 7
                if (isset($root[$name]) && is_array($root[$name]))
104 7
                {
105 1
                    $children = &$pageView->getChildren();
106 1
                    $children = $root[$name]['children'];
107 1
                }
108
109 7
                $root[$name] = &$pageView;
110 7
            }
111
            else
112
            {
113 4
                if (!isset($root[$name]))
114 4
                {
115 2
                    $root[$name]['children'] = array();
116 2
                    $root = &$root[$name]['children'];
117 2
                }
118 4
                elseif (isset($root[$name]) && is_array($root[$name]))
119
                {
120 2
                    $root = &$root[$name]['children'];
121 2
                }
122
                else
123
                {
124 2
                    $root = &$root[$name]->getChildren();
125
                }
126
            }
127 7
        }
128 7
    }
129
}
130