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

CollectionManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 67.39%

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 31
cts 46
cp 0.6739
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentItem() 0 4 1
A getCollections() 0 4 1
A getFlatCollections() 0 4 1
A belongsToCollection() 0 4 1
A getTentativeCollectionName() 0 12 3
B parseCollections() 0 35 4
A refreshItem() 0 4 1
A handleTrackableItem() 0 17 1
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use allejo\stakx\Object\ContentItem;
6
7
class CollectionManager extends TrackingManager
8
{
9
    /**
10
     * @var string[][]
11
     */
12
    private $collectionDefinitions;
13
14
    /**
15
     * @param  string $filePath
16
     *
17
     * @return ContentItem|null
18
     */
19 1
    public function &getContentItem ($filePath)
20
    {
21 1
        return $this->trackedItemsFlattened[$filePath];
22
    }
23
24 2
    public function getCollections ()
25
    {
26 2
        return $this->trackedItems;
27
    }
28
29 3
    public function getFlatCollections ()
30
    {
31 3
        return $this->trackedItemsFlattened;
32
    }
33
34
    /**
35
     * Check whether a given file path is inside a directory of a known Collection
36
     *
37
     * @param  string $filePath
38
     *
39
     * @return bool
40
     */
41
    public function belongsToCollection ($filePath)
42
    {
43
        return (!empty($this->getTentativeCollectionName($filePath)));
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 6
    public function parseCollections ($collections)
67
    {
68 6
        if ($collections === null)
69 6
        {
70
            $this->output->debug("No collections found, nothing to parse.");
71
            return;
72
        }
73
74 6
        $this->collectionDefinitions = $collections;
75
76
        /**
77
         * The information which each collection has taken from the configuration file
78
         *
79
         * $collection['name']      string The name of the collection
80
         *            ['folder']    string The folder where this collection has its ContentItems
81
         *
82
         * @var $collection array
83
         */
84 6
        foreach ($collections as $collection)
85
        {
86 6
            $this->output->notice("Loading '{$collection['name']}' collection...");
87
88 6
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
89
90 6
            if (!$this->fs->exists($collectionFolder))
91 6
            {
92
                $this->output->warning("The folder '{$collection['folder']}' could not be found for the '{$collection['name']}' collection");
93
                continue;
94
            }
95
96 6
            $this->scanTrackableItems($collectionFolder, array(
97 6
                'namespace' => $collection['name']
98 6
            ));
99 6
        }
100 6
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function refreshItem($filePath)
106
    {
107
        return;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 6
    protected function handleTrackableItem($filePath, $options = array())
114
    {
115 6
        $collectionName = $options['namespace'];
116
117 6
        $contentItem = new ContentItem($filePath);
118 6
        $contentItem->setCollection($collectionName);
119
120 6
        $this->addObjectToTracker($contentItem, $collectionName);
121
122 6
        $this->output->info(sprintf(
123 6
            "Loading ContentItem into '%s' collection: %s",
124 6
            $collectionName,
125 6
            $this->fs->getRelativePath($filePath)
126 6
        ));
127
128 6
        return $contentItem;
129
    }
130
}