| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use InvalidArgumentException; |
| 6 | 7 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| @@ -7,18 +8,15 @@ | ||
| 7 | 8 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\UploadedFileInterface; |
| 8 | 9 | use RuntimeException; |
| 9 | 10 | class UploadedFile implements UploadedFileInterface |
| 10 | 11 | { |
| 12 | + private const ERROR_MAP = [\UPLOAD_ERR_OK => 'UPLOAD_ERR_OK', \UPLOAD_ERR_INI_SIZE => 'UPLOAD_ERR_INI_SIZE', \UPLOAD_ERR_FORM_SIZE => 'UPLOAD_ERR_FORM_SIZE', \UPLOAD_ERR_PARTIAL => 'UPLOAD_ERR_PARTIAL', \UPLOAD_ERR_NO_FILE => 'UPLOAD_ERR_NO_FILE', \UPLOAD_ERR_NO_TMP_DIR => 'UPLOAD_ERR_NO_TMP_DIR', \UPLOAD_ERR_CANT_WRITE => 'UPLOAD_ERR_CANT_WRITE', \UPLOAD_ERR_EXTENSION => 'UPLOAD_ERR_EXTENSION']; | |
| 11 | 13 | /** |
| 12 | - * @var int[] | |
| 14 | + * @var string|null | |
| 13 | 15 | */ |
| 14 | - private static $errors = [\UPLOAD_ERR_OK, \UPLOAD_ERR_INI_SIZE, \UPLOAD_ERR_FORM_SIZE, \UPLOAD_ERR_PARTIAL, \UPLOAD_ERR_NO_FILE, \UPLOAD_ERR_NO_TMP_DIR, \UPLOAD_ERR_CANT_WRITE, \UPLOAD_ERR_EXTENSION]; | |
| 15 | - /** | |
| 16 | - * @var string | |
| 17 | - */ | |
| 18 | 16 | private $clientFilename; |
| 19 | 17 | /** |
| 20 | - * @var string | |
| 18 | + * @var string|null | |
| 21 | 19 | */ |
| 22 | 20 | private $clientMediaType; |
| 23 | 21 | /** |
| 24 | 22 | * @var int |
| @@ -32,9 +30,9 @@ | ||
| 32 | 30 | * @var bool |
| 33 | 31 | */ |
| 34 | 32 | private $moved = \false; |
| 35 | 33 | /** |
| 36 | - * @var int | |
| 34 | + * @var int|null | |
| 37 | 35 | */ |
| 38 | 36 | private $size; |
| 39 | 37 | /** |
| 40 | 38 | * @var StreamInterface|null |
| @@ -41,19 +39,15 @@ | ||
| 41 | 39 | */ |
| 42 | 40 | private $stream; |
| 43 | 41 | /** |
| 44 | 42 | * @param StreamInterface|string|resource $streamOrFile |
| 45 | - * @param int $size | |
| 46 | - * @param int $errorStatus | |
| 47 | - * @param string|null $clientFilename | |
| 48 | - * @param string|null $clientMediaType | |
| 49 | 43 | */ |
| 50 | - public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null) | |
| 44 | + public function __construct($streamOrFile, ?int $size, int $errorStatus, ?string $clientFilename = null, ?string $clientMediaType = null) | |
| 51 | 45 | { |
| 52 | 46 | $this->setError($errorStatus); |
| 53 | - $this->setSize($size); | |
| 54 | - $this->setClientFilename($clientFilename); | |
| 55 | - $this->setClientMediaType($clientMediaType); | |
| 47 | + $this->size = $size; | |
| 48 | + $this->clientFilename = $clientFilename; | |
| 49 | + $this->clientMediaType = $clientMediaType; | |
| 56 | 50 | if ($this->isOk()) { |
| 57 | 51 | $this->setStreamOrFile($streamOrFile); |
| 58 | 52 | } |
| 59 | 53 | } |
| @@ -59,13 +53,13 @@ | ||
| 59 | 53 | } |
| 60 | 54 | /** |
| 61 | 55 | * Depending on the value set file or stream variable |
| 62 | 56 | * |
| 63 | - * @param mixed $streamOrFile | |
| 57 | + * @param StreamInterface|string|resource $streamOrFile | |
| 64 | 58 | * |
| 65 | 59 | * @throws InvalidArgumentException |
| 66 | 60 | */ |
| 67 | - private function setStreamOrFile($streamOrFile) | |
| 61 | + private function setStreamOrFile($streamOrFile) : void | |
| 68 | 62 | { |
| 69 | 63 | if (\is_string($streamOrFile)) { |
| 70 | 64 | $this->file = $streamOrFile; |
| 71 | 65 | } elseif (\is_resource($streamOrFile)) { |
| @@ -76,89 +70,29 @@ | ||
| 76 | 70 | throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile'); |
| 77 | 71 | } |
| 78 | 72 | } |
| 79 | 73 | /** |
| 80 | - * @param int $error | |
| 81 | - * | |
| 82 | 74 | * @throws InvalidArgumentException |
| 83 | 75 | */ |
| 84 | - private function setError($error) | |
| 76 | + private function setError(int $error) : void | |
| 85 | 77 | { |
| 86 | - if (\false === \is_int($error)) { | |
| 87 | - throw new InvalidArgumentException('Upload file error status must be an integer'); | |
| 88 | - } | |
| 89 | - if (\false === \in_array($error, UploadedFile::$errors)) { | |
| 78 | + if (!isset(UploadedFile::ERROR_MAP[$error])) { | |
| 90 | 79 | throw new InvalidArgumentException('Invalid error status for UploadedFile'); |
| 91 | 80 | } |
| 92 | 81 | $this->error = $error; |
| 93 | 82 | } |
| 94 | - /** | |
| 95 | - * @param int $size | |
| 96 | - * | |
| 97 | - * @throws InvalidArgumentException | |
| 98 | - */ | |
| 99 | - private function setSize($size) | |
| 83 | + private static function isStringNotEmpty($param) : bool | |
| 100 | 84 | { |
| 101 | - if (\false === \is_int($size)) { | |
| 102 | - throw new InvalidArgumentException('Upload file size must be an integer'); | |
| 103 | - } | |
| 104 | - $this->size = $size; | |
| 105 | - } | |
| 106 | - /** | |
| 107 | - * @param mixed $param | |
| 108 | - * | |
| 109 | - * @return bool | |
| 110 | - */ | |
| 111 | - private function isStringOrNull($param) | |
| 112 | - { | |
| 113 | - return \in_array(\gettype($param), ['string', 'NULL']); | |
| 114 | - } | |
| 115 | - /** | |
| 116 | - * @param mixed $param | |
| 117 | - * | |
| 118 | - * @return bool | |
| 119 | - */ | |
| 120 | - private function isStringNotEmpty($param) | |
| 121 | - { | |
| 122 | 85 | return \is_string($param) && \false === empty($param); |
| 123 | 86 | } |
| 124 | 87 | /** |
| 125 | - * @param string|null $clientFilename | |
| 126 | - * | |
| 127 | - * @throws InvalidArgumentException | |
| 128 | - */ | |
| 129 | - private function setClientFilename($clientFilename) | |
| 130 | - { | |
| 131 | - if (\false === $this->isStringOrNull($clientFilename)) { | |
| 132 | - throw new InvalidArgumentException('Upload file client filename must be a string or null'); | |
| 133 | - } | |
| 134 | - $this->clientFilename = $clientFilename; | |
| 135 | - } | |
| 136 | - /** | |
| 137 | - * @param string|null $clientMediaType | |
| 138 | - * | |
| 139 | - * @throws InvalidArgumentException | |
| 140 | - */ | |
| 141 | - private function setClientMediaType($clientMediaType) | |
| 142 | - { | |
| 143 | - if (\false === $this->isStringOrNull($clientMediaType)) { | |
| 144 | - throw new InvalidArgumentException('Upload file client media type must be a string or null'); | |
| 145 | - } | |
| 146 | - $this->clientMediaType = $clientMediaType; | |
| 147 | - } | |
| 148 | - /** | |
| 149 | 88 | * Return true if there is no upload error |
| 150 | - * | |
| 151 | - * @return bool | |
| 152 | 89 | */ |
| 153 | - private function isOk() | |
| 90 | + private function isOk() : bool | |
| 154 | 91 | { |
| 155 | 92 | return $this->error === \UPLOAD_ERR_OK; |
| 156 | 93 | } |
| 157 | - /** | |
| 158 | - * @return bool | |
| 159 | - */ | |
| 160 | - public function isMoved() | |
| 94 | + public function isMoved() : bool | |
| 161 | 95 | { |
| 162 | 96 | return $this->moved; |
| 163 | 97 | } |
| 164 | 98 | /** |
| @@ -163,51 +97,35 @@ | ||
| 163 | 97 | } |
| 164 | 98 | /** |
| 165 | 99 | * @throws RuntimeException if is moved or not ok |
| 166 | 100 | */ |
| 167 | - private function validateActive() | |
| 101 | + private function validateActive() : void | |
| 168 | 102 | { |
| 169 | 103 | if (\false === $this->isOk()) { |
| 170 | - throw new RuntimeException('Cannot retrieve stream due to upload error'); | |
| 104 | + throw new RuntimeException(\sprintf('Cannot retrieve stream due to upload error (%s)', self::ERROR_MAP[$this->error])); | |
| 171 | 105 | } |
| 172 | 106 | if ($this->isMoved()) { |
| 173 | 107 | throw new RuntimeException('Cannot retrieve stream after it has already been moved'); |
| 174 | 108 | } |
| 175 | 109 | } |
| 176 | - /** | |
| 177 | - * {@inheritdoc} | |
| 178 | - * | |
| 179 | - * @throws RuntimeException if the upload was not successful. | |
| 180 | - */ | |
| 181 | - public function getStream() | |
| 110 | + public function getStream() : StreamInterface | |
| 182 | 111 | { |
| 183 | 112 | $this->validateActive(); |
| 184 | 113 | if ($this->stream instanceof StreamInterface) { |
| 185 | 114 | return $this->stream; |
| 186 | 115 | } |
| 187 | - return new LazyOpenStream($this->file, 'r+'); | |
| 116 | + /** @var string $file */ | |
| 117 | + $file = $this->file; | |
| 118 | + return new LazyOpenStream($file, 'r+'); | |
| 188 | 119 | } |
| 189 | - /** | |
| 190 | - * {@inheritdoc} | |
| 191 | - * | |
| 192 | - * @see http://php.net/is_uploaded_file | |
| 193 | - * @see http://php.net/move_uploaded_file | |
| 194 | - * | |
| 195 | - * @param string $targetPath Path to which to move the uploaded file. | |
| 196 | - * | |
| 197 | - * @throws RuntimeException if the upload was not successful. | |
| 198 | - * @throws InvalidArgumentException if the $path specified is invalid. | |
| 199 | - * @throws RuntimeException on any error during the move operation, or on | |
| 200 | - * the second or subsequent call to the method. | |
| 201 | - */ | |
| 202 | - public function moveTo($targetPath) | |
| 120 | + public function moveTo($targetPath) : void | |
| 203 | 121 | { |
| 204 | 122 | $this->validateActive(); |
| 205 | - if (\false === $this->isStringNotEmpty($targetPath)) { | |
| 123 | + if (\false === self::isStringNotEmpty($targetPath)) { | |
| 206 | 124 | throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string'); |
| 207 | 125 | } |
| 208 | 126 | if ($this->file) { |
| 209 | - $this->moved = \php_sapi_name() == 'cli' ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath); | |
| 127 | + $this->moved = \PHP_SAPI === 'cli' ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath); | |
| 210 | 128 | } else { |
| 211 | 129 | Utils::copyToStream($this->getStream(), new LazyOpenStream($targetPath, 'w')); |
| 212 | 130 | $this->moved = \true; |
| 213 | 131 | } |
| @@ -214,42 +132,21 @@ | ||
| 214 | 132 | if (\false === $this->moved) { |
| 215 | 133 | throw new RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath)); |
| 216 | 134 | } |
| 217 | 135 | } |
| 218 | - /** | |
| 219 | - * {@inheritdoc} | |
| 220 | - * | |
| 221 | - * @return int|null The file size in bytes or null if unknown. | |
| 222 | - */ | |
| 223 | - public function getSize() | |
| 136 | + public function getSize() : ?int | |
| 224 | 137 | { |
| 225 | 138 | return $this->size; |
| 226 | 139 | } |
| 227 | - /** | |
| 228 | - * {@inheritdoc} | |
| 229 | - * | |
| 230 | - * @see http://php.net/manual/en/features.file-upload.errors.php | |
| 231 | - * | |
| 232 | - * @return int One of PHP's UPLOAD_ERR_XXX constants. | |
| 233 | - */ | |
| 234 | - public function getError() | |
| 140 | + public function getError() : int | |
| 235 | 141 | { |
| 236 | 142 | return $this->error; |
| 237 | 143 | } |
| 238 | - /** | |
| 239 | - * {@inheritdoc} | |
| 240 | - * | |
| 241 | - * @return string|null The filename sent by the client or null if none | |
| 242 | - * was provided. | |
| 243 | - */ | |
| 244 | - public function getClientFilename() | |
| 144 | + public function getClientFilename() : ?string | |
| 245 | 145 | { |
| 246 | 146 | return $this->clientFilename; |
| 247 | 147 | } |
| 248 | - /** | |
| 249 | - * {@inheritdoc} | |
| 250 | - */ | |
| 251 | - public function getClientMediaType() | |
| 148 | + public function getClientMediaType() : ?string | |
| 252 | 149 | { |
| 253 | 150 | return $this->clientMediaType; |
| 254 | 151 | } |
| 255 | 152 | } |