Completed
Push — master ( 4dd6d0...6c40dd )
by Taosikai
10:58
created

src/UploadHandler.php (1 issue)

super-globals are not used.

Coding Style Minor

Upgrade to new PHP Analysis Engine

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\File;
17
use Symfony\Component\HttpFoundation\File\UploadedFile;
18
use Symfony\Component\HttpFoundation\FileBag;
19
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
20
21
class UploadHandler
22
{
23
    /**
24
     * @var NamerInterface
25
     */
26
    protected $namer;
27
28
    /**
29
     * @var FilesystemInterface
30
     */
31
    protected $filesystem;
32
33
    /**
34
     * @var Validator
35
     */
36
    protected $validator;
37
38
    /**
39
     * @var boolean
40
     */
41
    protected $overwrite;
42
43
    /**
44
     * @var FileBag
45
     */
46
    protected $uploadedFiles;
47
48
    public function __construct(
49
        FilesystemInterface $filesystem,
50
        NamerInterface $namer,
51
        $overwrite = false
52
    ) {
53
        $this->filesystem = $filesystem;
54
        $this->namer = $namer;
55
        $this->overwrite = $overwrite;
56
        $this->validator = new Validator();
57
    }
58
59
    /**
60
     * Gets the validator
61
     *
62
     * @return Validator
63
     */
64
    public function getValidator()
65
    {
66
        return $this->validator;
67
    }
68
69
    /**
70
     * Gets all uploaded files.
71
     *
72
     * [
73
     *     'file1' => UploadedFile,
74
     *     'file2' => [
75
     *         UploadedFile,
76
     *         UploadedFile
77
     *     ],
78
     * ]
79
     * @return UploadedFile[]
80
     */
81
    public function getUploadedFiles()
82
    {
83
        return $this->uploadedFiles->all();
84
    }
85
86
    /**
87
     * Process request
88
     * @param SymfonyRequest|null $request
89
     *
90
     * @return File[]|\Exception[]|File
91
     */
92
    public function handle($request = null)
93
    {
94
        return $this->processUploadedFiles($this->createUploadedFiles($request));
95
    }
96
97
    protected function processUploadedFiles($uploadedFiles)
98
    {
99
        $files = [];
100
        foreach ($uploadedFiles as $name => $uploadedFileItem) {
101
            if (is_array($uploadedFileItem)) {
102
                $files[$name] = $this->processUploadedFiles($uploadedFileItem);
103
            } else {
104
                $files[$name] = $this->processUploadedFile($uploadedFileItem);
105
            }
106
        }
107
        return $files;
108
    }
109
110
    protected function processUploadedFile(UploadedFile $file)
111
    {
112
        try {
113
            // validate the file
114
            $this->validator->validate($file);
115
            return $this->filesystem
116
                ->upload($this->namer->generate($file), $file, $this->overwrite);
117
        } catch (\Exception $exception) {
118
            return $exception;
119
        }
120
    }
121
122
    /**
123
     * @param SymfonyRequest|null $request
124
     * @return FileBag
125
     */
126
    protected function createUploadedFiles($request = null)
0 ignored issues
show
createUploadedFiles uses the super-global variable $_FILES which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
127
    {
128
        if ($request instanceof SymfonyRequest) {
129
            $files = $request->files;
130
        } else {
131
            $files = new FileBag($_FILES);
132
        }
133
        return $this->uploadedFiles = $files;
134
    }
135
}