Completed
Pull Request — master (#48)
by Vladimir
03:04
created

CollectionManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 67.24%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 6
dl 0
loc 164
ccs 39
cts 58
cp 0.6724
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollections() 0 4 1
A getContentItem() 0 9 2
A getJailedCollections() 0 17 4
B parseCollections() 0 37 4
A createNewItem() 0 8 1
A refreshItem() 0 3 1
A handleTrackableItem() 0 17 1
A getTentativeCollectionName() 0 12 3
1
<?php
2
3
/**
4
 * @copyright 2017 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\Command\BuildableCommand;
11
use allejo\stakx\Document\ContentItem;
12
use allejo\stakx\Document\JailedDocument;
13
use allejo\stakx\Exception\TrackedItemNotFoundException;
14
use allejo\stakx\Service;
15
16
/**
17
 * The class that reads and saves information about all of the collections.
18
 */
19
class CollectionManager extends TrackingManager
20
{
21
    /**
22
     * A copy of the collection definitions to be available for later usage.
23
     *
24
     * @var string[][]
25
     */
26
    private $collectionDefinitions;
27
28
    /**
29
     * Get all of the ContentItems grouped by Collection name.
30
     *
31
     * @return ContentItem[][]
32
     */
33 16
    public function &getCollections()
34
    {
35 16
        return $this->trackedItems;
36
    }
37
38
    /**
39
     * Get a ContentItem from a Collection pased on it's path.
40
     *
41
     * @param string $filePath
42
     *
43
     * @throws TrackedItemNotFoundException
44
     *
45
     * @return ContentItem
46
     */
47 1
    public function &getContentItem($filePath)
48
    {
49 1
        if (!isset($this->trackedItemsFlattened[$filePath]))
50 1
        {
51
            throw new TrackedItemNotFoundException("The ContentItem at '$filePath' was not found.");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $filePath instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
52
        }
53
54 1
        return $this->trackedItemsFlattened[$filePath];
55
    }
56
57
    /**
58
     * A jailed representation of CollectionManager::getCollections().
59
     *
60
     * @return JailedDocument[][]
61
     */
62 19
    public function getJailedCollections()
63
    {
64 19
        $jailItems = array();
65
66
        /**
67
         * @var string      $key
68
         * @var ContentItem $item
69
         */
70 19
        foreach ($this->trackedItemsFlattened as &$item)
71
        {
72 19
            if (!Service::getParameter(BuildableCommand::USE_DRAFTS) && $item['draft']) { continue; }
73
74 19
            $jailItems[$item->getNamespace()][$item->getName()] = $item->createJail();
75 19
        }
76
77 19
        return $jailItems;
78
    }
79
80
    /**
81
     * Parse every collection and store them in the manager.
82
     *
83
     * @param string[][] $collections An array of definitions for collections
84
     */
85 37
    public function parseCollections($collections)
86
    {
87 37
        if ($collections === null)
88 37
        {
89
            $this->output->debug('No collections found, nothing to parse.');
90
91
            return;
92
        }
93
94 37
        $this->collectionDefinitions = $collections;
95
96
        /**
97
         * The information which each collection has taken from the configuration file.
98
         *
99
         * $collection['name']      string The name of the collection
100
         *            ['folder']    string The folder where this collection has its ContentItems
101
         *
102
         * @var array
103
         */
104 37
        foreach ($collections as $collection)
105
        {
106 37
            $this->output->notice("Loading '{$collection['name']}' collection...");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $collection instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
107
108 37
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
109
110 37
            if (!$this->fs->exists($collectionFolder))
111 37
            {
112
                $this->output->warning("The folder '{$collection['folder']}' could not be found for the '{$collection['name']}' collection");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $collection instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
113
                continue;
114
            }
115
116 37
            $this->saveFolderDefinition($collection['folder'], $collection);
117 37
            $this->scanTrackableItems($collectionFolder, array(
118 37
                'namespace' => $collection['name'],
119 37
            ));
120 37
        }
121 37
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function createNewItem($filePath)
127
    {
128
        $collection = $this->getTentativeCollectionName($filePath);
129
130
        return $this->handleTrackableItem($filePath, array(
131
            'namespace' => $collection,
132
        ));
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function refreshItem($filePath)
139
    {
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 37
    protected function handleTrackableItem($filePath, $options = array())
146
    {
147 37
        $collectionName = $options['namespace'];
148
149 37
        $contentItem = new ContentItem($filePath);
150 37
        $contentItem->setNamespace($collectionName);
151
152 37
        $this->addObjectToTracker($contentItem, $contentItem->getName(), $collectionName);
153
154 37
        $this->output->info(sprintf(
155 37
            "Loading ContentItem into '%s' collection: %s",
156 37
            $collectionName,
157 37
            $this->fs->getRelativePath($filePath)
158 37
        ));
159
160 37
        return $contentItem;
161
    }
162
163
    /**
164
     * Get the name of the Collection this Content Item belongs to.
165
     *
166
     * @param string $filePath
167
     *
168
     * @return string
169
     */
170
    private function getTentativeCollectionName($filePath)
171
    {
172
        foreach ($this->collectionDefinitions as $collection)
173
        {
174
            if (strpos($filePath, $collection['folder']) === 0)
175
            {
176
                return $collection['name'];
177
            }
178
        }
179
180
        return '';
181
    }
182
}
183