MediaObject::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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