AssetCollection   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 53.49%

Importance

Changes 2
Bugs 1 Features 2
Metric Value
eloc 28
c 2
b 1
f 2
dl 0
loc 120
ccs 23
cts 43
cp 0.5349
rs 10
wmc 15

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setAssetType() 0 3 1
A unshift() 0 4 1
A add() 0 3 1
A addRaw() 0 3 1
A getAssetType() 0 3 1
A offsetSet() 0 4 1
A __toString() 0 3 1
A renderRaw() 0 3 1
A newItem() 0 3 1
A render() 0 9 2
A ensureItem() 0 6 2
A getInternal() 0 8 2
1
<?php
2
3
namespace ByTIC\Assets\Assets;
4
5
use Nip\Collections\Typed\ClassCollection;
6
7
/**
8
 * Class AssetCollection
9
 * @package ByTIC\Assets\Assets
10
 */
11
class AssetCollection extends ClassCollection
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $validClass = Asset::class;
17
18
    protected $assetType = '';
19
20
    /**
21
     * @inheritDoc
22
     */
23 1
    public function offsetSet($offset, $value): void
24
    {
25 1
        $this->ensureItem($value);
26 1
        parent::offsetSet($offset, $value);
27 1
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 1
    public function unshift($value, $key = null)
33
    {
34 1
        $this->ensureItem($value);
35 1
        parent::unshift($value, $key);
36 1
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 1
    public function add($source, $name = null)
42
    {
43 1
        parent::add($this->newItem($source), $name);
44 1
    }
45
46
    /**
47
     * @param $content
48
     */
49
    public function addRaw($content)
50
    {
51
        $this->getInternal()->appendContent($content);
52
    }
53
54
    /**
55
     * @return string
56
     */
57 3
    public function getAssetType(): string
58
    {
59 3
        return $this->assetType;
60
    }
61
62
    /**
63
     * @param string $type
64
     */
65 2
    public function setAssetType(string $type): void
66
    {
67 2
        $this->assetType = $type;
68 2
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function __toString()
74
    {
75
        return $this->render();
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function render()
82
    {
83
        $output = [];
84
        $items = $this->all();
85
        unset($items['_internal']);
86
        foreach ($items as $item) {
87
            $output[$item->getSource()] = $item->output();
88
        }
89
        return implode('', $output);
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function renderRaw()
96
    {
97
        return $this->getInternal()->output();
98
    }
99
100
    /**
101
     * @return Asset
102
     */
103
    protected function getInternal()
104
    {
105
        if (!$this->has('_internal')) {
106
            $asset = $this->newItem('');
107
            $this->set('_internal', $asset);
108
            return $asset;
109
        }
110
        return $this->get('_internal');
111
    }
112
113
    /**
114
     * @param $value
115 2
     */
116
    protected function ensureItem(&$value)
117 2
    {
118 1
        if ($value instanceof Asset) {
119
            return;
120 1
        }
121 1
        $value = $this->newItem($value);
122
    }
123
124
    /**
125
     * @param $value
126
     * @return Asset
127 2
     */
128
    protected function newItem($value)
129 2
    {
130
        return new Asset($value, $this->getAssetType());
131
    }
132
}
133