Completed
Push — master ( 19bc37...509c61 )
by Vladimir
02:40
created

PageManager::setCollections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like array(\allejo\stakx\Docu...PEATER_TYPE => array()) of type array<string|integer,array> is incompatible with the declared type array<integer,object<all...ent\TrackableDocument>> of property $trackedItems.

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...
56
            PageView::STATIC_TYPE   => array(),
57
            PageView::DYNAMIC_TYPE  => array(),
58
            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[][]
0 ignored issues
show
Documentation introduced by
Should the return type not be \allejo\stakx\Document\TrackableDocument[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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[][]
0 ignored issues
show
Documentation introduced by
Should the return type not be \allejo\stakx\Document\TrackableDocument[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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[]
0 ignored issues
show
Documentation introduced by
Should the return type not be \allejo\stakx\Document\TrackableDocument[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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, array $options = array())
200
    {
201 6
        $pageView = PageView::create($filePath);
202 6
        $namespace = $pageView->getType();
203
204
        switch ($namespace)
205
        {
206 6
            case PageView::STATIC_TYPE:
207 2
                $this->handleTrackableStaticPageView($pageView);
208 2
                break;
209
210 4
            case PageView::DYNAMIC_TYPE:
211 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...
212 3
                break;
213
214
            default:
215
                break;
216
        }
217
218 5
        $this->addObjectToTracker($pageView, $namespace);
219
220 5
        return $pageView;
221
    }
222
223
    /**
224
     * Handle special behavior and treatment for static PageViews while we're iterating through them.
225
     *
226
     * @param PageView $pageView
227
     *
228
     * @since 0.1.0
229
     */
230 2
    private function handleTrackableStaticPageView(&$pageView)
231
    {
232 2
        if (empty($pageView['title']))
233
        {
234 2
            return;
235
        }
236
237 2
        $this->staticPages[$pageView['title']] = &$pageView;
238 2
    }
239
240
    /**
241
     * Handle special behavior and treatment for dynamic PageViews while we're iterating through them.
242
     *
243
     * @param DynamicPageView $pageView
244
     *
245
     * @since 0.1.0
246
     */
247 4
    private function handleTrackableDynamicPageView(&$pageView)
248
    {
249 4
        $frontMatter = $pageView->getFrontMatter(false);
250 4
        $namespace = (isset($frontMatter['collection'])) ? 'collection' : 'dataset';
251
252 4
        $collection = $frontMatter[$namespace];
253 4
        $array = $namespace . 's';
254
255 4
        if (!isset($this->{$array}[$collection]))
256
        {
257 1
            throw new CollectionNotFoundException("The '$collection' $namespace is not defined");
258
        }
259
260 3
        foreach ($this->{$array}[$collection] as &$item)
261
        {
262 3
            $item->evaluateFrontMatter($frontMatter);
263 3
            $item->setParentPageView($pageView);
264 3
            $item->buildPermalink(true);
265 3
            $pageView->addRepeatableItem($item);
266
        }
267 3
    }
268
}
269