Passed
Push — main ( 2da435...d93426 )
by Thierry
04:09
created
jaxon-upload/src/Manager/UploadManager.php 2 patches
Switch Indentation   +159 added lines, -159 removed lines patch added patch discarded remove patch
@@ -31,40 +31,40 @@  discard block
 block discarded – undo
31 31
 
32 32
 class UploadManager
33 33
 {
34
-    /**
34
+/**
35 35
      * The id of the upload field in the form
36 36
      *
37 37
      * @var string
38 38
      */
39
-    protected $sUploadFieldId = '';
39
+protected $sUploadFieldId = '';
40 40
 
41
-    /**
41
+/**
42 42
      * A user defined function to transform uploaded file names
43 43
      *
44 44
      * @var Closure
45 45
      */
46
-    protected $cNameSanitizer = null;
46
+protected $cNameSanitizer = null;
47 47
 
48
-    /**
48
+/**
49 49
      * @var array<string, Filesystem>
50 50
      */
51
-    protected $aFilesystems = [];
51
+protected $aFilesystems = [];
52 52
 
53
-    /**
53
+/**
54 54
      * @var array
55 55
      */
56
-    private $errorMessages = [
57
-        0 => 'There is no error, the file uploaded with success',
58
-        1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
59
-        2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
60
-        3 => 'The uploaded file was only partially uploaded',
61
-        4 => 'No file was uploaded',
62
-        6 => 'Missing a temporary folder',
63
-        7 => 'Failed to write file to disk.',
64
-        8 => 'A PHP extension stopped the file upload.',
65
-    ];
56
+private $errorMessages = [
57
+0 => 'There is no error, the file uploaded with success',
58
+1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
59
+2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
60
+3 => 'The uploaded file was only partially uploaded',
61
+4 => 'No file was uploaded',
62
+6 => 'Missing a temporary folder',
63
+7 => 'Failed to write file to disk.',
64
+8 => 'A PHP extension stopped the file upload.',
65
+];
66 66
 
67
-    /**
67
+/**
68 68
      * The constructor
69 69
      *
70 70
      * @param Validator $xValidator
@@ -74,49 +74,49 @@  discard block
 block discarded – undo
74 74
      * @param StorageManager $xStorageManager
75 75
      * @param ConfigManager $xConfigManager
76 76
      */
77
-    public function __construct(private Validator $xValidator, private Translator $xTranslator,
78
-        private LoggerInterface $xLogger, private FileNameInterface $xFileName,
79
-        private StorageManager $xStorageManager, private ConfigManager $xConfigManager)
80
-    {
81
-        // This feature is not yet implemented
82
-        $this->setUploadFieldId('');
83
-    }
77
+public function __construct(private Validator $xValidator, private Translator $xTranslator,
78
+private LoggerInterface $xLogger, private FileNameInterface $xFileName,
79
+private StorageManager $xStorageManager, private ConfigManager $xConfigManager)
80
+{
81
+// This feature is not yet implemented
82
+$this->setUploadFieldId('');
83
+}
84 84
 
85
-    /**
85
+/**
86 86
      * Generate a random name
87 87
      *
88 88
      * @return string
89 89
      */
90
-    protected function randomName(): string
91
-    {
92
-        return $this->xFileName->random(16);
93
-    }
90
+protected function randomName(): string
91
+{
92
+return $this->xFileName->random(16);
93
+}
94 94
 
95
-    /**
95
+/**
96 96
      * Set the id of the upload field in the form
97 97
      *
98 98
      * @param string $sUploadFieldId
99 99
      *
100 100
      * @return void
101 101
      */
102
-    public function setUploadFieldId(string $sUploadFieldId): void
103
-    {
104
-        $this->sUploadFieldId = $sUploadFieldId;
105
-    }
102
+public function setUploadFieldId(string $sUploadFieldId): void
103
+{
104
+$this->sUploadFieldId = $sUploadFieldId;
105
+}
106 106
 
107
-    /**
107
+/**
108 108
      * Filter uploaded file name
109 109
      *
110 110
      * @param Closure $cNameSanitizer    The closure which filters filenames
111 111
      *
112 112
      * @return void
113 113
      */
114
-    public function setNameSanitizer(Closure $cNameSanitizer): void
115
-    {
116
-        $this->cNameSanitizer = $cNameSanitizer;
117
-    }
114
+public function setNameSanitizer(Closure $cNameSanitizer): void
115
+{
116
+$this->cNameSanitizer = $cNameSanitizer;
117
+}
118 118
 
119
-    /**
119
+/**
120 120
      * Make sure the upload dir exists and is writable
121 121
      *
122 122
      * @param Filesystem $xFilesystem
@@ -125,55 +125,55 @@  discard block
 block discarded – undo
125 125
      * @return string
126 126
      * @throws RequestException
127 127
      */
128
-    private function _makeUploadDir(Filesystem $xFilesystem, string $sUploadDir): string
129
-    {
130
-        try
131
-        {
132
-            $xFilesystem->createDirectory($sUploadDir);
133
-            if(!$xFilesystem->directoryExists($sUploadDir))
134
-            {
135
-                throw new RequestException($this->xTranslator->trans('errors.upload.access'));
136
-            }
137
-            return $sUploadDir;
138
-        }
139
-        catch(FilesystemException $e)
140
-        {
141
-            $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
142
-            throw new RequestException($this->xTranslator->trans('errors.upload.access'));
143
-        }
144
-    }
128
+private function _makeUploadDir(Filesystem $xFilesystem, string $sUploadDir): string
129
+{
130
+try
131
+{
132
+$xFilesystem->createDirectory($sUploadDir);
133
+if(!$xFilesystem->directoryExists($sUploadDir))
134
+{
135
+    throw new RequestException($this->xTranslator->trans('errors.upload.access'));
136
+}
137
+return $sUploadDir;
138
+}
139
+catch(FilesystemException $e)
140
+{
141
+$this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
142
+throw new RequestException($this->xTranslator->trans('errors.upload.access'));
143
+}
144
+}
145 145
 
146
-    /**
146
+/**
147 147
      * @param string $sField
148 148
      *
149 149
      * @return Filesystem
150 150
      * @throws RequestException
151 151
      */
152
-    public function filesystem(string $sField = ''): Filesystem
153
-    {
154
-        $sField = trim($sField);
155
-        if(isset($this->aFilesystems[$sField]))
156
-        {
157
-            return $this->aFilesystems[$sField];
158
-        }
152
+public function filesystem(string $sField = ''): Filesystem
153
+{
154
+$sField = trim($sField);
155
+if(isset($this->aFilesystems[$sField]))
156
+{
157
+return $this->aFilesystems[$sField];
158
+}
159 159
 
160
-        // Default upload dir
161
-        $sStorage = $this->xConfigManager->getAppOption('upload.default.storage', 'upload');
162
-        $sConfigKey = "upload.files.$sField";
163
-        if($sField !== '' && $this->xConfigManager->hasAppOption($sConfigKey))
164
-        {
165
-            $sStorage = $this->xConfigManager->getAppOption("$sConfigKey.storage", $sStorage);
166
-        }
167
-        if(!is_string($sStorage))
168
-        {
169
-            throw new RequestException($this->xTranslator->trans('errors.upload.adapter'));
170
-        }
160
+// Default upload dir
161
+$sStorage = $this->xConfigManager->getAppOption('upload.default.storage', 'upload');
162
+$sConfigKey = "upload.files.$sField";
163
+if($sField !== '' && $this->xConfigManager->hasAppOption($sConfigKey))
164
+{
165
+$sStorage = $this->xConfigManager->getAppOption("$sConfigKey.storage", $sStorage);
166
+}
167
+if(!is_string($sStorage))
168
+{
169
+throw new RequestException($this->xTranslator->trans('errors.upload.adapter'));
170
+}
171 171
 
172
-        $this->aFilesystems[$sField] = $this->xStorageManager->get($sStorage);
173
-        return $this->aFilesystems[$sField];
174
-    }
172
+$this->aFilesystems[$sField] = $this->xStorageManager->get($sStorage);
173
+return $this->aFilesystems[$sField];
174
+}
175 175
 
176
-    /**
176
+/**
177 177
      * Get the path to the upload dir
178 178
      *
179 179
      * @param string $sField
@@ -181,13 +181,13 @@  discard block
 block discarded – undo
181 181
      * @return string
182 182
      * @throws RequestException
183 183
      */
184
-    private function getUploadDir(string $sField): string
185
-    {
186
-        $xFileSystem = $this->filesystem($sField);
187
-        return $this->_makeUploadDir($xFileSystem, $this->randomName() . '/');
188
-    }
184
+private function getUploadDir(string $sField): string
185
+{
186
+$xFileSystem = $this->filesystem($sField);
187
+return $this->_makeUploadDir($xFileSystem, $this->randomName() . '/');
188
+}
189 189
 
190
-    /**
190
+/**
191 191
      * Check uploaded files
192 192
      *
193 193
      * @param UploadedFile $xHttpFile
@@ -197,43 +197,43 @@  discard block
 block discarded – undo
197 197
      * @return array
198 198
      * @throws RequestException
199 199
      */
200
-    private function makeUploadedFile(UploadedFile $xHttpFile, string $sUploadDir, string $sField): array
201
-    {
202
-        // Check the uploaded file validity
203
-        $nErrorCode = $xHttpFile->getError();
204
-        if($nErrorCode !== UPLOAD_ERR_OK)
205
-        {
206
-            $this->xLogger->error('File upload error.', [
207
-                'code' => $nErrorCode,
208
-                'message' => $this->errorMessages[$nErrorCode],
209
-            ]);
210
-            $sMessage = $this->xTranslator->trans('errors.upload.failed', [
211
-                'name' => $sField,
212
-            ]);
213
-            throw new RequestException($sMessage);
214
-        }
200
+private function makeUploadedFile(UploadedFile $xHttpFile, string $sUploadDir, string $sField): array
201
+{
202
+// Check the uploaded file validity
203
+$nErrorCode = $xHttpFile->getError();
204
+if($nErrorCode !== UPLOAD_ERR_OK)
205
+{
206
+$this->xLogger->error('File upload error.', [
207
+    'code' => $nErrorCode,
208
+    'message' => $this->errorMessages[$nErrorCode],
209
+]);
210
+$sMessage = $this->xTranslator->trans('errors.upload.failed', [
211
+    'name' => $sField,
212
+]);
213
+throw new RequestException($sMessage);
214
+}
215 215
 
216
-        // Filename without the extension. Needs to be sanitized.
217
-        $sName = pathinfo($xHttpFile->getClientFilename(), PATHINFO_FILENAME);
218
-        if($this->cNameSanitizer !== null)
219
-        {
220
-            $sName = (string)call_user_func($this->cNameSanitizer,
221
-                $sName, $sField, $this->sUploadFieldId);
222
-        }
216
+// Filename without the extension. Needs to be sanitized.
217
+$sName = pathinfo($xHttpFile->getClientFilename(), PATHINFO_FILENAME);
218
+if($this->cNameSanitizer !== null)
219
+{
220
+$sName = (string)call_user_func($this->cNameSanitizer,
221
+    $sName, $sField, $this->sUploadFieldId);
222
+}
223 223
 
224
-        // Set the user file data
225
-        $xFile = File::fromHttpFile($this->filesystem($sField), $xHttpFile, $sUploadDir, $sName);
226
-        // Verify file validity (format, size)
227
-        if(!$this->xValidator->validateUploadedFile($sField, $xFile))
228
-        {
229
-            throw new RequestException($this->xValidator->getErrorMessage());
230
-        }
224
+// Set the user file data
225
+$xFile = File::fromHttpFile($this->filesystem($sField), $xHttpFile, $sUploadDir, $sName);
226
+// Verify file validity (format, size)
227
+if(!$this->xValidator->validateUploadedFile($sField, $xFile))
228
+{
229
+throw new RequestException($this->xValidator->getErrorMessage());
230
+}
231 231
 
232
-        // All's right, save the file for copy.
233
-        return ['temp' => $xHttpFile, 'user' => $xFile];
234
-    }
232
+// All's right, save the file for copy.
233
+return ['temp' => $xHttpFile, 'user' => $xFile];
234
+}
235 235
 
236
-    /**
236
+/**
237 237
      * Read uploaded files info from HTTP request data
238 238
      *
239 239
      * @param ServerRequestInterface $xRequest
@@ -241,46 +241,46 @@  discard block
 block discarded – undo
241 241
      * @return array
242 242
      * @throws RequestException
243 243
      */
244
-    public function readFromHttpData(ServerRequestInterface $xRequest): array
245
-    {
246
-        // Get the uploaded files
247
-        $aTempFiles = $xRequest->getUploadedFiles();
244
+public function readFromHttpData(ServerRequestInterface $xRequest): array
245
+{
246
+// Get the uploaded files
247
+$aTempFiles = $xRequest->getUploadedFiles();
248 248
 
249
-        $aUserFiles = [];
250
-        $aAllFiles = []; // A flat list of all uploaded files
251
-        foreach($aTempFiles as $sField => $aFiles)
252
-        {
253
-            $aUserFiles[$sField] = [];
254
-            // Get the path to the upload dir
255
-            $sUploadDir = $this->getUploadDir($sField);
256
-            if(!is_array($aFiles))
257
-            {
258
-                $aFiles = [$aFiles];
259
-            }
260
-            foreach($aFiles as $xHttpFile)
261
-            {
262
-                $aFile = $this->makeUploadedFile($xHttpFile, $sUploadDir, $sField);
263
-                $aUserFiles[$sField][] = $aFile['user'];
264
-                $aAllFiles[] = $aFile;
265
-            }
266
-        }
249
+$aUserFiles = [];
250
+$aAllFiles = []; // A flat list of all uploaded files
251
+foreach($aTempFiles as $sField => $aFiles)
252
+{
253
+$aUserFiles[$sField] = [];
254
+// Get the path to the upload dir
255
+$sUploadDir = $this->getUploadDir($sField);
256
+if(!is_array($aFiles))
257
+{
258
+    $aFiles = [$aFiles];
259
+}
260
+foreach($aFiles as $xHttpFile)
261
+{
262
+    $aFile = $this->makeUploadedFile($xHttpFile, $sUploadDir, $sField);
263
+    $aUserFiles[$sField][] = $aFile['user'];
264
+    $aAllFiles[] = $aFile;
265
+}
266
+}
267 267
 
268
-        // Copy the uploaded files from the temp dir to the user dir
269
-        try
270
-        {
271
-            foreach($aAllFiles as $aFiles)
272
-            {
273
-                $sPath = $aFiles['user']->path();
274
-                $xContent = $aFiles['temp']->getStream();
275
-                $aFiles['user']->filesystem()->write($sPath, $xContent);
276
-            }
277
-        }
278
-        catch(FilesystemException $e)
279
-        {
280
-            $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
281
-            throw new RequestException($this->xTranslator->trans('errors.upload.access'));
282
-        }
268
+// Copy the uploaded files from the temp dir to the user dir
269
+try
270
+{
271
+foreach($aAllFiles as $aFiles)
272
+{
273
+    $sPath = $aFiles['user']->path();
274
+    $xContent = $aFiles['temp']->getStream();
275
+    $aFiles['user']->filesystem()->write($sPath, $xContent);
276
+}
277
+}
278
+catch(FilesystemException $e)
279
+{
280
+$this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
281
+throw new RequestException($this->xTranslator->trans('errors.upload.access'));
282
+}
283 283
 
284
-        return $aUserFiles;
285
-    }
284
+return $aUserFiles;
285
+}
286 286
 }
Please login to merge, or discard this patch.
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -130,13 +130,13 @@  discard block
 block discarded – undo
130 130
         try
131 131
         {
132 132
             $xFilesystem->createDirectory($sUploadDir);
133
-            if(!$xFilesystem->directoryExists($sUploadDir))
133
+            if (!$xFilesystem->directoryExists($sUploadDir))
134 134
             {
135 135
                 throw new RequestException($this->xTranslator->trans('errors.upload.access'));
136 136
             }
137 137
             return $sUploadDir;
138 138
         }
139
-        catch(FilesystemException $e)
139
+        catch (FilesystemException $e)
140 140
         {
141 141
             $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
142 142
             throw new RequestException($this->xTranslator->trans('errors.upload.access'));
@@ -152,7 +152,7 @@  discard block
 block discarded – undo
152 152
     public function filesystem(string $sField = ''): Filesystem
153 153
     {
154 154
         $sField = trim($sField);
155
-        if(isset($this->aFilesystems[$sField]))
155
+        if (isset($this->aFilesystems[$sField]))
156 156
         {
157 157
             return $this->aFilesystems[$sField];
158 158
         }
@@ -160,11 +160,11 @@  discard block
 block discarded – undo
160 160
         // Default upload dir
161 161
         $sStorage = $this->xConfigManager->getAppOption('upload.default.storage', 'upload');
162 162
         $sConfigKey = "upload.files.$sField";
163
-        if($sField !== '' && $this->xConfigManager->hasAppOption($sConfigKey))
163
+        if ($sField !== '' && $this->xConfigManager->hasAppOption($sConfigKey))
164 164
         {
165 165
             $sStorage = $this->xConfigManager->getAppOption("$sConfigKey.storage", $sStorage);
166 166
         }
167
-        if(!is_string($sStorage))
167
+        if (!is_string($sStorage))
168 168
         {
169 169
             throw new RequestException($this->xTranslator->trans('errors.upload.adapter'));
170 170
         }
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
     {
202 202
         // Check the uploaded file validity
203 203
         $nErrorCode = $xHttpFile->getError();
204
-        if($nErrorCode !== UPLOAD_ERR_OK)
204
+        if ($nErrorCode !== UPLOAD_ERR_OK)
205 205
         {
206 206
             $this->xLogger->error('File upload error.', [
207 207
                 'code' => $nErrorCode,
@@ -215,7 +215,7 @@  discard block
 block discarded – undo
215 215
 
216 216
         // Filename without the extension. Needs to be sanitized.
217 217
         $sName = pathinfo($xHttpFile->getClientFilename(), PATHINFO_FILENAME);
218
-        if($this->cNameSanitizer !== null)
218
+        if ($this->cNameSanitizer !== null)
219 219
         {
220 220
             $sName = (string)call_user_func($this->cNameSanitizer,
221 221
                 $sName, $sField, $this->sUploadFieldId);
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
         // Set the user file data
225 225
         $xFile = File::fromHttpFile($this->filesystem($sField), $xHttpFile, $sUploadDir, $sName);
226 226
         // Verify file validity (format, size)
227
-        if(!$this->xValidator->validateUploadedFile($sField, $xFile))
227
+        if (!$this->xValidator->validateUploadedFile($sField, $xFile))
228 228
         {
229 229
             throw new RequestException($this->xValidator->getErrorMessage());
230 230
         }
@@ -248,16 +248,16 @@  discard block
 block discarded – undo
248 248
 
249 249
         $aUserFiles = [];
250 250
         $aAllFiles = []; // A flat list of all uploaded files
251
-        foreach($aTempFiles as $sField => $aFiles)
251
+        foreach ($aTempFiles as $sField => $aFiles)
252 252
         {
253 253
             $aUserFiles[$sField] = [];
254 254
             // Get the path to the upload dir
255 255
             $sUploadDir = $this->getUploadDir($sField);
256
-            if(!is_array($aFiles))
256
+            if (!is_array($aFiles))
257 257
             {
258 258
                 $aFiles = [$aFiles];
259 259
             }
260
-            foreach($aFiles as $xHttpFile)
260
+            foreach ($aFiles as $xHttpFile)
261 261
             {
262 262
                 $aFile = $this->makeUploadedFile($xHttpFile, $sUploadDir, $sField);
263 263
                 $aUserFiles[$sField][] = $aFile['user'];
@@ -268,14 +268,14 @@  discard block
 block discarded – undo
268 268
         // Copy the uploaded files from the temp dir to the user dir
269 269
         try
270 270
         {
271
-            foreach($aAllFiles as $aFiles)
271
+            foreach ($aAllFiles as $aFiles)
272 272
             {
273 273
                 $sPath = $aFiles['user']->path();
274 274
                 $xContent = $aFiles['temp']->getStream();
275 275
                 $aFiles['user']->filesystem()->write($sPath, $xContent);
276 276
             }
277 277
         }
278
-        catch(FilesystemException $e)
278
+        catch (FilesystemException $e)
279 279
         {
280 280
             $this->xLogger->error('Filesystem error.', ['message' => $e->getMessage()]);
281 281
             throw new RequestException($this->xTranslator->trans('errors.upload.access'));
Please login to merge, or discard this patch.