Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 65 |
|
0.00% |
0 / 27 |
CRAP | |
0.00% |
0 / 1 |
Media | |
0.00% |
0 / 65 |
|
0.00% |
0 / 27 |
1482 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
encrypt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
decrypt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasPassword | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setPassword | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
comparePassword | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
getFileName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExtension | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getAbsolutePath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
getVirtualPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setVirtualPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addMediaType | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
removeMediaType | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getMediaTypes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMediaType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMediaTypeName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
hasMediaTypeId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasMediaTypeName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
addTag | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
removeTag | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toArray | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package Modules\Media\Models |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace Modules\Media\Models; |
16 | |
17 | use Modules\Admin\Models\Account; |
18 | use Modules\Admin\Models\NullAccount; |
19 | use Modules\Tag\Models\NullTag; |
20 | use Modules\Tag\Models\Tag; |
21 | use phpOMS\Security\EncryptionHelper; |
22 | |
23 | /** |
24 | * Media class. |
25 | * |
26 | * @package Modules\Media\Models |
27 | * @license OMS License 2.0 |
28 | * @link https://jingga.app |
29 | * @since 1.0.0 |
30 | */ |
31 | class Media implements \JsonSerializable |
32 | { |
33 | /** |
34 | * ID. |
35 | * |
36 | * @var int |
37 | * @since 1.0.0 |
38 | */ |
39 | public int $id = 0; |
40 | |
41 | /** |
42 | * Name. |
43 | * |
44 | * @var string |
45 | * @since 1.0.0 |
46 | */ |
47 | public string $name = ''; |
48 | |
49 | /** |
50 | * Content. |
51 | * |
52 | * @var null|MediaContent |
53 | * @since 1.0.0 |
54 | */ |
55 | public ?MediaContent $content = null; |
56 | |
57 | /** |
58 | * Type. |
59 | * |
60 | * @var MediaType[] |
61 | * @since 1.0.0 |
62 | */ |
63 | public array $types = []; |
64 | |
65 | /** |
66 | * Extension. |
67 | * |
68 | * @var string |
69 | * @since 1.0.0 |
70 | */ |
71 | public string $extension = ''; |
72 | |
73 | /** |
74 | * File size in bytes. |
75 | * |
76 | * @var int |
77 | * @since 1.0.0 |
78 | */ |
79 | public int $size = 0; |
80 | |
81 | /** |
82 | * Author. |
83 | * |
84 | * @var Account |
85 | * @since 1.0.0 |
86 | */ |
87 | public Account $createdBy; |
88 | |
89 | /** |
90 | * Uploaded. |
91 | * |
92 | * @var \DateTimeImmutable |
93 | * @since 1.0.0 |
94 | */ |
95 | public \DateTimeImmutable $createdAt; |
96 | |
97 | /** |
98 | * Resource path. |
99 | * |
100 | * @var string |
101 | * @since 1.0.0 |
102 | */ |
103 | public string $path = ''; |
104 | |
105 | /** |
106 | * Virtual path. |
107 | * |
108 | * @var string |
109 | * @since 1.0.0 |
110 | */ |
111 | public string $virtualPath = '/'; |
112 | |
113 | /** |
114 | * Is path absolute? |
115 | * |
116 | * @var bool |
117 | * @since 1.0.0 |
118 | */ |
119 | public bool $isAbsolute = false; |
120 | |
121 | /** |
122 | * Is versioned. |
123 | * |
124 | * @var bool |
125 | * @since 1.0.0 |
126 | */ |
127 | public bool $isVersioned = false; |
128 | |
129 | /** |
130 | * Media Description. |
131 | * |
132 | * @var string |
133 | * @since 1.0.0 |
134 | */ |
135 | public string $description = ''; |
136 | |
137 | /** |
138 | * Media Description. |
139 | * |
140 | * @var string |
141 | * @since 1.0.0 |
142 | */ |
143 | public string $descriptionRaw = ''; |
144 | |
145 | /** |
146 | * Resource id. |
147 | * |
148 | * @var null|Media |
149 | * @since 1.0.0 |
150 | */ |
151 | public ?Media $source = null; |
152 | |
153 | /** |
154 | * Is encrypted. |
155 | * |
156 | * @var bool |
157 | * @since 1.0.0 |
158 | */ |
159 | public bool $isEncrypted = false; |
160 | |
161 | /** |
162 | * Media password hash. |
163 | * |
164 | * @var null|string |
165 | * @since 1.0.0 |
166 | */ |
167 | public ?string $password = null; |
168 | |
169 | /** |
170 | * Media is hidden. |
171 | * |
172 | * @var int |
173 | * @since 1.0.0 |
174 | */ |
175 | public int $status = MediaStatus::NORMAL; |
176 | |
177 | /** |
178 | * Media class. |
179 | * |
180 | * @var int |
181 | * @since 1.0.0 |
182 | */ |
183 | public int $class = MediaClass::FILE; |
184 | |
185 | /** |
186 | * Unit |
187 | * |
188 | * @var null|int |
189 | * @since 1.0.0 |
190 | */ |
191 | public ?int $unit = null; |
192 | |
193 | /** |
194 | * Tags. |
195 | * |
196 | * @var Tag[] |
197 | * @since 1.0.0 |
198 | */ |
199 | public array $tags = []; |
200 | |
201 | /** |
202 | * Language. |
203 | * |
204 | * @var null|string |
205 | * @since 1.0.0 |
206 | */ |
207 | public ?string $language = null; |
208 | |
209 | /** |
210 | * Country. |
211 | * |
212 | * @var null|string |
213 | * @since 1.0.0 |
214 | */ |
215 | public ?string $country = null; |
216 | |
217 | /** |
218 | * Constructor. |
219 | * |
220 | * @since 1.0.0 |
221 | */ |
222 | public function __construct() |
223 | { |
224 | $this->createdBy = new NullAccount(); |
225 | $this->createdAt = new \DateTimeImmutable(); |
226 | } |
227 | |
228 | /** |
229 | * @return int |
230 | * |
231 | * @since 1.0.0 |
232 | */ |
233 | public function getId() : int |
234 | { |
235 | return $this->id; |
236 | } |
237 | |
238 | /** |
239 | * Encrypt the media file |
240 | * |
241 | * @param string $key Password to encrypt the file with |
242 | * @param null|string $outputPath Output path of the encryption (null = replace file) |
243 | * |
244 | * @return bool |
245 | * |
246 | * @since 1.0.0 |
247 | */ |
248 | public function encrypt(string $key, string $outputPath = null) : bool |
249 | { |
250 | return EncryptionHelper::encryptFile($this->getAbsolutePath(), $outputPath ?? $this->getAbsolutePath(), $key); |
251 | } |
252 | |
253 | /** |
254 | * Decrypt the media file |
255 | * |
256 | * @param string $key Password to encrypt the file with |
257 | * @param null|string $outputPath Output path of the encryption (null = replace file) |
258 | * |
259 | * @return bool |
260 | * |
261 | * @since 1.0.0 |
262 | */ |
263 | public function decrypt(string $key, string $outputPath = null) : bool |
264 | { |
265 | return EncryptionHelper::decryptFile($this->getAbsolutePath(), $outputPath ?? $this->getAbsolutePath(), $key); |
266 | } |
267 | |
268 | /** |
269 | * Has password defined |
270 | * |
271 | * @return bool |
272 | * |
273 | * @since 1.0.0 |
274 | */ |
275 | public function hasPassword() : bool |
276 | { |
277 | return !empty($this->password); |
278 | } |
279 | |
280 | /** |
281 | * Set encryption password |
282 | * |
283 | * @param null|string $password Password |
284 | * |
285 | * @return void |
286 | * |
287 | * @since 1.0.0 |
288 | */ |
289 | public function setPassword(?string $password) : void |
290 | { |
291 | $temp = empty($password) ? null : \password_hash($password, \PASSWORD_BCRYPT); |
292 | |
293 | $this->password = $temp === false ? null : $temp; |
294 | } |
295 | |
296 | /** |
297 | * Compare user password with password of the media file |
298 | * |
299 | * @param string $password User password |
300 | * |
301 | * @return bool |
302 | * |
303 | * @since 1.0.0 |
304 | */ |
305 | public function comparePassword(string $password) : bool |
306 | { |
307 | return \password_verify($password, $this->password ?? ''); |
308 | } |
309 | |
310 | /** |
311 | * Get the media path |
312 | * |
313 | * @return string |
314 | * |
315 | * @since 1.0.0 |
316 | */ |
317 | public function getPath() : string |
318 | { |
319 | return $this->isAbsolute ? $this->path : \ltrim($this->path, '\\/'); |
320 | } |
321 | |
322 | /** |
323 | * Get the media path |
324 | * |
325 | * @return string |
326 | * |
327 | * @since 1.0.0 |
328 | */ |
329 | public function getFileName() : string |
330 | { |
331 | return \basename($this->path); |
332 | } |
333 | |
334 | /** |
335 | * Get the media path |
336 | * |
337 | * @return string |
338 | * |
339 | * @since 1.0.0 |
340 | */ |
341 | public function getExtension() : string |
342 | { |
343 | $pos = \strrpos('.', $this->path); |
344 | |
345 | if ($pos === false) { |
346 | return ''; |
347 | } |
348 | |
349 | return \substr($this->path, $pos + 1); |
350 | } |
351 | |
352 | /** |
353 | * Get the absolute media path |
354 | * |
355 | * @return string |
356 | * |
357 | * @since 1.0.0 |
358 | */ |
359 | public function getAbsolutePath() : string |
360 | { |
361 | return $this->isAbsolute ? $this->path : __DIR__ . '/../../../' . \ltrim($this->path, '\\/'); |
362 | } |
363 | |
364 | /** |
365 | * @return string |
366 | * |
367 | * @since 1.0.0 |
368 | */ |
369 | public function getVirtualPath() : string |
370 | { |
371 | return $this->virtualPath; |
372 | } |
373 | |
374 | /** |
375 | * @param string $path $filepath |
376 | * |
377 | * @return void |
378 | * |
379 | * @since 1.0.0 |
380 | */ |
381 | public function setPath(string $path) : void |
382 | { |
383 | $this->path = \strtr($path, '\\', '/'); |
384 | } |
385 | |
386 | /** |
387 | * @param string $path $filepath |
388 | * |
389 | * @return void |
390 | * |
391 | * @since 1.0.0 |
392 | */ |
393 | public function setVirtualPath(string $path) : void |
394 | { |
395 | $this->virtualPath = \strtr($path, '\\', '/'); |
396 | } |
397 | |
398 | /** |
399 | * Adding new type. |
400 | * |
401 | * @param MediaType $type MediaType |
402 | * |
403 | * @return int |
404 | * |
405 | * @since 1.0.0 |
406 | */ |
407 | public function addMediaType(MediaType $type) : int |
408 | { |
409 | $this->types[] = $type; |
410 | |
411 | \end($this->types); |
412 | $key = (int) \key($this->types); |
413 | \reset($this->types); |
414 | |
415 | return $key; |
416 | } |
417 | |
418 | /** |
419 | * Remove MediaType from list. |
420 | * |
421 | * @param int $id MediaType |
422 | * |
423 | * @return bool |
424 | * |
425 | * @since 1.0.0 |
426 | */ |
427 | public function removeMediaType($id) : bool |
428 | { |
429 | if (isset($this->types[$id])) { |
430 | unset($this->types[$id]); |
431 | |
432 | return true; |
433 | } |
434 | |
435 | return false; |
436 | } |
437 | |
438 | /** |
439 | * Get media types. |
440 | * |
441 | * @return MediaType[] |
442 | * |
443 | * @since 1.0.0 |
444 | */ |
445 | public function getMediaTypes() : array |
446 | { |
447 | return $this->types; |
448 | } |
449 | |
450 | /** |
451 | * Get media type. |
452 | * |
453 | * @param int $id Element id |
454 | * |
455 | * @return MediaType |
456 | * |
457 | * @since 1.0.0 |
458 | */ |
459 | public function getMediaType(int $id) : MediaType |
460 | { |
461 | return $this->types[$id] ?? new NullMediaType(); |
462 | } |
463 | |
464 | /** |
465 | * Get media type by name |
466 | * |
467 | * @param string $name Type name |
468 | * |
469 | * @return MediaType |
470 | * |
471 | * @since 1.0.0 |
472 | */ |
473 | public function getMediaTypeName(string $name) : MediaType |
474 | { |
475 | foreach ($this->types as $type) { |
476 | if ($type->name === $name) { |
477 | return $type; |
478 | } |
479 | } |
480 | |
481 | return new NullMediaType(); |
482 | } |
483 | |
484 | /** |
485 | * Has media type by id |
486 | * |
487 | * @param int $id Media type id |
488 | * |
489 | * @return bool |
490 | * |
491 | * @since 1.0.0 |
492 | */ |
493 | public function hasMediaTypeId(int $id) : bool |
494 | { |
495 | return isset($this->types[$id]); |
496 | } |
497 | |
498 | /** |
499 | * Has media type by name |
500 | * |
501 | * @param string $name Media type name |
502 | * |
503 | * @return bool |
504 | * |
505 | * @since 1.0.0 |
506 | */ |
507 | public function hasMediaTypeName(string $name) : bool |
508 | { |
509 | foreach ($this->types as $type) { |
510 | if ($type->name === $name) { |
511 | return true; |
512 | } |
513 | } |
514 | |
515 | return false; |
516 | } |
517 | |
518 | /** |
519 | * Adding new tag. |
520 | * |
521 | * @param Tag $tag Tag |
522 | * |
523 | * @return int |
524 | * |
525 | * @since 1.0.0 |
526 | */ |
527 | public function addTag(Tag $tag) : int |
528 | { |
529 | $this->tags[] = $tag; |
530 | |
531 | \end($this->tags); |
532 | $key = (int) \key($this->tags); |
533 | \reset($this->tags); |
534 | |
535 | return $key; |
536 | } |
537 | |
538 | /** |
539 | * Remove Tag from list. |
540 | * |
541 | * @param int $id Tag |
542 | * |
543 | * @return bool |
544 | * |
545 | * @since 1.0.0 |
546 | */ |
547 | public function removeTag($id) : bool |
548 | { |
549 | if (isset($this->tags[$id])) { |
550 | unset($this->tags[$id]); |
551 | |
552 | return true; |
553 | } |
554 | |
555 | return false; |
556 | } |
557 | |
558 | /** |
559 | * Get tags. |
560 | * |
561 | * @return Tag[] |
562 | * |
563 | * @since 1.0.0 |
564 | */ |
565 | public function getTags() : array |
566 | { |
567 | return $this->tags; |
568 | } |
569 | |
570 | /** |
571 | * Get tag. |
572 | * |
573 | * @param int $id Element id |
574 | * |
575 | * @return Tag |
576 | * |
577 | * @since 1.0.0 |
578 | */ |
579 | public function getTag(int $id) : Tag |
580 | { |
581 | return $this->tags[$id] ?? new NullTag(); |
582 | } |
583 | |
584 | /** |
585 | * {@inheritdoc} |
586 | */ |
587 | public function toArray() : array |
588 | { |
589 | return [ |
590 | 'id' => $this->id, |
591 | 'name' => $this->name, |
592 | 'description' => $this->description, |
593 | 'descriptionRaw' => $this->descriptionRaw, |
594 | 'extension' => $this->extension, |
595 | 'virtualpath' => $this->virtualPath, |
596 | 'size' => $this->size, |
597 | 'status' => $this->status, |
598 | 'path' => $this->path, |
599 | 'absolute' => $this->isAbsolute, |
600 | 'createdBy' => $this->createdBy, |
601 | 'createdAt' => $this->createdAt, |
602 | ]; |
603 | } |
604 | |
605 | /** |
606 | * {@inheritdoc} |
607 | */ |
608 | public function jsonSerialize() : mixed |
609 | { |
610 | return $this->toArray(); |
611 | } |
612 | } |