Completed
Push — master ( 6bf463...9a052b )
by Vladimir
03:13
created

CollectionManager::addToCollection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 8.6667

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
ccs 2
cts 14
cp 0.1429
crap 8.6667
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use allejo\stakx\Object\ContentItem;
6
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
7
use Symfony\Component\Finder\SplFileInfo;
8
9
class CollectionManager extends BaseManager
10
{
11
    /**
12
     * @var string[][]
13
     */
14
    private $collectionDefinitions;
15
16
    /**
17
     * @var ContentItem[]
18
     */
19
    private $collectionsFlat;
20
21
    /**
22
     * @var ContentItem[][]
23
     */
24
    private $collections;
25
26 6
    public function __construct ()
27
    {
28 6
        parent::__construct();
29
30 6
        $this->collections = array();
31 6
        $this->collectionsFlat = array();
32 6
    }
33
34
    /**
35
     * @param $filePath
36
     *
37
     * @return ContentItem|null
38
     */
39 1
    public function &getContentItem ($filePath)
40
    {
41 1
        if ($this->isTrackedByManager($filePath))
42 1
        {
43 1
            $contentItemId = $this->fs->getBaseName($filePath);
44
45 1
            return $this->collectionsFlat[$contentItemId];
46
        }
47
48
        return null;
49
    }
50
51 2
    public function getCollections ()
52
    {
53 2
        return $this->collections;
54
    }
55
56 3
    public function getFlatCollections ()
57
    {
58 3
        $this->flattenCollections();
59
60 3
        return $this->collectionsFlat;
61
    }
62
63
    /**
64
     * Check whether a given file path to a content item is already being tracked as part of a collection
65
     *
66
     * @param  string $filePath
67
     *
68
     * @return bool
69
     */
70 2
    public function isTrackedByManager ($filePath)
71
    {
72 2
        $this->flattenCollections();
73
74 2
        $contentItemId = $this->fs->getBaseName($filePath);
75
76 2
        return (array_key_exists($contentItemId, $this->collectionsFlat));
77
    }
78
79
    /**
80
     * Check whether a given file path is inside a directory of a known Collection
81
     *
82
     * @param  string $filePath
83
     *
84
     * @return bool
85
     */
86
    public function belongsToCollection ($filePath)
87
    {
88
        return (!empty($this->getTentativeCollectionName($filePath)));
89
    }
90
91
    /**
92
     * Get the name of the Collection this Content Item belongs to
93
     *
94
     * @param  string $filePath
95
     *
96
     * @return string
97
     */
98
    public function getTentativeCollectionName ($filePath)
99
    {
100
        foreach ($this->collectionDefinitions as $collection)
101
        {
102
            if (strpos($filePath, $collection['folder']) === 0)
103
            {
104
                return $collection['name'];
105
            }
106
        }
107
108
        return '';
109
    }
110
111 6
    public function addToCollection ($filePath)
112
    {
113
        $relativePath = $filePath;
114
        $filePath = $this->fs->absolutePath($filePath);
115
116
        if (!$this->fs->exists($filePath))
117 6
        {
118
            throw new FileNotFoundException(sprintf("Collection item to be added cannot be found: %s", $relativePath));
119
        }
120
121
        $collectionName = $this->getTentativeCollectionName($relativePath);
122
        $contentItem    = $this->addContentItemToCollection($filePath, $collectionName);
123
        $fileName       = $this->fs->getBaseName($contentItem->getRelativeFilePath());
124
125
        if (!empty($this->collectionsFlat))
126
        {
127
            $this->collectionsFlat[$fileName] = $contentItem;
128
        }
129
    }
130
131 6
    public function parseCollections ($collections)
132
    {
133 6
        if ($collections === null)
134 6
        {
135
            $this->output->debug("No collections found, nothing to parse.");
136
            return;
137
        }
138
139 6
        $this->collectionDefinitions = $collections;
140
141
        /**
142
         * The information which each collection has taken from the configuration file
143
         *
144
         * $collection['name']      string The name of the collection
145
         *            ['folder']    string The folder where this collection has its ContentItems
146
         *
147
         * @var $collection array
148
         */
149 6
        foreach ($collections as $collection)
150
        {
151 6
            $this->output->notice("Loading '{$collection['name']}' collection...");
152
153 6
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
154
155 6
            if (!$this->fs->exists($collectionFolder))
156 6
            {
157
                $this->output->warning("The folder '{$collection['folder']}' could not be found for the '{$collection['name']}' collection");
158
                continue;
159
            }
160
161 6
            $finder = $this->fs->getFinder(array(), array(), $collectionFolder);
162
163
            /** @var $file SplFileInfo */
164 6
            foreach ($finder as $file)
165
            {
166 6
                $filePath = $this->fs->appendPath($collectionFolder, $file->getRelativePathname());
167
168 6
                $this->addContentItemToCollection($filePath, $collection['name']);
169 6
            }
170 6
        }
171 6
    }
172
173 6
    private function addContentItemToCollection ($filePath, $collectionName)
174
    {
175 6
        $fileName = $this->fs->getBaseName($filePath);
176
177 6
        $contentItem = new ContentItem($filePath);
178 6
        $contentItem->setCollection($collectionName);
179
180 6
        $this->collections[$collectionName][$fileName] = $contentItem;
181
182 6
        $this->output->info(sprintf(
183 6
            "Loading ContentItem into '%s' collection: %s",
184 6
            $collectionName,
185 6
            $this->fs->getRelativePath($filePath)
186 6
        ));
187
188 6
        return $contentItem;
189
    }
190
191 5
    private function flattenCollections ()
192
    {
193 5
        if (empty($this->collectionsFlat) && !empty($this->collections))
194 5
        {
195 4
            $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...
196 4
        }
197
    }
198
}