Completed
Pull Request — master (#41)
by Vladimir
04:25
created

PageManager::addToSiteMenu()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 11.0359

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 21
nc 11
nop 1
dl 0
loc 45
ccs 28
cts 30
cp 0.9333
crap 11.0359
rs 5.2653
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Exception\CollectionNotFoundException;
11
use allejo\stakx\Object\ContentItem;
12
use allejo\stakx\Object\DynamicPageView;
13
use allejo\stakx\Object\JailObject;
14
use allejo\stakx\Object\PageView;
15
use allejo\stakx\System\FileExplorer;
16
17
/**
18
 * This class is responsible for handling all of the PageViews within a website.
19
 *
20
 * PageManager will parse all available dynamic and static PageViews. After, dynamic PageViews will be prepared by
21
 * setting the appropriate values for each ContentItem such as permalinks.
22
 *
23
 * @internal
24
 */
25
class PageManager extends TrackingManager
26
{
27
    /**
28
     * A reference to the collections available to this website.
29
     *
30
     * @var ContentItem[][]
31
     */
32
    private $collections;
33
34
    /**
35
     * A place to store a reference to static PageViews with titles.
36
     *
37
     * @var PageView[]
38
     */
39
    private $staticPages;
40
41
    /**
42
     * PageManager constructor.
43
     */
44 7
    public function __construct()
45
    {
46 7
        parent::__construct();
47
48 7
        $this->collections = array();
49 7
        $this->staticPages = array();
50 7
    }
51
52
    /**
53
     * Give this manager the collections we'll be using for dynamic PageViews.
54
     *
55
     * @param ContentItem[][] $collections
56
     *
57
     * @since 0.1.0
58
     */
59 4
    public function setCollections(&$collections)
60
    {
61 4
        $this->collections = &$collections;
62 4
    }
63
64
    /**
65
     * Get all of the PageViews tracked by this manager.
66
     *
67
     * @since 0.1.0
68
     *
69
     * @return PageView[][]
70
     */
71 5
    public function getAllPageViews()
72
    {
73 5
        return $this->trackedItemsFlattened;
74
    }
75
76
    /**
77
     * Get the static PageViews tracked by this manager indexed by their title.
78
     *
79
     * @since 0.1.0
80
     *
81
     * @return PageView[]
82
     */
83 1
    public function getStaticPageViews()
84
    {
85 1
        return $this->staticPages;
86
    }
87
88
    /**
89
     * Get the jailed version of the static PageViews indexed by their title.
90
     *
91
     * @since 0.1.0
92
     *
93
     * @return JailObject[]
94
     */
95 1
    public function getJailedStaticPageViews()
96
    {
97 1
        $jailedObjects = array();
98
99 1
        foreach ($this->staticPages as $key => $value)
100
        {
101 1
            $jailedObjects[$key] = $value->createJail();
102 1
        }
103
104 1
        return $jailedObjects;
105
    }
106
107
    /**
108
     * Go through all of the PageView directories and create a respective PageView for each and classify them as a
109
     * dynamic or static PageView.
110
     *
111
     * @param string[] $pageViewFolders
112
     *
113
     * @since 0.1.0
114
     */
115 7
    public function parsePageViews($pageViewFolders)
116
    {
117 7
        if (empty($pageViewFolders))
118 7
        {
119
            return;
120
        }
121
122 7
        foreach ($pageViewFolders as $pageViewFolderName)
123
        {
124
            /** @var string $pageViewFolderPath */
125 7
            $pageViewFolderPath = $this->fs->absolutePath($pageViewFolderName);
126
127 7
            if (!$this->fs->exists($pageViewFolderPath))
128 7
            {
129 1
                $this->output->warning("The '$pageViewFolderName' folder could not be found");
130 1
                continue;
131
            }
132
133 6
            $this->scanTrackableItems($pageViewFolderPath, array(
134 6
                'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES,
135 6
            ), array('/.html$/', '/.twig$/'));
136 6
        }
137 6
    }
138
139
    /**
140
     * Add a new ContentItem to the respective parent PageView of the ContentItem.
141
     *
142
     * @param ContentItem $contentItem
143
     *
144
     * @since 0.1.0
145
     */
146 1
    public function trackNewContentItem(&$contentItem)
147
    {
148 1
        $collection = $contentItem->getCollection();
149 1
        $this->trackedItems[PageView::DYNAMIC_TYPE][$collection]->addContentItem($contentItem);
150 1
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 6
    protected function handleTrackableItem($filePath, $options = array())
156
    {
157 6
        $pageView = PageView::create($filePath);
158 6
        $namespace = $pageView->getType();
159 6
        $storageKey = $pageView->getRelativeFilePath();
160
161
        switch ($namespace)
162
        {
163 6
            case PageView::STATIC_TYPE:
164 2
                $this->handleTrackableStaticPageView($pageView);
165 2
                break;
166
167 4
            case PageView::DYNAMIC_TYPE:
168 4
                $this->handleTrackableDynamicPageView($pageView);
0 ignored issues
show
Compatibility introduced by
$pageView of type object<allejo\stakx\Object\PageView> is not a sub-type of object<allejo\stakx\Object\DynamicPageView>. It seems like you assume a child class of the class allejo\stakx\Object\PageView to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
169 3
                $storageKey = $pageView->getCollection();
170 3
                break;
171
172
            default:
173
                break;
174
        }
175
176 5
        $this->addObjectToTracker($pageView, $storageKey, $namespace);
177 5
    }
178
179
    /**
180
     * Handle special behavior and treatment for static PageViews while we're iterating through them.
181
     *
182
     * @param PageView $pageView
183
     *
184
     * @since 0.1.0
185
     */
186 2
    private function handleTrackableStaticPageView(&$pageView)
187
    {
188 2
        if (empty($pageView['title']))
189 2
        {
190 2
            return;
191
        }
192
193 2
        $this->staticPages[$pageView['title']] = &$pageView;
194 2
    }
195
196
    /**
197
     * Handle special behavior and treatment for dynamic PageViews while we're iterating through them.
198
     *
199
     * @param DynamicPageView $pageView
200
     *
201
     * @since 0.1.0
202
     */
203 4
    private function handleTrackableDynamicPageView(&$pageView)
204
    {
205 4
        $frontMatter = $pageView->getFrontMatter(false);
206 4
        $collection = $frontMatter['collection'];
207
208 4
        if (!isset($this->collections[$collection]))
209 4
        {
210 1
            throw new CollectionNotFoundException("The '$collection' collection is not defined");
211
        }
212
213 3
        foreach ($this->collections[$collection] as &$item)
214
        {
215 3
            $item->evaluateFrontMatter($frontMatter);
216 3
            $pageView->addContentItem($item);
217 3
        }
218 3
    }
219
}
220