Completed
Push — master ( 943281...5c35c6 )
by Vladimir
02:38
created

CollectionManager::getTentativeCollectionName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 12
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
     */
32 1
    public function &getContentItem ($filePath)
33
    {
34 1
        return $this->trackedItemsFlattened[$filePath];
35
    }
36
37
    /**
38
     * Get all of the Content Items grouped by Collection
39
     *
40
     * @return ContentItem[][]
41
     */
42 2
    public function &getCollections ()
43
    {
44 2
        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
     *
72
     * @return string
73
     */
74
    public function getTentativeCollectionName ($filePath)
75
    {
76
        foreach ($this->collectionDefinitions as $collection)
77
        {
78
            if (strpos($filePath, $collection['folder']) === 0)
79
            {
80
                return $collection['name'];
81
            }
82
        }
83
84
        return '';
85
    }
86
87
    /**
88
     * Parse every collection and store them in the manager
89
     *
90
     * @param string[][] $collections An array of definitions for collections
91
     */
92 4
    public function parseCollections ($collections)
93
    {
94 4
        if ($collections === null)
95 4
        {
96
            $this->output->debug("No collections found, nothing to parse.");
97
            return;
98
        }
99
100 4
        $this->collectionDefinitions = $collections;
101
102
        /**
103
         * The information which each collection has taken from the configuration file
104
         *
105
         * $collection['name']      string The name of the collection
106
         *            ['folder']    string The folder where this collection has its ContentItems
107
         *
108
         * @var $collection array
109
         */
110 4
        foreach ($collections as $collection)
111
        {
112 4
            $this->output->notice("Loading '{$collection['name']}' collection...");
113
114 4
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
115
116 4
            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 4
            $this->saveFolderDefinition($collection['folder'], $collection);
123 4
            $this->scanTrackableItems($collectionFolder, array(
124 4
                'namespace' => $collection['name']
125 4
            ));
126 4
        }
127 4
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function createNewItem($filePath)
133
    {
134
        $collection = $this->getTentativeCollectionName($filePath);
135
136
        return $this->handleTrackableItem($filePath, array(
137
            'namespace' => $collection
138
        ));
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function refreshItem($filePath)
145
    {
146
        return;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 4
    protected function handleTrackableItem($filePath, $options = array())
153
    {
154 4
        $collectionName = $options['namespace'];
155
156 4
        $contentItem = new ContentItem($filePath);
157 4
        $contentItem->setCollection($collectionName);
158
159 4
        $this->addObjectToTracker($contentItem, $contentItem->getName(), $collectionName);
160
161 4
        $this->output->info(sprintf(
162 4
            "Loading ContentItem into '%s' collection: %s",
163 4
            $collectionName,
164 4
            $this->fs->getRelativePath($filePath)
165 4
        ));
166
167 4
        return $contentItem;
168
    }
169
}