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

PageManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Test Coverage

Coverage 89.61%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 226
ccs 69
cts 77
cp 0.8961
rs 10
c 3
b 0
f 0
wmc 21
lcom 3
cbo 7

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A setCollections() 0 4 1
A getAllPageViews() 0 4 1
A getPageViews() 0 4 1
A getPageViewsFlattened() 0 4 1
A getStaticPageViews() 0 4 1
A getJailedStaticPageViews() 0 11 2
B parsePageViews() 0 23 4
A trackNewContentItem() 0 5 1
A handleTrackableItem() 0 23 3
A handleTrackableStaticPageView() 0 9 2
A handleTrackableDynamicPageView() 0 16 3
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();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54 7
        $this->staticPages = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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
     * @todo Remove this function
73
     * @deprecated Been replaced by getPageViewsFlattened()
74
     * @since 0.1.0
75
     *
76
     * @return PageView[][]
77
     */
78
    public function getAllPageViews()
79
    {
80
        return $this->trackedItemsFlattened;
81
    }
82
83
    /**
84
     * Get all of the PageViews in an associative array with PageView types as the keys
85
     *
86
     * @since  0.1.1
87
     *
88
     * @return PageView[][]
89
     */
90
    public function &getPageViews()
91
    {
92
        return $this->trackedItems;
93
    }
94
95
    /**
96
     * Get all of the PageViews in flat array
97
     *
98
     * @since  0.1.1
99
     *
100
     * @return PageView[]
101
     */
102
    public function &getPageViewsFlattened()
103
    {
104
        return $this->trackedItemsFlattened;
105
    }
106
107
    /**
108
     * Get the static PageViews tracked by this manager indexed by their title.
109
     *
110
     * @since 0.1.0
111
     *
112
     * @return PageView[]
113
     */
114 7
    public function getStaticPageViews()
115
    {
116 1
        return $this->staticPages;
117 7
    }
118
119
    /**
120
     * Get the jailed version of the static PageViews indexed by their title.
121
     *
122
     * @since 0.1.0
123
     *
124
     * @return JailObject[]
125
     */
126 1
    public function getJailedStaticPageViews()
127
    {
128 1
        $jailedObjects = array();
129
130 1
        foreach ($this->staticPages as $key => $value)
131
        {
132 1
            $jailedObjects[$key] = $value->createJail();
133 1
        }
134
135 1
        return $jailedObjects;
136
    }
137
138
    /**
139
     * Go through all of the PageView directories and create a respective PageView for each and classify them as a
140
     * dynamic or static PageView.
141
     *
142
     * @param string[] $pageViewFolders
143
     *
144
     * @since 0.1.0
145
     */
146 7
    public function parsePageViews($pageViewFolders)
147
    {
148 7
        if (empty($pageViewFolders))
149 7
        {
150
            return;
151
        }
152
153 7
        foreach ($pageViewFolders as $pageViewFolderName)
154
        {
155
            /** @var string $pageViewFolderPath */
156 7
            $pageViewFolderPath = $this->fs->absolutePath($pageViewFolderName);
157
158 7
            if (!$this->fs->exists($pageViewFolderPath))
159 7
            {
160 1
                $this->output->warning("The '$pageViewFolderName' folder could not be found");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $pageViewFolderName instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
161 1
                continue;
162
            }
163
164 6
            $this->scanTrackableItems($pageViewFolderPath, array(
165 6
                'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES,
166 6
            ), array('/.html$/', '/.twig$/'));
167 6
        }
168 6
    }
169
170
    /**
171
     * Add a new ContentItem to the respective parent PageView of the ContentItem.
172
     *
173
     * @param ContentItem $contentItem
174
     *
175
     * @since 0.1.0
176
     */
177 1
    public function trackNewContentItem(&$contentItem)
178
    {
179 1
        $collection = $contentItem->getCollection();
180 1
        $this->trackedItems[PageView::DYNAMIC_TYPE][$collection]->addContentItem($contentItem);
181 1
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 6
    protected function handleTrackableItem($filePath, $options = array())
187
    {
188 6
        $pageView = PageView::create($filePath);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
189 6
        $namespace = $pageView->getType();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
190 6
        $storageKey = $pageView->getRelativeFilePath();
191
192
        switch ($namespace)
193
        {
194 6
            case PageView::STATIC_TYPE:
195 2
                $this->handleTrackableStaticPageView($pageView);
196 2
                break;
197
198 4
            case PageView::DYNAMIC_TYPE:
199 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...
200 3
                $storageKey = $pageView->getCollection();
201 3
                break;
202
203
            default:
204
                break;
205
        }
206
207 5
        $this->addObjectToTracker($pageView, $storageKey, $namespace);
208 5
    }
209
210
    /**
211
     * Handle special behavior and treatment for static PageViews while we're iterating through them.
212
     *
213
     * @param PageView $pageView
214
     *
215
     * @since 0.1.0
216
     */
217 2
    private function handleTrackableStaticPageView(&$pageView)
218
    {
219 2
        if (empty($pageView['title']))
220 2
        {
221 2
            return;
222
        }
223
224 2
        $this->staticPages[$pageView['title']] = &$pageView;
225 2
    }
226
227
    /**
228
     * Handle special behavior and treatment for dynamic PageViews while we're iterating through them.
229
     *
230
     * @param DynamicPageView $pageView
231
     *
232
     * @since 0.1.0
233
     */
234 4
    private function handleTrackableDynamicPageView(&$pageView)
235
    {
236 4
        $frontMatter = $pageView->getFrontMatter(false);
237 4
        $collection = $frontMatter['collection'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
238
239 4
        if (!isset($this->collections[$collection]))
240 4
        {
241 1
            throw new CollectionNotFoundException("The '$collection' collection is not defined");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $collection instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
242
        }
243
244 3
        foreach ($this->collections[$collection] as &$item)
245
        {
246 3
            $item->evaluateFrontMatter($frontMatter);
247 3
            $pageView->addContentItem($item);
248 3
        }
249 3
    }
250
}
251