Completed
Push — master ( 0c3733...9b1319 )
by Vladimir
02:41
created

CollectionManager::getContentItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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