1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web\adapters\silex; |
3
|
|
|
|
4
|
|
|
use rtens\domin\delivery\ParameterReader; |
5
|
|
|
use rtens\domin\Parameter; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
|
9
|
|
|
class SilexParameterReader implements ParameterReader { |
10
|
|
|
|
11
|
|
|
/** @var Request */ |
12
|
|
|
private $request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Request $request |
16
|
|
|
*/ |
17
|
|
|
public function __construct(Request $request) { |
18
|
|
|
$this->request = $request; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* IMPORTANT: files must be properly merged into the parameters |
23
|
|
|
* |
24
|
|
|
* @param Parameter $parameter |
25
|
|
|
* @return mixed The serialized paramater |
26
|
|
|
*/ |
27
|
|
|
public function read(Parameter $parameter) { |
28
|
|
|
if ($this->request->files->has($parameter->getName())) { |
29
|
|
|
return $this->map($this->request->files->get($parameter->getName())); |
30
|
|
|
} |
31
|
|
|
return $this->map($this->request->get($parameter->getName())); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Parameter $parameter |
36
|
|
|
* @return boolean |
37
|
|
|
*/ |
38
|
|
|
public function has(Parameter $parameter) { |
39
|
|
|
return $this->request->files->has($parameter->getName()) |
40
|
|
|
|| $this->request->get($parameter->getName(), '__NOPEDINOPE__') != '__NOPEDINOPE__'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
private function map($value) { |
|
|
|
|
44
|
|
|
if (is_array($value)) { |
45
|
|
|
$mapped = []; |
46
|
|
|
foreach ($value as $key => $item) { |
47
|
|
|
$mapped[$key] = $this->map($item); |
48
|
|
|
} |
49
|
|
|
return $mapped; |
50
|
|
|
} else if ($value instanceof UploadedFile) { |
|
|
|
|
51
|
|
|
return [ |
52
|
|
|
'name' => $value->getClientOriginalName(), |
53
|
|
|
'tmp_name' => $value->getPath() . DIRECTORY_SEPARATOR . $value->getFilename(), |
54
|
|
|
'error' => $value->getError(), |
55
|
|
|
'size' => $value->getSize(), |
56
|
|
|
'type' => $value->getType() |
57
|
|
|
]; |
58
|
|
|
} else { |
59
|
|
|
return $value; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
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.