MediaObject   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 49
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A convertSizeToString() 0 15 4
A getFormattedFileSize() 0 3 2
A getId() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Model\Uploadable;
15
16
use Ramsey\Uuid\Uuid;
17
18
/**
19
 * @author Daniel West <[email protected]>
20
 */
21
final class MediaObject
22
{
23
    private string $id;
24
25
    public string $contentUrl;
26
27
    public int $fileSize;
28
29
    public string $mimeType;
30
31
    public ?int $width = null;
32
33
    public ?int $height = null;
34
35
    public ?string $imagineFilter = null;
36
37
    // defined otherwise the IRI mapping in API Platform does not work with just the getter method
38
    private ?string $formattedFileSize = null;
39
40
    public function __construct()
41
    {
42
        $this->id = Uuid::uuid4()->getHex()->toString();
43
    }
44
45
    public function getId(): string
46
    {
47
        return $this->id;
48
    }
49
50
    public function getFormattedFileSize(): string
51
    {
52
        return $this->formattedFileSize ?? $this->fileSize < 0 ? '' : $this->convertSizeToString($this->fileSize);
53
    }
54
55
    private function convertSizeToString(int $bytes): string
56
    {
57
        if ($bytes >= 1073741824) {
58
            return number_format($bytes / 1073741824, 1) . 'GB';
59
        }
60
61
        if ($bytes >= 1048576) {
62
            return number_format($bytes / 1048576, 1) . 'MB';
63
        }
64
65
        if ($bytes >= 1024) {
66
            return number_format($bytes / 1024, 1) . 'KB';
67
        }
68
69
        return $bytes . 'B';
70
    }
71
}
72