Completed
Push — master ( 8df106...cec1bd )
by Vladimir
03:13
created

CollectionManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 116
rs 10
wmc 13
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getContentItem() 0 11 2
A getCollections() 0 4 1
A getFlatCollections() 0 6 1
A isContentItem() 0 8 1
B parseCollections() 0 50 5
A flattenCollections() 0 7 2
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use allejo\stakx\Object\ContentItem;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
class CollectionManager extends ItemManager
9
{
10
    /**
11
     * @var ContentItem[]
12
     */
13
    private $collectionsFlat;
14
15
    /**
16
     * @var ContentItem[][]
17
     */
18
    private $collections;
19
20
    public function __construct ()
21
    {
22
        parent::__construct();
23
24
        $this->collections = array();
25
    }
26
27
    /**
28
     * @param $filePath
29
     *
30
     * @return ContentItem|null
31
     */
32
    public function &getContentItem ($filePath)
33
    {
34
        if ($this->isContentItem($filePath))
35
        {
36
            $contentItemId = $this->fs->getBaseName($filePath);
37
38
            return $this->collectionsFlat[$contentItemId];
39
        }
40
41
        return null;
42
    }
43
44
    public function getCollections ()
45
    {
46
        return $this->collections;
47
    }
48
49
    public function getFlatCollections ()
50
    {
51
        $this->flattenCollections();
52
53
        return $this->collectionsFlat;
54
    }
55
56
    public function isContentItem ($filePath)
57
    {
58
        $this->flattenCollections();
59
60
        $contentItemId = $this->fs->getBaseName($filePath);
61
62
        return (array_key_exists($contentItemId, $this->collectionsFlat));
63
    }
64
65
    public function parseCollections ($folders)
66
    {
67
        if ($folders === null)
68
        {
69
            $this->output->debug("No collections found, nothing to parse.");
70
            return;
71
        }
72
73
        /**
74
         * The information which each collection has taken from the configuration file
75
         *
76
         * $collection['name']      string The name of the collection
77
         *            ['folder']    string The folder where this collection has its ContentItems
78
         *            ['permalink'] string The URL pattern each ContentItem will have
79
         *
80
         * @var $collection array
81
         */
82
        foreach ($folders as $collection)
83
        {
84
            $this->output->notice("Loading '{$collection['name']}' collection...");
85
86
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
87
88
            if (!$this->fs->exists($collectionFolder))
89
            {
90
                $this->output->warning("The folder '{$collection['folder']}' could not be found for the '{$collection['name']}' collection");
91
                continue;
92
            }
93
94
            $finder = $this->fs->getFinder(array(), array(), $collectionFolder);
95
96
            /** @var $file SplFileInfo */
97
            foreach ($finder as $file)
98
            {
99
                $filePath = $this->fs->appendPath($collectionFolder, $file->getRelativePathname());
100
                $fileName = $this->fs->getBaseName($filePath);
101
102
                $contentItem = new ContentItem($filePath);
103
                $contentItem->setCollection($collection['name']);
104
105
                $this->collections[$collection['name']][$fileName] = $contentItem;
106
107
                $this->output->info(sprintf(
108
                    "Loading ContentItem into '%s' collection: %s",
109
                    $collection['name'],
110
                    $this->fs->appendPath($collection['folder'], $file->getRelativePathname())
111
                ));
112
            }
113
        }
114
    }
115
116
    private function flattenCollections ()
117
    {
118
        if (!isset($this->collectionsFlat))
119
        {
120
            $this->collectionsFlat = call_user_func_array('array_merge', $this->collections);
0 ignored issues
show
Documentation Bug introduced by
It seems like call_user_func_array('ar...e', $this->collections) of type * is incompatible with the declared type array<integer,object<all...kx\Object\ContentItem>> of property $collectionsFlat.

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...
121
        }
122
    }
123
}