NewCollectionTrait::getCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 2
b 1
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\MediaRepository\Traits;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
7
/**
8
 * Trait NewCollectionTrait.
9
 */
10
trait NewCollectionTrait
11
{
12
    /**
13
     * @param string $collectionName
14
     *
15
     * @return Collection
16
     */
17 11
    public function getCollection(string $collectionName): Collection
18
    {
19 11
        if (!isset($this->collections[$collectionName])) {
20 11
            $this->initCollection($collectionName);
21
        }
22
23 11
        return $this->collections[$collectionName];
24
    }
25
26
    /**
27
     * @param string $collectionName
28
     */
29 11
    protected function initCollection(string $collectionName)
30
    {
31 11
        $collection = $this->getNewCollection($collectionName);
32 11
        $this->prepareCollection($collection);
33 11
        $this->addCollection($collection);
34 11
    }
35
36
    /**
37
     * @param string $collectionName
38
     *
39
     * @return Collection
40
     */
41 11
    protected function getNewCollection(string $collectionName)
42
    {
43 11
        $collection = new Collection();
44 11
        $collection->setName($collectionName);
45 11
        $collection->setMediaRepository($this);
46
47 11
        return $collection;
48
    }
49
50
    /**
51
     * @param Collection $collection
52
     */
53 11
    protected function prepareCollection($collection)
54
    {
55 11
        $this->prepareCollectionImages($collection);
56 11
        $this->prepareCollectionFiles($collection);
57 11
    }
58
59
    /**
60
     * @param Collection $collection
61
     */
62 11
    protected function prepareCollectionImages($collection)
63
    {
64 11
        if (in_array($collection->getName(), ['images', 'covers', 'logos'])) {
65 4
            $collection->setMediaType('images');
66 4
            $collection->setOriginalPath('full');
67
        }
68 11
    }
69
70
    /**
71
     * @param Collection $collection
72
     */
73 11
    protected function prepareCollectionFiles($collection)
74
    {
75 11
        if (in_array($collection->getName(), ['files'])) {
76 7
            $collection->setMediaType('files');
77
        }
78 11
    }
79
80
    /**
81
     * @param Collection $collection
82
     */
83 11
    protected function addCollection(Collection $collection)
84
    {
85 11
        $this->collections[$collection->getName()] = $collection;
0 ignored issues
show
Bug Best Practice introduced by
The property collections does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86 11
    }
87
}
88