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

PageManager::handleTrackableItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0593

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 23
ccs 13
cts 16
cp 0.8125
crap 3.0593
rs 9.0856
c 2
b 0
f 0
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->trackedItems = array(
49 7
            PageView::STATIC_TYPE => array(),
50 7
            PageView::DYNAMIC_TYPE => array(),
51 7
            PageView::REPEATER_TYPE => array(),
52
        );
53 7
        $this->collections = array();
54 7
        $this->staticPages = array();
55 7
    }
56
57
    /**
58
     * Give this manager the collections we'll be using for dynamic PageViews.
59
     *
60
     * @param ContentItem[][] $collections
61
     *
62
     * @since 0.1.0
63
     */
64 4
    public function setCollections(&$collections)
65
    {
66 4
        $this->collections = &$collections;
67 4
    }
68
69
    /**
70
     * Get all of the PageViews tracked by this manager.
71
     *
72
     * @since 0.1.0
73
     *
74
     * @return PageView[][]
75
     */
76 5
    public function getAllPageViews()
77
    {
78 5
        return $this->trackedItemsFlattened;
79
    }
80
81
    /**
82
     * Get all of the PageViews in an associative array with PageView types as the keys
83
     *
84
     * @since  0.1.1
85
     *
86
     * @return PageView[][]
87
     */
88
    public function &getPageViews()
89
    {
90
        return $this->trackedItems;
91
    }
92
93
    /**
94
     * Get the static PageViews tracked by this manager indexed by their title.
95
     *
96
     * @since 0.1.0
97
     *
98
     * @return PageView[]
99
     */
100 1
    public function getStaticPageViews()
101
    {
102 1
        return $this->staticPages;
103
    }
104
105
    /**
106
     * Get the jailed version of the static PageViews indexed by their title.
107
     *
108
     * @since 0.1.0
109
     *
110
     * @return JailObject[]
111
     */
112 7
    public function getJailedStaticPageViews()
113
    {
114 1
        $jailedObjects = array();
115
116 1
        foreach ($this->staticPages as $key => $value)
117 7
        {
118 1
            $jailedObjects[$key] = $value->createJail();
119 1
        }
120
121 1
        return $jailedObjects;
122
    }
123
124
    /**
125
     * Go through all of the PageView directories and create a respective PageView for each and classify them as a
126
     * dynamic or static PageView.
127
     *
128
     * @param string[] $pageViewFolders
129
     *
130
     * @since 0.1.0
131
     */
132 7
    public function parsePageViews($pageViewFolders)
133
    {
134 7
        if (empty($pageViewFolders))
135 7
        {
136
            return;
137
        }
138
139 7
        foreach ($pageViewFolders as $pageViewFolderName)
140
        {
141
            /** @var string $pageViewFolderPath */
142 7
            $pageViewFolderPath = $this->fs->absolutePath($pageViewFolderName);
143
144 7
            if (!$this->fs->exists($pageViewFolderPath))
145 7
            {
146 1
                $this->output->warning("The '$pageViewFolderName' folder could not be found");
147 1
                continue;
148
            }
149
150 6
            $this->scanTrackableItems($pageViewFolderPath, array(
151 6
                'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES,
152 6
            ), array('/.html$/', '/.twig$/'));
153 6
        }
154 6
    }
155
156
    /**
157
     * Add a new ContentItem to the respective parent PageView of the ContentItem.
158
     *
159
     * @param ContentItem $contentItem
160
     *
161
     * @since 0.1.0
162
     */
163 1
    public function trackNewContentItem(&$contentItem)
164
    {
165 1
        $collection = $contentItem->getCollection();
166 1
        $this->trackedItems[PageView::DYNAMIC_TYPE][$collection]->addContentItem($contentItem);
167 1
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 6
    protected function handleTrackableItem($filePath, $options = array())
173
    {
174 6
        $pageView = PageView::create($filePath);
175 6
        $namespace = $pageView->getType();
176 6
        $storageKey = $pageView->getRelativeFilePath();
177
178
        switch ($namespace)
179
        {
180 6
            case PageView::STATIC_TYPE:
181 2
                $this->handleTrackableStaticPageView($pageView);
182 2
                break;
183
184 4
            case PageView::DYNAMIC_TYPE:
185 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...
186 3
                $storageKey = $pageView->getCollection();
187 3
                break;
188
189
            default:
190
                break;
191
        }
192
193 5
        $this->addObjectToTracker($pageView, $storageKey, $namespace);
194 5
    }
195
196
    /**
197
     * Handle special behavior and treatment for static PageViews while we're iterating through them.
198
     *
199
     * @param PageView $pageView
200
     *
201
     * @since 0.1.0
202
     */
203 2
    private function handleTrackableStaticPageView(&$pageView)
204
    {
205 2
        if (empty($pageView['title']))
206 2
        {
207 2
            return;
208
        }
209
210 2
        $this->staticPages[$pageView['title']] = &$pageView;
211 2
    }
212
213
    /**
214
     * Handle special behavior and treatment for dynamic PageViews while we're iterating through them.
215
     *
216
     * @param DynamicPageView $pageView
217
     *
218
     * @since 0.1.0
219
     */
220 4
    private function handleTrackableDynamicPageView(&$pageView)
221
    {
222 4
        $frontMatter = $pageView->getFrontMatter(false);
223 4
        $collection = $frontMatter['collection'];
224
225 4
        if (!isset($this->collections[$collection]))
226 4
        {
227 1
            throw new CollectionNotFoundException("The '$collection' collection is not defined");
228
        }
229
230 3
        foreach ($this->collections[$collection] as &$item)
231
        {
232 3
            $item->evaluateFrontMatter($frontMatter);
233 3
            $pageView->addContentItem($item);
234 3
        }
235 3
    }
236
}
237