Completed
Push — master ( a322af...6a50c0 )
by Vladimir
02:30
created

CollectionManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90.74%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
c 7
b 0
f 1
dl 0
loc 116
ccs 49
cts 54
cp 0.9074
rs 10
wmc 14
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getContentItem() 0 11 2
A getCollections() 0 4 1
A getFlatCollections() 0 6 1
A isContentItem() 0 8 1
B parseCollections() 0 49 5
A flattenCollections() 0 7 3
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use allejo\stakx\Object\ContentItem;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
class CollectionManager extends ItemManager
9
{
10
    /**
11
     * @var ContentItem[]
12
     */
13
    private $collectionsFlat;
14
15
    /**
16
     * @var ContentItem[][]
17
     */
18
    private $collections;
19
20 6
    public function __construct ()
21
    {
22 6
        parent::__construct();
23
24 6
        $this->collections = array();
25 6
        $this->collectionsFlat = array();
26 6
    }
27
28
    /**
29
     * @param $filePath
30
     *
31
     * @return ContentItem|null
32
     */
33 1
    public function &getContentItem ($filePath)
34
    {
35 1
        if ($this->isContentItem($filePath))
36 1
        {
37 1
            $contentItemId = $this->fs->getBaseName($filePath);
38
39 1
            return $this->collectionsFlat[$contentItemId];
40
        }
41
42
        return null;
43
    }
44
45 2
    public function getCollections ()
46
    {
47 2
        return $this->collections;
48
    }
49
50 3
    public function getFlatCollections ()
51
    {
52 3
        $this->flattenCollections();
53
54 3
        return $this->collectionsFlat;
55
    }
56
57 2
    public function isContentItem ($filePath)
58
    {
59 2
        $this->flattenCollections();
60
61 2
        $contentItemId = $this->fs->getBaseName($filePath);
62
63 2
        return (array_key_exists($contentItemId, $this->collectionsFlat));
64
    }
65
66 6
    public function parseCollections ($folders)
67
    {
68 6
        if ($folders === null)
69 6
        {
70
            $this->output->debug("No collections found, nothing to parse.");
71
            return;
72
        }
73
74
        /**
75
         * The information which each collection has taken from the configuration file
76
         *
77
         * $collection['name']      string The name of the collection
78
         *            ['folder']    string The folder where this collection has its ContentItems
79
         *
80
         * @var $collection array
81
         */
82 6
        foreach ($folders as $collection)
83
        {
84 6
            $this->output->notice("Loading '{$collection['name']}' collection...");
85
86 6
            $collectionFolder = $this->fs->absolutePath($collection['folder']);
87
88 6
            if (!$this->fs->exists($collectionFolder))
89 6
            {
90
                $this->output->warning("The folder '{$collection['folder']}' could not be found for the '{$collection['name']}' collection");
91
                continue;
92
            }
93
94 6
            $finder = $this->fs->getFinder(array(), array(), $collectionFolder);
95
96
            /** @var $file SplFileInfo */
97 6
            foreach ($finder as $file)
98
            {
99 6
                $filePath = $this->fs->appendPath($collectionFolder, $file->getRelativePathname());
100 6
                $fileName = $this->fs->getBaseName($filePath);
101
102 6
                $contentItem = new ContentItem($filePath);
103 6
                $contentItem->setCollection($collection['name']);
104
105 6
                $this->collections[$collection['name']][$fileName] = $contentItem;
106
107 6
                $this->output->info(sprintf(
108 6
                    "Loading ContentItem into '%s' collection: %s",
109 6
                    $collection['name'],
110 6
                    $this->fs->appendPath($collection['folder'], $file->getRelativePathname())
111 6
                ));
112 6
            }
113 6
        }
114 6
    }
115
116 6
    private function flattenCollections ()
117 6
    {
118 5
        if (empty($this->collectionsFlat) && !empty($this->collections))
119 5
        {
120 4
            $this->collectionsFlat = call_user_func_array('array_merge', $this->collections);
0 ignored issues
show
Documentation Bug introduced by
It seems like call_user_func_array('ar...e', $this->collections) of type * is incompatible with the declared type array<integer,object<all...kx\Object\ContentItem>> of property $collectionsFlat.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
121 4
        }
122
    }
123
}