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\Application\Command\Upload; |
14
|
|
|
|
15
|
|
|
use BenGorFile\File\Domain\Model\FileMimeTypeDoesNotSupportException; |
16
|
|
|
use BenGorFile\File\Domain\Model\FileNameInvalidException; |
17
|
|
|
use Ramsey\Uuid\Uuid; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Upload file by hashing command class. |
21
|
|
|
* |
22
|
|
|
* @author Beñat Espiña <[email protected]> |
23
|
|
|
* @author Gorka Laucirica <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ByHashUploadFileCommand |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The file id. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $id; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The file name. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $name; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The real content of file. |
43
|
|
|
* |
44
|
|
|
* @var mixed |
45
|
|
|
*/ |
46
|
|
|
private $uploadedFile; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The file mime type. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $mimeType; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor. |
57
|
|
|
* |
58
|
|
|
* @param string $aName The file name |
59
|
|
|
* @param mixed $anUploadedFile The real content of file |
60
|
|
|
* @param string $aMimeType The file mime type |
61
|
|
|
* @param string|null $anId The file id |
62
|
|
|
* |
63
|
|
|
* @throws FileNameInvalidException when the mime type given is null |
64
|
|
|
* @throws FileMimeTypeDoesNotSupportException when the name given is null |
65
|
|
|
*/ |
66
|
|
|
public function __construct($aName, $anUploadedFile, $aMimeType, $anId = null) |
67
|
|
|
{ |
68
|
|
|
if (null === $aName) { |
69
|
|
|
throw new FileNameInvalidException(); |
70
|
|
|
} |
71
|
|
|
if (null === $anUploadedFile) { |
72
|
|
|
throw new \InvalidArgumentException('The file content cannot be null'); |
73
|
|
|
} |
74
|
|
|
if (null === $aMimeType) { |
75
|
|
|
throw new FileMimeTypeDoesNotSupportException(); |
76
|
|
|
} |
77
|
|
|
$this->id = null === $anId ? Uuid::uuid4()->toString() : $anId; |
78
|
|
|
$this->name = $aName; |
79
|
|
|
$this->uploadedFile = $anUploadedFile; |
80
|
|
|
$this->mimeType = $aMimeType; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the file id. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function id() |
89
|
|
|
{ |
90
|
|
|
return $this->id; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the file name. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function name() |
99
|
|
|
{ |
100
|
|
|
return $this->name; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets the mime type. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function mimeType() |
109
|
|
|
{ |
110
|
|
|
return $this->mimeType; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Gets the real content of file. |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function uploadedFile() |
119
|
|
|
{ |
120
|
|
|
return $this->uploadedFile; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|