Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

CollectionManager::getContentItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.6666
c 0
b 0
f 0
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\Configuration;
11
use allejo\stakx\Document\ContentItem;
12
use allejo\stakx\Document\JailedDocument;
13
use allejo\stakx\Exception\TrackedItemNotFoundException;
14
use allejo\stakx\Filesystem\FilesystemLoader as fs;
15
16
/**
17
 * The class that reads and saves information about all of the collections.
18
 */
19
class CollectionManager extends TrackingManager
20
{
21
    /** @var string[][] A copy of the collection definitions to be available for later usage. */
22
    private $collectionDefinitions;
23
    private $configuration;
24
25
    /**
26
     * CollectionManager constructor.
27
     */
28 37
    public function __construct(Configuration $configuration)
29
    {
30 37
        parent::__construct();
31
32 37
        $this->configuration = $configuration;
33 37
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 5
    public function compileManager()
39
    {
40 5
        if (!$this->configuration->hasCollections())
41 5
        {
42
            $this->output->notice('No Collections defined... Ignoring');
43
            return;
44
        }
45
46 5
        $this->parseCollections($this->configuration->getCollectionsFolders());
47 5
    }
48
49
    /**
50
     * Get all of the ContentItems grouped by Collection name.
51
     *
52
     * @return ContentItem[][]
0 ignored issues
show
Documentation introduced by
Should the return type not be \allejo\stakx\Document\ReadableDocument[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
53
     */
54 16
    public function &getCollections()
55
    {
56 16
        return $this->trackedItems;
57
    }
58
59
    /**
60
     * Get a ContentItem from a Collection pased on it's path.
61
     *
62
     * @param string $filePath
63
     *
64
     * @throws TrackedItemNotFoundException
65
     *
66
     * @return ContentItem
67
     */
68 1
    public function &getContentItem($filePath)
69
    {
70 1
        if (!isset($this->trackedItemsFlattened[$filePath]))
71 1
        {
72
            throw new TrackedItemNotFoundException("The ContentItem at '$filePath' was not found.");
73
        }
74
75 1
        return $this->trackedItemsFlattened[$filePath];
76
    }
77
78
    /**
79
     * A jailed representation of CollectionManager::getCollections().
80
     *
81
     * @return JailedDocument[][]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<JailedDocument|JailedDocument[]>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
82
     */
83 19
    public function getJailedCollections()
84
    {
85 19
        return self::getJailedTrackedItems($this->trackedItemsFlattened);
86
    }
87
88
    /**
89
     * Parse every collection and store them in the manager.
90
     *
91
     * @param string[][] $collections An array of definitions for collections
92
     */
93 37
    public function parseCollections($collections)
94
    {
95 37
        if ($collections === null)
96 37
        {
97
            $this->output->debug('No collections found, nothing to parse.');
98
99
            return;
100
        }
101
102 37
        $this->collectionDefinitions = $collections;
103
104
        /**
105
         * The information which each collection has taken from the configuration file.
106
         *
107
         * $collection['name']   string The name of the collection
108
         *            ['folder'] string The folder where this collection has its ContentItems
109
         *
110
         * @var array
111
         */
112 37
        foreach ($collections as $collection)
113
        {
114 37
            $this->output->notice("Loading '{name}' collection...", [
115 37
                'name' => $collection['name'],
116 37
            ]);
117
118 37
            $collectionFolder = fs::absolutePath($collection['folder']);
119
120 37
            if (!fs::exists($collectionFolder))
121 37
            {
122
                $this->output->warning("The folder '{folder}' could not be found for the '{name}' collection", [
123
                    'folder' => $collection['folder'],
124
                    'name'   => $collection['name'],
125
                ]);
126
                continue;
127
            }
128
129 37
            $this->saveFolderDefinition($collection['folder'], $collection);
130 37
            $this->scanTrackableItems($collectionFolder, array(
131 37
                'namespace' => $collection['name'],
132 37
            ));
133 37
        }
134 37
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function createNewItem($filePath)
140
    {
141
        $collection = $this->getTentativeCollectionName($filePath);
142
143
        return $this->handleTrackableItem($filePath, array(
144
            'namespace' => $collection,
145
        ));
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function refreshItem($filePath)
152
    {
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 37
    protected function handleTrackableItem($filePath, array $options = array())
159
    {
160 37
        $collectionName = $options['namespace'];
161
162 37
        $contentItem = new ContentItem($filePath);
163 37
        $contentItem->setNamespace($collectionName);
164
165 37
        $this->addObjectToTracker($contentItem, $collectionName);
166
167 37
        $this->output->info("Loading ContentItem into '{name}' collection: {path}", [
168 37
            'name' => $collectionName,
169 37
            'path' => fs::getRelativePath($filePath),
170 37
        ]);
171
172 37
        return $contentItem;
173
    }
174
175
    /**
176
     * Get the name of the Collection this Content Item belongs to.
177
     *
178
     * @param string $filePath
179
     *
180
     * @return string
181
     */
182
    private function getTentativeCollectionName($filePath)
183
    {
184
        foreach ($this->collectionDefinitions as $collection)
185
        {
186
            if (strpos($filePath, $collection['folder']) === 0)
187
            {
188
                return $collection['name'];
189
            }
190
        }
191
192
        return '';
193
    }
194
}
195