Completed
Pull Request — master (#48)
by Vladimir
02:35
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\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[][]
0 ignored issues
show
Documentation introduced by
Should the return type not be 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...
61
     */
62 19
    public function getJailedCollections()
63
    {
64 19
        return $this->getJailedTrackedItems();
65
    }
66
67
    /**
68
     * Parse every collection and store them in the manager.
69
     *
70
     * @param string[][] $collections An array of definitions for collections
71
     */
72 37
    public function parseCollections($collections)
73
    {
74 37
        if ($collections === null)
75 37
        {
76
            $this->output->debug('No collections found, nothing to parse.');
77
78
            return;
79
        }
80
81 37
        $this->collectionDefinitions = $collections;
82
83
        /**
84
         * The information which each collection has taken from the configuration file.
85
         *
86
         * $collection['name']      string The name of the collection
87
         *            ['folder']    string The folder where this collection has its ContentItems
88
         *
89
         * @var array
90
         */
91 37
        foreach ($collections as $collection)
92
        {
93 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...
94
95 37
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
96
97 37
            if (!$this->fs->exists($collectionFolder))
98 37
            {
99
                $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...
100
                continue;
101
            }
102
103 37
            $this->saveFolderDefinition($collection['folder'], $collection);
104 37
            $this->scanTrackableItems($collectionFolder, array(
105 37
                'namespace' => $collection['name'],
106 37
            ));
107 37
        }
108 37
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 37
    public function createNewItem($filePath)
114
    {
115
        $collection = $this->getTentativeCollectionName($filePath);
116
117 37
        return $this->handleTrackableItem($filePath, array(
118
            'namespace' => $collection,
119
        ));
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function refreshItem($filePath)
126
    {
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 37
    protected function handleTrackableItem($filePath, $options = array())
133
    {
134 37
        $collectionName = $options['namespace'];
135
136 37
        $contentItem = new ContentItem($filePath);
137 37
        $contentItem->setNamespace($collectionName);
138
139 37
        $this->addObjectToTracker($contentItem, $contentItem->getName(), $collectionName);
140
141 37
        $this->output->info(sprintf(
142 37
            "Loading ContentItem into '%s' collection: %s",
143 37
            $collectionName,
144 37
            $this->fs->getRelativePath($filePath)
145 37
        ));
146
147 37
        return $contentItem;
148
    }
149
150
    /**
151
     * Get the name of the Collection this Content Item belongs to.
152
     *
153
     * @param string $filePath
154
     *
155
     * @return string
156
     */
157
    private function getTentativeCollectionName($filePath)
158
    {
159
        foreach ($this->collectionDefinitions as $collection)
160
        {
161
            if (strpos($filePath, $collection['folder']) === 0)
162
            {
163
                return $collection['name'];
164
            }
165
        }
166
167
        return '';
168
    }
169
}
170