NewCollectionTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 19
dl 0
loc 76
ccs 29
cts 29
cp 1
rs 10
c 2
b 1
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewCollection() 0 7 1
A initCollection() 0 5 1
A getCollection() 0 7 2
A addCollection() 0 3 1
A prepareCollectionImages() 0 5 2
A prepareCollectionFiles() 0 4 2
A prepareCollection() 0 4 1
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