Completed
Pull Request — master (#41)
by Vladimir
03:10
created

PageManager   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 194
Duplicated Lines 8.76 %

Coupling/Cohesion

Components 4
Dependencies 6

Test Coverage

Coverage 73.77%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 17
loc 194
rs 10
c 3
b 0
f 0
ccs 45
cts 61
cp 0.7377
wmc 24
lcom 4
cbo 6

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setCollections() 0 4 1
A getStaticPages() 0 4 1
A getJailedStaticPages() 17 17 3
B parsePageViews() 0 24 4
A updatePageView() 0 13 3
A isTracked() 0 4 2
A getPageViews() 0 4 1
B handleTrackableItem() 0 24 3
A handleTrackableDynamicPageView() 0 16 3
A handleTrackableStaticPageView() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\PageView;
14
use allejo\stakx\System\FileExplorer;
15
16
/**
17
 * This class is responsible for handling all of the PageViews within a website.
18
 *
19
 * PageManager will parse all available dynamic and static PageViews. After, dynamic PageViews will be prepared by
20
 * setting the appropriate values for each ContentItem such as permalinks. Lastly, this class will compile all of the
21
 * PageViews and write them to the target directory.
22
 *
23
 * @package allejo\stakx\Manager
24
 */
25
class PageManager extends TrackingManager
26
{
27
    /**
28
     * The relative (to the stakx project) file path to the redirect template
29
     *
30
     * @var string|bool
31
     */
32
    private $redirectTemplate;
33
34
    /**
35
     * @var PageView[]
36
     */
37
    private $twigExtendsDeps;
38
39
    /**
40
     * @var ContentItem[][]
41
     */
42
    private $collections;
43
44
    /**
45
     * @var PageView[]
46
     */
47
    private $flatPages;
48
49
    /**
50
     * PageManager constructor
51
     */
52
    public function __construct()
53
    {
54
        parent::__construct();
55
56
        $this->redirectTemplate = false;
57
        $this->twigExtendsDeps = array();
58
        $this->collections = array();
59
        $this->flatPages = array();
60
    }
61
62
    /**
63
     * Give this manager the collections we'll be using for dynamic PageViews
64
     *
65
     * @param ContentItem[][] $collections
66
     */
67
    public function setCollections (&$collections)
68
    {
69
        $this->collections = &$collections;
70
    }
71
72
    public function getStaticPages ()
73
    {
74
        return $this->flatPages;
75
    }
76
77 View Code Duplication
    public function getJailedStaticPages ()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79 3
        $jailedObjects = array();
80
81 3
        foreach ($this->flatPages as $key => $value)
82
        {
83 3
            // If it's an array, it means the parent is hidden from the site menu therefore its children should be too
84 3
            if (is_array($value))
85 3
            {
86 3
                continue;
87 3
            }
88 3
89
            $jailedObjects[$key] = $value->createJail();
90
        }
91
92
        return $jailedObjects;
93
    }
94
95 3
    /**
96
     * Go through all of the PageView directories and create a respective PageView for each and classify them as a
97 3
     * dynamic or static PageView.
98 3
     *
99
     * @param $pageViewFolders
100
     */
101
    public function parsePageViews ($pageViewFolders)
102
    {
103
        if (empty($pageViewFolders)) { return; }
104
105
        /**
106
         * The name of the folder where PageViews are located
107
         *
108
         * @var $pageViewFolder string
109
         */
110
        foreach ($pageViewFolders as $pageViewFolderName)
111
        {
112
            $pageViewFolder = $this->fs->absolutePath($pageViewFolderName);
113
114
            if (!$this->fs->exists($pageViewFolder))
115 3
            {
116
                continue;
117 3
            }
118 3
119
            $this->scanTrackableItems($pageViewFolder, array(
120 3
                'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES
121
            ), array('/.html$/', '/.twig$/'));
122 3
            $this->saveFolderDefinition($pageViewFolderName);
123 3
        }
124
    }
125 3
126 3
    /**
127
     * Add a new ContentItem to the respective parent PageView of the ContentItem
128 1
     *
129
     * @param ContentItem $contentItem
130 1
     */
131
    public function updatePageView ($contentItem)
132
    {
133
        /** @var DynamicPageView $pageView */
134
        foreach ($this->trackedItems['dynamic'] as &$pageView)
135
        {
136
            $fm = $pageView->getFrontMatter(false);
137
138 1
            if ($fm['collection'] == $contentItem->getCollection())
139
            {
140 1
                $pageView->addContentItem($contentItem);
141
            }
142 1
        }
143
    }
144
145 1
    /**
146 1
     * {@inheritdoc}
147
     */
148
    public function isTracked($filePath)
149
    {
150 1
        return (parent::isTracked($filePath) || isset($this->twigExtendsDeps[$filePath]));
151 1
    }
152
153 1
    /**
154
     * @return PageView[]
155
     */
156
    public function getPageViews ()
157
    {
158
        return $this->trackedItemsFlattened;
159
    }
160
161
    /**
162 3
     * {@inheritdoc}
163
     */
164 3
    protected function handleTrackableItem($filePath, $options = array())
165
    {
166
        $pageView  = PageView::create($filePath);
167
        $namespace = $pageView->getType();
168
169
        switch ($namespace)
170
        {
171 3
            case PageView::DYNAMIC_TYPE:
172
                $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...
173 3
                break;
174
175 3
            case PageView::STATIC_TYPE:
176 3
                $this->handleTrackableStaticPageView($pageView);
177
                break;
178
179
            default:
180 3
                break;
181
        }
182 3
183 3
        $this->addObjectToTracker($pageView, $pageView->getRelativeFilePath(), $namespace);
184 3
        $this->saveTrackerOptions($pageView->getRelativeFilePath(), array(
185 3
            'viewType' => $namespace
186
        ));
187
    }
188
189
    /**
190 3
     * @param DynamicPageView $pageView
191
     */
192 3
    private function handleTrackableDynamicPageView ($pageView)
193
    {
194 3
        $frontMatter = $pageView->getFrontMatter(false);
195 3
        $collection = $frontMatter['collection'];
196 3
197
        if (!isset($this->collections[$collection]))
198
        {
199
            throw new CollectionNotFoundException("The '$collection' collection is not defined");
200
        }
201
202
        foreach ($this->collections[$collection] as &$item)
203
        {
204
            $item->evaluateFrontMatter($frontMatter);
205
            $pageView->addContentItem($item);
206
        }
207
    }
208
209
    /**
210
     * @param PageView $pageView
211
     */
212
    private function handleTrackableStaticPageView ($pageView)
213
    {
214
        if (empty($pageView['title'])) { return; }
215
216
        $this->flatPages[$pageView['title']] = $pageView;
217
    }
218
}