Completed
Push — master ( c2b5cc...89bfcf )
by Vladimir
03:33
created

PageManager::getPageViewsFlattened()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
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\Document\ContentItem;
11
use allejo\stakx\Document\DataItem;
12
use allejo\stakx\Document\DynamicPageView;
13
use allejo\stakx\Document\JailedDocument;
14
use allejo\stakx\Document\PageView;
15
use allejo\stakx\Exception\CollectionNotFoundException;
16
use allejo\stakx\Exception\DataSetNotFoundException;
17
use allejo\stakx\System\FileExplorer;
18
19
/**
20
 * This class is responsible for handling all of the PageViews within a website.
21
 *
22
 * PageManager will parse all available dynamic and static PageViews. After, dynamic PageViews will be prepared by
23
 * setting the appropriate values for each ContentItem such as permalinks.
24
 *
25
 * @internal
26
 */
27
class PageManager extends TrackingManager
28
{
29
    /**
30
     * A reference to the collections available to this website.
31
     *
32
     * @var ContentItem[][]
33
     */
34
    private $collections;
35
36
    /**
37
     * A place to store a reference to static PageViews with titles.
38
     *
39
     * @var PageView[]
40
     */
41
    private $staticPages;
42
43
    /**
44
     * @var DataItem[]|array
45
     */
46
    private $datasets;
47
48
    /**
49
     * PageManager constructor.
50
     */
51 7
    public function __construct()
52
    {
53 7
        parent::__construct();
54
55 7
        $this->trackedItems = array(
56 7
            PageView::STATIC_TYPE   => array(),
57 7
            PageView::DYNAMIC_TYPE  => array(),
58 7
            PageView::REPEATER_TYPE => array(),
59
        );
60 7
        $this->collections = array();
61 7
        $this->staticPages = array();
62 7
    }
63
64
    /**
65
     * Give this manager the collections we'll be using for dynamic PageViews.
66
     *
67
     * @param ContentItem[][] $collections
68
     *
69
     * @since 0.1.0
70
     */
71 4
    public function setCollections(&$collections)
72
    {
73 4
        $this->collections = &$collections;
74 4
    }
75
76
    public function setDatasets($datasets)
77
    {
78
        $this->datasets = $datasets;
79
    }
80
81
    /**
82
     * Get all of the PageViews tracked by this manager.
83
     *
84
     * @todo       Remove this function
85
     *
86
     * @deprecated Been replaced by getPageViewsFlattened()
87
     * @since      0.1.0
88
     *
89
     * @return PageView[][]
90
     */
91
    public function getAllPageViews()
92
    {
93
        return $this->trackedItemsFlattened;
94
    }
95
96
    /**
97
     * Get all of the PageViews in an associative array with PageView types as the keys.
98
     *
99
     * @since  0.1.1
100
     *
101
     * @return PageView[][]
102
     */
103
    public function &getPageViews()
104
    {
105
        return $this->trackedItems;
106
    }
107
108
    /**
109
     * Get all of the PageViews in flat array.
110
     *
111
     * @since  0.1.1
112
     *
113
     * @return PageView[]
114
     */
115
    public function &getPageViewsFlattened()
116
    {
117
        return $this->trackedItemsFlattened;
118
    }
119
120
    /**
121
     * Get the static PageViews tracked by this manager indexed by their title.
122
     *
123
     * @since 0.1.0
124
     *
125
     * @return PageView[]
126
     */
127 1
    public function getStaticPageViews()
128
    {
129 1
        return $this->staticPages;
130
    }
131
132
    /**
133
     * Get the jailed version of the static PageViews indexed by their title.
134
     *
135
     * @since 0.1.0
136
     *
137
     * @return JailedDocument[]
138
     */
139 1
    public function getJailedStaticPageViews()
140
    {
141 1
        $jailedObjects = array();
142
143 1
        foreach ($this->staticPages as $key => $value)
144
        {
145 1
            $jailedObjects[$key] = $value->createJail();
146
        }
147
148 1
        return $jailedObjects;
149
    }
150
151
    /**
152
     * Go through all of the PageView directories and create a respective PageView for each and classify them as a
153
     * dynamic or static PageView.
154
     *
155
     * @param string[] $pageViewFolders
156
     *
157
     * @since 0.1.0
158
     */
159 7
    public function parsePageViews($pageViewFolders)
160
    {
161 7
        if (empty($pageViewFolders))
162
        {
163
            return;
164
        }
165
166 7
        foreach ($pageViewFolders as $pageViewFolderName)
167
        {
168
            /** @var string $pageViewFolderPath */
169 7
            $pageViewFolderPath = $this->fs->absolutePath($pageViewFolderName);
170
171 7
            if (!$this->fs->exists($pageViewFolderPath))
172
            {
173 1
                $this->output->warning("The '$pageViewFolderName' folder could not be found");
174 1
                continue;
175
            }
176
177 6
            $this->scanTrackableItems($pageViewFolderPath, array(
178 6
                'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES,
179 6
            ), array('/.html$/', '/.twig$/'));
180
        }
181 6
    }
182
183
    /**
184
     * Add a new ContentItem to the respective parent PageView of the ContentItem.
185
     *
186
     * @param ContentItem $contentItem
187
     *
188
     * @since 0.1.0
189
     */
190 1
    public function trackNewContentItem(&$contentItem)
191
    {
192 1
        $collection = $contentItem->getNamespace();
193 1
        $this->trackedItems[PageView::DYNAMIC_TYPE][$collection]->addRepeatableItem($contentItem);
194 1
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 6
    protected function &handleTrackableItem($filePath, $options = array())
200
    {
201 6
        $pageView = PageView::create($filePath);
202 6
        $namespace = $pageView->getType();
203 6
        $storageKey = $pageView->getRelativeFilePath();
204
205
        switch ($namespace)
206
        {
207 6
            case PageView::STATIC_TYPE:
208 2
                $this->handleTrackableStaticPageView($pageView);
209 2
                break;
210
211 4
            case PageView::DYNAMIC_TYPE:
212 4
                $this->handleTrackableDynamicPageView($pageView);
0 ignored issues
show
Compatibility introduced by
$pageView of type object<allejo\stakx\Document\PageView> is not a sub-type of object<allejo\stakx\Document\DynamicPageView>. It seems like you assume a child class of the class allejo\stakx\Document\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...
213 3
                $storageKey = $pageView->getRepeatableName();
214 3
                break;
215
216
            default:
217
                break;
218
        }
219
220 5
        $this->addObjectToTracker($pageView, $storageKey, $namespace);
0 ignored issues
show
Documentation introduced by
$pageView is of type object<allejo\stakx\Document\PageView>, but the function expects a object<allejo\stakx\Docu...\TwigDocumentInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
221
222 5
        return $pageView;
223
    }
224
225
    /**
226
     * Handle special behavior and treatment for static PageViews while we're iterating through them.
227
     *
228
     * @param PageView $pageView
229
     *
230
     * @since 0.1.0
231
     */
232 2
    private function handleTrackableStaticPageView(&$pageView)
233
    {
234 2
        if (empty($pageView['title']))
235
        {
236 2
            return;
237
        }
238
239 2
        $this->staticPages[$pageView['title']] = &$pageView;
240 2
    }
241
242
    /**
243
     * Handle special behavior and treatment for dynamic PageViews while we're iterating through them.
244
     *
245
     * @param DynamicPageView $pageView
246
     *
247
     * @since 0.1.0
248
     */
249 4
    private function handleTrackableDynamicPageView(&$pageView)
250
    {
251 4
        $frontMatter = $pageView->getFrontMatter(false);
252 4
        $namespace = (isset($frontMatter['collection'])) ? 'collection' : 'dataset';
253
254 4
        $collection = $frontMatter[$namespace];
255 4
        $array = $namespace . 's';
256
257 4
        if (!isset($this->{$array}[$collection]))
258
        {
259 1
            throw new CollectionNotFoundException("The '$collection' $namespace is not defined");
260
        }
261
262 3
        foreach ($this->{$array}[$collection] as &$item)
263
        {
264 3
            $item->evaluateFrontMatter($frontMatter);
265 3
            $pageView->addRepeatableItem($item);
266
        }
267 3
    }
268
}
269