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