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

CollectionManager::getFlatCollections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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
}