1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Libcast\AssetDistributor\Asset; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\File; |
6
|
|
|
use Libcast\AssetDistributor\Configuration\CategoryRegistry; |
7
|
|
|
|
8
|
|
|
abstract class AbstractAsset |
9
|
|
|
{ |
10
|
|
|
const VISIBILITY_PUBLIC = 'public'; |
11
|
|
|
|
12
|
|
|
const VISIBILITY_PRIVATE = 'private'; |
13
|
|
|
|
14
|
|
|
const VISIBILITY_HIDDEN = 'hidden'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* @var File |
19
|
|
|
*/ |
20
|
|
|
protected $file; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $etag; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $title; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $description; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $tags = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $category; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $visibility = self::VISIBILITY_PUBLIC; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* @param File $file |
61
|
|
|
*/ |
62
|
|
|
public function __construct(File $file) |
63
|
|
|
{ |
64
|
|
|
$this->file = $file; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @return File |
70
|
|
|
*/ |
71
|
|
|
public function getFile() |
72
|
|
|
{ |
73
|
|
|
return $this->file; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getPath() |
81
|
|
|
{ |
82
|
|
|
$filesystem = $this->file->getFilesystem(); /** @var \League\Flysystem\Filesystem $filesystem */ |
83
|
|
|
|
84
|
|
|
$filesystemAdapter = $filesystem->getAdapter(); /** @var \League\Flysystem\Adapter\AbstractAdapter $filesystemAdapter */ |
85
|
|
|
|
86
|
|
|
return $filesystemAdapter->applyPathPrefix($this->file->getPath()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getMimetype() |
94
|
|
|
{ |
95
|
|
|
return $this->file->getMimetype(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function getSize() |
103
|
|
|
{ |
104
|
|
|
return $this->file->getSize(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getEtag() |
112
|
|
|
{ |
113
|
|
|
if ($this->etag) { |
114
|
|
|
return $this->etag; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->etag = md5(implode(':', [ |
118
|
|
|
$this->getPath(), |
119
|
|
|
$this->getMimetype(), |
120
|
|
|
$this->getSize(), |
121
|
|
|
])); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getTitle() |
129
|
|
|
{ |
130
|
|
|
if (!$this->title) { |
131
|
|
|
$this->setTitle(pathinfo($this->getPath(), PATHINFO_BASENAME)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->title; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* @param string $title |
140
|
|
|
*/ |
141
|
|
|
public function setTitle($title) |
142
|
|
|
{ |
143
|
|
|
$this->title = $title; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getDescription() |
151
|
|
|
{ |
152
|
|
|
return $this->description; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
* @param string $description |
158
|
|
|
*/ |
159
|
|
|
public function setDescription($description) |
160
|
|
|
{ |
161
|
|
|
$this->description = $description; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function getTags() |
169
|
|
|
{ |
170
|
|
|
return $this->tags; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* |
175
|
|
|
* @param array $tags |
176
|
|
|
*/ |
177
|
|
|
public function setTags(array $tags) |
178
|
|
|
{ |
179
|
|
|
$this->tags = $tags; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* |
184
|
|
|
* @param $key |
185
|
|
|
* @param $value |
186
|
|
|
*/ |
187
|
|
|
public function addTag($key, $value) |
188
|
|
|
{ |
189
|
|
|
$this->tags[$key] = $value; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* |
194
|
|
|
* @param null $vendor |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
public function getCategory($vendor = null) |
198
|
|
|
{ |
199
|
|
|
if ($this->category && $vendor) { |
200
|
|
|
return CategoryRegistry::get($this->category, $vendor); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this->category; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* |
208
|
|
|
* @param mixed $category |
209
|
|
|
*/ |
210
|
|
|
public function setCategory($category) |
211
|
|
|
{ |
212
|
|
|
$this->category = $category; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getVisibility() |
220
|
|
|
{ |
221
|
|
|
return $this->visibility; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* |
226
|
|
|
* @param $visibility |
227
|
|
|
* @throws \Exception |
228
|
|
|
*/ |
229
|
|
|
public function setVisibility($visibility) |
230
|
|
|
{ |
231
|
|
|
if (!in_array($visibility, [ |
232
|
|
|
self::VISIBILITY_PUBLIC, |
233
|
|
|
self::VISIBILITY_PRIVATE, |
234
|
|
|
self::VISIBILITY_HIDDEN, |
235
|
|
|
])) { |
236
|
|
|
throw new \Exception("Visibility '$visibility' is not supported"); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$this->visibility = $visibility; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* |
244
|
|
|
* @return bool |
245
|
|
|
*/ |
246
|
|
|
public function isPublic() |
247
|
|
|
{ |
248
|
|
|
return self::VISIBILITY_PUBLIC === $this->getVisibility(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* |
253
|
|
|
* @return bool |
254
|
|
|
*/ |
255
|
|
|
public function isPrivate() |
256
|
|
|
{ |
257
|
|
|
return self::VISIBILITY_PRIVATE === $this->getVisibility(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
|
|
public function isHidden() |
265
|
|
|
{ |
266
|
|
|
return self::VISIBILITY_HIDDEN === $this->getVisibility(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function __toString() |
274
|
|
|
{ |
275
|
|
|
return $this->getEtag(); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|