These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the slince/upload package. |
||
5 | * |
||
6 | * (c) Slince <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Slince\Upload; |
||
13 | |||
14 | use Slince\Upload\Filesystem\FilesystemInterface; |
||
15 | use Slince\Upload\Naming\NamerInterface; |
||
16 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
||
17 | use Symfony\Component\HttpFoundation\FileBag; |
||
18 | use Symfony\Component\HttpFoundation\Request as SymfonyRequest; |
||
19 | |||
20 | class UploadHandler |
||
21 | { |
||
22 | /** |
||
23 | * @var NamerInterface |
||
24 | */ |
||
25 | protected $namer; |
||
26 | |||
27 | /** |
||
28 | * @var FilesystemInterface |
||
29 | */ |
||
30 | protected $filesystem; |
||
31 | |||
32 | /** |
||
33 | * @var Validator |
||
34 | */ |
||
35 | protected $validator; |
||
36 | |||
37 | /** |
||
38 | * @var boolean |
||
39 | */ |
||
40 | protected $overwrite; |
||
41 | |||
42 | public function __construct( |
||
43 | FilesystemInterface $filesystem, |
||
44 | NamerInterface $namer, |
||
45 | $overwrite = false |
||
46 | ) { |
||
47 | $this->filesystem = $filesystem; |
||
48 | $this->namer = $namer; |
||
49 | $this->overwrite = $overwrite; |
||
50 | $this->validator = new Validator(); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Gets the validator |
||
55 | * |
||
56 | * @return Validator |
||
57 | */ |
||
58 | public function getValidator() |
||
59 | { |
||
60 | return $this->validator; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Process request |
||
65 | * @param SymfonyRequest|null $request |
||
66 | * |
||
67 | * @return UploadedFile[]|\Exception[]|UploadedFile |
||
68 | */ |
||
69 | public function handle($request = null) |
||
70 | { |
||
71 | $files = []; |
||
72 | foreach ($this->createFiles($request) as $uploadedFile) { |
||
73 | $files[] = $this->processFile($uploadedFile); |
||
74 | } |
||
75 | return count($files) === 1 ? $files[0] : $files; |
||
76 | } |
||
77 | |||
78 | protected function processFile(UploadedFile $file) |
||
79 | { |
||
80 | try { |
||
81 | // validate the file |
||
82 | $this->validator->validate($file); |
||
83 | return $this->filesystem |
||
84 | ->upload($this->namer->generate($file), $file, $this->overwrite); |
||
85 | } catch (\Exception $exception) { |
||
86 | return $exception; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param SymfonyRequest|null $request |
||
92 | * @return FileBag |
||
93 | */ |
||
94 | protected function createFiles($request = null) |
||
0 ignored issues
–
show
|
|||
95 | { |
||
96 | if ($request instanceof SymfonyRequest) { |
||
97 | $files = $request->files; |
||
98 | } else { |
||
99 | $files = new FileBag($_FILES); |
||
100 | } |
||
101 | return $files; |
||
102 | } |
||
103 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: