Passed
Pull Request — master (#631)
by ANTHONIUS
08:16
created

FileTrait::getLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Core\Entity;
6
7
use DateTimeInterface;
8
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
9
use MongoDB\BSON\ObjectId;
10
11
trait FileTrait
12
{
13
    /**
14
     * @ODM\Id()
15
     */
16
    protected ?string $id = null;
17
18
    /**
19
     * @ODM\File\Filename()
20
     */
21
    protected ?string $name = null;
22
23
    /**
24
     * @ODM\File\UploadDate(type="tz_date")
25
     */
26
    protected ?DateTimeInterface $uploadDate = null;
27
28
    /**
29
     * @ODM\File\Length
30
     */
31
    protected ?int $length = null;
32
33
    /**
34
     * @ODM\File\ChunkSize
35
     */
36
    protected ?int $chunkSize = null;
37
38
    /**
39
     * @return string|null
40
     */
41
    public function getId(): ?string
42
    {
43
        return $this->id;
44
    }
45
46
    public function setId(string $id)
47
    {
48
        $this->id = $id;
49
        return $this;
50
    }
51
52
    /**
53
     * @return string|null
54
     */
55
    public function getName(): ?string
56
    {
57
        return $this->name;
58
    }
59
60
    public function setName(string $name)
61
    {
62
        $this->name = $name;
63
        return $this;
64
    }
65
66
    public function getUploadDate(): ?DateTimeInterface
67
    {
68
        return $this->uploadDate;
69
    }
70
71
    /**
72
     * @return int|null
73
     */
74
    public function getLength(): ?int
75
    {
76
        return $this->length;
77
    }
78
79
    /**
80
     * @return int|null
81
     */
82
    public function getChunkSize(): ?int
83
    {
84
        return $this->chunkSize;
85
    }
86
87
    /**
88
     * Gets the length of file in GB, MB ot kB format
89
     *
90
     * @return string
91
     */
92
    public function getPrettySize(): string
93
    {
94
        $size = $this->getLength();
95
96
        if ($size >= 1073741824) {
97
            return round($size / 1073741824, 2) . ' GB';
98
        }
99
100
        if ($size >= 1048576) {
101
            return round($size / 1048576, 2) . ' MB';
102
        }
103
104
        if ($size >= 1024) {
105
            return round($size / 1024, 2) . ' kB';
106
        }
107
108
        return (string)$size;
109
    }
110
}