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

CollectionManager::getJailedCollections()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 14
ccs 0
cts 7
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2016 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\Object\ContentItem;
11
use allejo\stakx\Object\JailObject;
12
13
/**
14
 * Class CollectionManager
15
 *
16
 * @package allejo\stakx\Manager
17
 */
18
class CollectionManager extends TrackingManager
19
{
20
    /**
21
     * A copy of the collection definitions to be available for later usage
22
     *
23
     * @var string[][]
24
     */
25
    private $collectionDefinitions;
26
27
    /**
28
     * @param  string $filePath
29
     *
30
     * @return ContentItem
31 1
     */
32
    public function &getContentItem ($filePath)
33 1
    {
34
        return $this->trackedItemsFlattened[$filePath];
35
    }
36
37
    /**
38
     * Get all of the Content Items grouped by Collection
39
     *
40
     * @return ContentItem[][]
41 2
     */
42
    public function &getCollections ()
43 2
    {
44
        return $this->trackedItems;
45
    }
46
47
    /**
48
     * Get jailed ContentItems
49
     *
50
     * @return JailObject[][]
51
     */
52
    public function getJailedCollections ()
53
    {
54
        $jailItems = array();
55
56
        foreach ($this->trackedItems as $key => $items)
57
        {
58
            foreach ($items as $name => $contentItem)
59
            {
60
                $jailItems[$key][$name] = $contentItem->createJail();
61
            }
62
        }
63
64
        return $jailItems;
65
    }
66
67
    /**
68
     * Get the name of the Collection this Content Item belongs to
69
     *
70
     * @param  string $filePath
71 4
     *
72
     * @return string
73 4
     */
74 4
    public function getTentativeCollectionName ($filePath)
75
    {
76
        foreach ($this->collectionDefinitions as $collection)
77
        {
78
            if (strpos($filePath, $collection['folder']) === 0)
79 4
            {
80
                return $collection['name'];
81
            }
82
        }
83
84
        return '';
85
    }
86
87
    /**
88
     * Parse every collection and store them in the manager
89 4
     *
90
     * @param string[][] $collections An array of definitions for collections
91 4
     */
92
    public function parseCollections ($collections)
93 4
    {
94
        if ($collections === null)
95 4
        {
96 4
            $this->output->debug("No collections found, nothing to parse.");
97
            return;
98
        }
99
100
        $this->collectionDefinitions = $collections;
101 4
102 4
        /**
103 4
         * The information which each collection has taken from the configuration file
104 4
         *
105 4
         * $collection['name']      string The name of the collection
106 4
         *            ['folder']    string The folder where this collection has its ContentItems
107
         *
108
         * @var $collection array
109
         */
110
        foreach ($collections as $collection)
111 4
        {
112
            $this->output->notice("Loading '{$collection['name']}' collection...");
113
114
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
115
116
            if (!$this->fs->exists($collectionFolder))
117 4
            {
118
                $this->output->warning("The folder '{$collection['folder']}' could not be found for the '{$collection['name']}' collection");
119
                continue;
120
            }
121
122
            $this->saveFolderDefinition($collection['folder'], $collection);
123
            $this->scanTrackableItems($collectionFolder, array(
124
                'namespace' => $collection['name']
125
            ));
126
        }
127
    }
128
129
    /**
130
     * {@inheritdoc}
131 4
     */
132
    public function createNewItem($filePath)
133 4
    {
134
        $collection = $this->getTentativeCollectionName($filePath);
135 4
136 4
        return $this->handleTrackableItem($filePath, array(
137
            'namespace' => $collection
138 4
        ));
139
    }
140 4
141 4
    /**
142 4
     * {@inheritdoc}
143 4
     */
144 4
    public function refreshItem($filePath)
145
    {
146 4
        return;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    protected function handleTrackableItem($filePath, $options = array())
153
    {
154
        $collectionName = $options['namespace'];
155
156
        $contentItem = new ContentItem($filePath);
157
        $contentItem->setCollection($collectionName);
158
159
        $this->addObjectToTracker($contentItem, $contentItem->getName(), $collectionName);
160
161
        $this->output->info(sprintf(
162
            "Loading ContentItem into '%s' collection: %s",
163
            $collectionName,
164
            $this->fs->getRelativePath($filePath)
165
        ));
166
167
        return $contentItem;
168
    }
169
}