Media   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 22

15 Methods

Rating   Name   Duplication   Size   Complexity  
A metadata() 0 3 1
A matchesInfo() 0 3 1
A info() 0 3 1
A uploaded() 0 3 2
A createdAt() 0 3 1
A updatedAt() 0 3 1
A createdById() 0 3 2
A type() 0 3 1
A updatedById() 0 3 2
A __construct() 0 9 2
A upload() 0 12 3
A name() 0 3 1
A id() 0 3 1
A file() 0 7 2
A status() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Media\Domain\Model;
6
7
use Damax\Common\Domain\Model\Metadata;
8
use Damax\Media\Domain\Exception\InvalidFile;
9
use DateTimeImmutable;
10
use DateTimeInterface;
11
12
class Media
13
{
14
    private const STATUS_PENDING = 'pending';
15
    private const STATUS_UPLOADED = 'uploaded';
16
17
    private $id;
18
    private $status = self::STATUS_PENDING;
19
    private $type;
20
    private $name;
21
    private $mimeType;
22
    private $fileSize;
23
    private $fileKey;
24
    private $storage;
25
    private $metadata = [];
26
    private $createdAt;
27
    private $updatedAt;
28
    private $createdById;
29
    private $updatedById;
30
31
    public function __construct(MediaId $id, string $type, string $name, FileInfo $info, UserId $userId = null)
32
    {
33
        $this->id = (string) $id;
34
        $this->type = $type;
35
        $this->name = $name;
36
        $this->mimeType = $info->mimeType();
37
        $this->fileSize = $info->fileSize();
38
        $this->createdAt = $this->updatedAt = new DateTimeImmutable();
39
        $this->createdById = $this->updatedById = $userId ? (string) $userId : null;
40
    }
41
42
    public function id(): MediaId
43
    {
44
        return MediaId::fromString((string) $this->id);
45
    }
46
47
    public function status(): string
48
    {
49
        return $this->status;
50
    }
51
52
    public function type(): string
53
    {
54
        return $this->type;
55
    }
56
57
    public function name(): string
58
    {
59
        return $this->name;
60
    }
61
62
    public function info(): FileInfo
63
    {
64
        return new FileInfo($this->mimeType, $this->fileSize);
65
    }
66
67
    public function matchesInfo(FileInfo $info): bool
68
    {
69
        return $this->info()->sameAs($info);
70
    }
71
72
    public function uploaded(): bool
73
    {
74
        return $this->fileKey && $this->storage;
75
    }
76
77
    /**
78
     * @throws InvalidFile
79
     */
80
    public function file(): File
81
    {
82
        if (!$this->uploaded()) {
83
            throw InvalidFile::notUploaded();
84
        }
85
86
        return new File($this->fileKey, $this->storage, $this->info());
87
    }
88
89
    public function metadata(): Metadata
90
    {
91
        return Metadata::fromArray($this->metadata);
92
    }
93
94
    public function createdAt(): DateTimeInterface
95
    {
96
        return $this->createdAt;
97
    }
98
99
    public function updatedAt(): DateTimeInterface
100
    {
101
        return $this->updatedAt;
102
    }
103
104
    public function createdById(): ?UserId
105
    {
106
        return $this->createdById ? UserId::fromString((string) $this->createdById) : null;
107
    }
108
109
    public function updatedById(): ?UserId
110
    {
111
        return $this->updatedById ? UserId::fromString((string) $this->updatedById) : null;
112
    }
113
114
    /**
115
     * @throws InvalidFile
116
     */
117
    public function upload(File $file, Metadata $metadata, UserId $uploaderId = null)
118
    {
119
        if (!$this->matchesInfo($file->info())) {
120
            throw InvalidFile::unmatchedInfo();
121
        }
122
123
        $this->status = self::STATUS_UPLOADED;
124
        $this->fileKey = $file->key();
125
        $this->storage = $file->storage();
126
        $this->metadata = $metadata->all();
127
        $this->updatedAt = new DateTimeImmutable();
128
        $this->updatedById = $uploaderId ? (string) $uploaderId : $this->updatedById;
129
    }
130
}
131