1
|
|
|
<?php namespace Nord\Lumen\FileManager\Doctrine\ORM; |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use Doctrine\ORM\Mapping as ORM; |
5
|
|
|
use Nord\Lumen\FileManager\Contracts\File as FileContract; |
6
|
|
|
use Nord\Lumen\FileManager\Facades\FileManager; |
7
|
|
|
|
8
|
|
|
class File implements FileContract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @ORM\Id |
12
|
|
|
* @ORM\GeneratedValue |
13
|
|
|
* @ORM\Column(type="integer", name="id") |
14
|
|
|
* |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
private $autoIncrementId; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $id; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $extension; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $path; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $mimeType; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
private $byteSize; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $data; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $disk; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Carbon |
61
|
|
|
*/ |
62
|
|
|
private $savedAt; |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* File constructor. |
67
|
|
|
* |
68
|
|
|
* @param string $id |
69
|
|
|
* @param string $name |
70
|
|
|
* @param string $extension |
71
|
|
|
* @param string $path |
72
|
|
|
* @param string $mimeType |
73
|
|
|
* @param int $byteSize |
74
|
|
|
* @param array $data |
75
|
|
|
* @param string $disk |
76
|
|
|
* @param Carbon $savedAt |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
79
|
|
|
$id, |
80
|
|
|
$name, |
81
|
|
|
$extension, |
82
|
|
|
$path, |
83
|
|
|
$mimeType, |
84
|
|
|
$byteSize, |
85
|
|
|
array $data, |
86
|
|
|
$disk, |
87
|
|
|
Carbon $savedAt |
88
|
|
|
) { |
89
|
|
|
$this->setId($id); |
90
|
|
|
$this->setName($name); |
91
|
|
|
$this->setExtension($extension); |
92
|
|
|
$this->setPath($path); |
93
|
|
|
$this->setMimeType($mimeType); |
94
|
|
|
$this->setByteSize($byteSize); |
95
|
|
|
$this->setData($data); |
96
|
|
|
$this->setDisk($disk); |
97
|
|
|
$this->setSavedAt($savedAt); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getAutoIncrementId() |
104
|
|
|
{ |
105
|
|
|
return $this->autoIncrementId; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getId() |
113
|
|
|
{ |
114
|
|
|
return $this->id; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getName() |
122
|
|
|
{ |
123
|
|
|
return $this->name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getExtension() |
130
|
|
|
{ |
131
|
|
|
return $this->extension; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getMimeType() |
139
|
|
|
{ |
140
|
|
|
return $this->mimeType; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return int |
146
|
|
|
*/ |
147
|
|
|
public function getByteSize() |
148
|
|
|
{ |
149
|
|
|
return $this->byteSize; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function getData() |
157
|
|
|
{ |
158
|
|
|
return $this->data; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getDisk() |
166
|
|
|
{ |
167
|
|
|
return $this->disk; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return Carbon |
173
|
|
|
*/ |
174
|
|
|
public function getSavedAt() |
175
|
|
|
{ |
176
|
|
|
return $this->savedAt; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function getSavedAtAsTimestamp() |
184
|
|
|
{ |
185
|
|
|
return $this->getSavedAt()->getTimestamp(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getFilename() |
193
|
|
|
{ |
194
|
|
|
return $this->name . '-' . $this->id . '.' . $this->extension; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @inheritdoc |
200
|
|
|
*/ |
201
|
|
|
public function getFilePath() |
202
|
|
|
{ |
203
|
|
|
return $this->getPath() . $this->getFilename(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @inheritdoc |
209
|
|
|
*/ |
210
|
|
|
public function getUrl(array $options = []) |
211
|
|
|
{ |
212
|
|
|
return FileManager::getFileUrl($this, $options); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $id |
218
|
|
|
* |
219
|
|
|
* @throws \Exception |
220
|
|
|
*/ |
221
|
|
|
private function setId($id) |
222
|
|
|
{ |
223
|
|
|
if (empty($id)) { |
224
|
|
|
throw new \Exception('File ID cannot be empty.'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$this->id = $id; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param string $name |
233
|
|
|
* |
234
|
|
|
* @throws \Exception |
235
|
|
|
*/ |
236
|
|
|
private function setName($name) |
237
|
|
|
{ |
238
|
|
|
if (empty($name)) { |
239
|
|
|
throw new \Exception('File name cannot be empty.'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$this->name = $name; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string $extension |
248
|
|
|
* |
249
|
|
|
* @throws \Exception |
250
|
|
|
*/ |
251
|
|
|
private function setExtension($extension) |
252
|
|
|
{ |
253
|
|
|
if (empty($extension)) { |
254
|
|
|
throw new \Exception('File extension cannot be empty.'); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$this->extension = $extension; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $path |
263
|
|
|
*/ |
264
|
|
|
private function setPath($path) |
265
|
|
|
{ |
266
|
|
|
$this->path = $path; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string $mimeType |
272
|
|
|
*/ |
273
|
|
|
private function setMimeType($mimeType) |
274
|
|
|
{ |
275
|
|
|
$this->mimeType = $mimeType; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param int $byteSize |
281
|
|
|
*/ |
282
|
|
|
private function setByteSize($byteSize) |
283
|
|
|
{ |
284
|
|
|
$this->byteSize = $byteSize; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param array $data |
290
|
|
|
*/ |
291
|
|
|
private function setData(array $data) |
292
|
|
|
{ |
293
|
|
|
$this->data = $data; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param string $storage |
299
|
|
|
*/ |
300
|
|
|
private function setDisk($storage) |
301
|
|
|
{ |
302
|
|
|
$this->disk = $storage; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param Carbon $savedAt |
308
|
|
|
*/ |
309
|
|
|
private function setSavedAt(Carbon $savedAt) |
310
|
|
|
{ |
311
|
|
|
$this->savedAt = $savedAt; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
|
private function getPath() |
319
|
|
|
{ |
320
|
|
|
return isset($this->path) ? $this->path . '/' : ''; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.