Completed
Push — master ( fefec9...56b8a6 )
by Beñat
10s
created

File   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 6
Bugs 0 Features 5
Metric Value
wmc 14
c 6
b 0
f 5
lcom 1
cbo 9
dl 0
loc 185
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A id() 0 4 1
A createdOn() 0 4 1
A mimeType() 0 4 1
A name() 0 4 1
A overwrite() 0 8 1
A remove() 0 4 1
A rename() 0 11 2
A updatedOn() 0 4 1
A __toString() 0 4 1
A availableMimeTypes() 0 4 1
A setMimeType() 0 7 2
1
<?php
2
3
/*
4
 * This file is part of the BenGorFile package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorFile\File\Domain\Model;
14
15
/**
16
 * File domain class.
17
 *
18
 * @author Beñat Espiña <[email protected]>
19
 * @author Gorka Laucirica <[email protected]>
20
 */
21
class File extends FileAggregateRoot
22
{
23
    /**
24
     * The id.
25
     *
26
     * @var FileId
27
     */
28
    protected $id;
29
30
    /**
31
     * Created on.
32
     *
33
     * @var \DateTimeImmutable
34
     */
35
    protected $createdOn;
36
37
    /**
38
     * The file mime type.
39
     *
40
     * @var FileMimeType
41
     */
42
    protected $mimeType;
43
44
    /**
45
     * The file name.
46
     *
47
     * @var FileName
48
     */
49
    protected $name;
50
51
    /**
52
     * Updated on.
53
     *
54
     * @var \DateTimeImmutable
55
     */
56
    protected $updatedOn;
57
58
    /**
59
     * Constructor.
60
     *
61
     * @param FileId       $anId      The id
62
     * @param FileName     $aName     The file name
63
     * @param FileMimeType $aMimeType The file mime type
64
     */
65
    public function __construct(FileId $anId, FileName $aName, FileMimeType $aMimeType)
66
    {
67
        $this->id = $anId;
68
        $this->name = $aName;
69
        $this->createdOn = new \DateTimeImmutable();
70
        $this->updatedOn = new \DateTimeImmutable();
71
        $this->setMimeType($aMimeType);
72
73
        $this->publish(new FileUploaded($this->id()));
74
    }
75
76
    /**
77
     * Gets the id.
78
     *
79
     * @return FileId
80
     */
81
    public function id()
82
    {
83
        return $this->id;
84
    }
85
86
    /**
87
     * Gets the created on.
88
     *
89
     * @return \DateTimeImmutable
90
     */
91
    public function createdOn()
92
    {
93
        return $this->createdOn;
94
    }
95
96
    /**
97
     * Gets the file mime type.
98
     *
99
     * @return FileMimeType
100
     */
101
    public function mimeType()
102
    {
103
        return $this->mimeType;
104
    }
105
106
    /**
107
     * Gets the file name.
108
     *
109
     * @return FileName
110
     */
111
    public function name()
112
    {
113
        return $this->name;
114
    }
115
116
    /**
117
     * Overwrites the file.
118
     *
119
     * @param FileName     $aName     The file name
120
     * @param FileMimeType $aMimeType The file mime type
121
     */
122
    public function overwrite(FileName $aName, FileMimeType $aMimeType)
123
    {
124
        $this->name = $aName;
125
        $this->mimeType = $aMimeType;
126
        $this->updatedOn = new \DateTimeImmutable();
127
128
        $this->publish(new FileOverwritten($this->id()));
129
    }
130
131
    /**
132
     * Removes the file.
133
     */
134
    public function remove()
135
    {
136
        $this->publish(new FileRemoved($this->id()));
137
    }
138
139
    /**
140
     * Renames the file.
141
     *
142
     * @param FileName $aName The file name
143
     *
144
     * @throws FileNameException when the file name extension is different of the current
145
     */
146
    public function rename(FileName $aName)
147
    {
148
        if ($aName->extension() !== $this->name()->extension()) {
149
            throw FileNameException::invalidName($aName);
150
        }
151
152
        $this->name = $aName;
153
        $this->updatedOn = new \DateTimeImmutable();
154
155
        $this->publish(new FileRenamed($this->id()));
156
    }
157
158
    /**
159
     * Gets the updated on.
160
     *
161
     * @return \DateTimeImmutable
162
     */
163
    public function updatedOn()
164
    {
165
        return $this->updatedOn;
166
    }
167
168
    /**
169
     * Magic method that represents the file domain object in string format.
170
     *
171
     * @return string
172
     */
173
    public function __toString()
174
    {
175
        return $this->name->filename();
176
    }
177
178
    /**
179
     * Gets the available mime types in scalar type.
180
     *
181
     * This method is an extension point that it allows
182
     * to add more mime types easily in the domain.
183
     *
184
     * @return array
185
     */
186
    protected function availableMimeTypes()
187
    {
188
        return FileMimeType::mimeTypes();
189
    }
190
191
    /**
192
     * Sets the given mime type in the file scope.
193
     *
194
     * @param FileMimeType $aMimeType The mime type
195
     *
196
     * @throws FileMimeTypeException when the given mime type is not support for the file
197
     */
198
    private function setMimeType(FileMimeType $aMimeType)
199
    {
200
        if (!in_array($aMimeType->mimeType(), $this->availableMimeTypes(), true)) {
201
            throw FileMimeTypeException::doesNotSupport($aMimeType->mimeType());
202
        }
203
        $this->mimeType = $aMimeType;
204
    }
205
}
206