Completed
Pull Request — master (#45)
by Vladimir
02:32
created

CollectionManager::createNewItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
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
        {
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
        return from($this->trackedItems)
65
            ->select(function ($v) {
66 19
                return from($v)
67 19
                    ->where(function ($v) {
68 19
                        return Service::getParameter(BuildableCommand::USE_DRAFTS) || !$v->isDraft();
69 19
                    })
70 19
                    ->select('$v->createJail()')
71
                ;
72 19
            })
73 19
            ->toArrayDeep()
74
        ;
75
    }
76
77
    /**
78
     * Parse every collection and store them in the manager.
79
     *
80
     * @param string[][] $collections An array of definitions for collections
81
     */
82 37
    public function parseCollections($collections)
83
    {
84 37
        if ($collections === null)
85
        {
86
            $this->output->debug('No collections found, nothing to parse.');
87
88
            return;
89
        }
90
91 37
        $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 array
100
         */
101 37
        foreach ($collections as $collection)
102
        {
103 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...
104
105 37
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
106
107 37
            if (!$this->fs->exists($collectionFolder))
108
            {
109
                $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...
110
                continue;
111
            }
112
113 37
            $this->saveFolderDefinition($collection['folder'], $collection);
114 37
            $this->scanTrackableItems($collectionFolder, array(
115 37
                'namespace' => $collection['name'],
116
            ));
117
        }
118 37
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function createNewItem($filePath)
124
    {
125
        $collection = $this->getTentativeCollectionName($filePath);
126
127
        return $this->handleTrackableItem($filePath, array(
128
            'namespace' => $collection,
129
        ));
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function refreshItem($filePath)
136
    {
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 37
    protected function handleTrackableItem($filePath, $options = array())
143
    {
144 37
        $collectionName = $options['namespace'];
145
146 37
        $contentItem = new ContentItem($filePath);
147 37
        $contentItem->setCollection($collectionName);
148
149 37
        $this->addObjectToTracker($contentItem, $contentItem->getName(), $collectionName);
150
151 37
        $this->output->info(sprintf(
152 37
            "Loading ContentItem into '%s' collection: %s",
153
            $collectionName,
154 37
            $this->fs->getRelativePath($filePath)
155
        ));
156
157 37
        return $contentItem;
158
    }
159
160
    /**
161
     * Get the name of the Collection this Content Item belongs to.
162
     *
163
     * @param string $filePath
164
     *
165
     * @return string
166
     */
167
    private function getTentativeCollectionName($filePath)
168
    {
169
        foreach ($this->collectionDefinitions as $collection)
170
        {
171
            if (strpos($filePath, $collection['folder']) === 0)
172
            {
173
                return $collection['name'];
174
            }
175
        }
176
177
        return '';
178
    }
179
}
180