Completed
Pull Request — master (#11)
by Vladimir
02:26
created

CollectionManager::getCollections()   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 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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|null
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
     * Check whether a given file path is inside a directory of a known Collection
48
     *
49
     * @param  string $filePath
50
     *
51
     * @return bool
52
     */
53
    public function belongsToCollection ($filePath)
54
    {
55
        return (!empty($this->getTentativeCollectionName($filePath)));
56
    }
57
58
    /**
59
     * Get the name of the Collection this Content Item belongs to
60
     *
61
     * @param  string $filePath
62
     *
63
     * @return string
64
     */
65
    public function getTentativeCollectionName ($filePath)
66
    {
67
        foreach ($this->collectionDefinitions as $collection)
68
        {
69
            if (strpos($filePath, $collection['folder']) === 0)
70
            {
71
                return $collection['name'];
72
            }
73
        }
74
75
        return '';
76
    }
77
78
    /**
79
     * Parse every collection and store them in the manager
80
     *
81
     * @param string[][] $collections An array of definitions for collections
82
     */
83 4
    public function parseCollections ($collections)
84
    {
85 4
        if ($collections === null)
86 4
        {
87
            $this->output->debug("No collections found, nothing to parse.");
88
            return;
89
        }
90
91 4
        $this->collectionDefinitions = $collections;
92
93
        /**
94
         * The information which each collection has taken from the configuration file
95
         *
96
         * $collection['name']      string The name of the collection
97
         *            ['folder']    string The folder where this collection has its ContentItems
98
         *
99
         * @var $collection array
100
         */
101 4
        foreach ($collections as $collection)
102
        {
103 4
            $this->output->notice("Loading '{$collection['name']}' collection...");
104
105 4
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
106
107 4
            if (!$this->fs->exists($collectionFolder))
108 4
            {
109
                $this->output->warning("The folder '{$collection['folder']}' could not be found for the '{$collection['name']}' collection");
110
                continue;
111
            }
112
113 4
            $this->scanTrackableItems($collectionFolder, array(
114 4
                'namespace' => $collection['name']
115 4
            ));
116 4
        }
117 4
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function refreshItem($filePath)
123
    {
124
        return;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 4
    protected function handleTrackableItem($filePath, $options = array())
131
    {
132 4
        $collectionName = $options['namespace'];
133
134 4
        $contentItem = new ContentItem($filePath);
135 4
        $contentItem->setCollection($collectionName);
136
137 4
        $this->addObjectToTracker($contentItem, $collectionName);
138
139 4
        $this->output->info(sprintf(
140 4
            "Loading ContentItem into '%s' collection: %s",
141 4
            $collectionName,
142 4
            $this->fs->getRelativePath($filePath)
143 4
        ));
144
145 4
        return $contentItem;
146
    }
147
}