Completed
Push — master ( 9218af...d5ea06 )
by Stéphane
13:04
created

AssetReference::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 9.7998
c 0
b 0
f 0
1
<?php namespace Rocket\UI\Assets\Assetic\Asset;
2
3
use Assetic\Asset\AssetInterface;
4
use Assetic\AssetManager;
5
use Assetic\Filter\FilterInterface;
6
7
/**
8
 * A reference to an asset in the asset manager.
9
 *
10
 * @author Kris Wallsmith <[email protected]>
11
 */
12
class AssetReference implements AssetInterface
13
{
14
    private $am;
15
    private $name;
16
    private $filters = [];
17
    private $clone = false;
18
    private $asset;
19
20 6
    public function __construct(AssetManager $am, $name)
21
    {
22 6
        $this->am = $am;
23 6
        $this->name = $name;
24 6
    }
25
26
    public function __clone()
27
    {
28
        $this->clone = true;
29
30
        if ($this->asset) {
31
            $this->asset = clone $this->asset;
32
        }
33
    }
34
35
    public function ensureFilter(FilterInterface $filter)
36
    {
37
        $this->filters[] = $filter;
38
    }
39
40
    public function getFilters()
41
    {
42
        $this->flushFilters();
43
44
        return $this->callAsset(__FUNCTION__);
45
    }
46
47
    public function clearFilters()
48
    {
49
        $this->filters = [];
50
        $this->callAsset(__FUNCTION__);
51
    }
52
53
    public function load(FilterInterface $additionalFilter = null)
54
    {
55
        $this->flushFilters();
56
57
        return $this->callAsset(__FUNCTION__, [$additionalFilter]);
58
    }
59
60
    public function dump(FilterInterface $additionalFilter = null)
61
    {
62
        $this->flushFilters();
63
64
        return $this->callAsset(__FUNCTION__, [$additionalFilter]);
65
    }
66
67
    public function getContent()
68
    {
69
        return $this->callAsset(__FUNCTION__);
70
    }
71
72
    public function setContent($content)
73
    {
74
        $this->callAsset(__FUNCTION__, [$content]);
75
    }
76
77
    public function getSourceRoot()
78
    {
79
        return $this->callAsset(__FUNCTION__);
80
    }
81
82 6
    public function getSourcePath()
83
    {
84 6
        return $this->callAsset(__FUNCTION__);
85
    }
86
87 6
    public function getSourceDirectory()
88
    {
89 6
        return $this->callAsset(__FUNCTION__);
90
    }
91
92
    public function getTargetPath()
93
    {
94
        return $this->callAsset(__FUNCTION__);
95
    }
96
97
    public function setTargetPath($targetPath)
98
    {
99
        $this->callAsset(__FUNCTION__, [$targetPath]);
100
    }
101
102
    public function getLastModified()
103
    {
104
        return $this->callAsset(__FUNCTION__);
105
    }
106
107
    public function getVars()
108
    {
109
        return $this->callAsset(__FUNCTION__);
110
    }
111
112
    public function getValues()
113
    {
114
        return $this->callAsset(__FUNCTION__);
115
    }
116
117
    public function setValues(array $values)
118
    {
119
        $this->callAsset(__FUNCTION__, [$values]);
120
    }
121
122
    /**
123
     * @return \Assetic\Asset\AssetInterface
124
     */
125 6
    public function getAsset()
126
    {
127 6
        return $this->resolve();
128
    }
129
130
    // private
131
132 6
    private function callAsset($method, $arguments = array())
133
    {
134 6
        $asset = $this->resolve();
135
136 6
        return call_user_func_array(array($asset, $method), $arguments);
137
    }
138
139
    private function flushFilters()
140
    {
141
        $asset = $this->resolve();
142
143
        while ($filter = array_shift($this->filters)) {
144
            $asset->ensureFilter($filter);
145
        }
146
    }
147
148 6
    private function resolve()
149
    {
150 6
        if ($this->asset) {
151
            return $this->asset;
152
        }
153
154 6
        $asset = $this->am->get($this->name);
155
156 6
        if ($this->clone) {
157
            $asset = $this->asset = clone $asset;
158
        }
159
160 6
        return $asset;
161
    }
162
}
163