Passed
Push — master ( 8fd177...8c2cea )
by Rougin
03:08
created

UploadedFile   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 190
ccs 0
cts 67
cp 0
rs 10
c 1
b 0
f 0
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A moveTo() 0 6 1
A create() 0 13 1
A normalize() 0 17 4
A getError() 0 3 1
A getSize() 0 3 1
A __construct() 0 11 1
A getClientFilename() 0 3 1
A getStream() 0 9 2
A diverse() 0 13 3
A getClientMediaType() 0 3 1
1
<?php
2
3
namespace Zapheus\Bridge\Psr;
4
5
use Psr\Http\Message\UploadedFileInterface;
6
7
/**
8
 * Uploaded File
9
 *
10
 * @package Zapheus
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class UploadedFile implements UploadedFileInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $file;
19
20
    /**
21
     * @var integer|null
22
     */
23
    protected $size;
24
25
    /**
26
     * @var integer
27
     */
28
    protected $error;
29
30
    /**
31
     * @var string
32
     */
33
    protected $name;
34
35
    /**
36
     * @var string
37
     */
38
    protected $media;
39
40
    /**
41
     * Initializes the uploaded file instance.
42
     *
43
     * @param string       $file
44
     * @param integer|null $size
45
     * @param integer      $error
46
     * @param string|null  $name
47
     * @param string|null  $media
48
     */
49
    public function __construct($file, $size = null, $error = UPLOAD_ERR_OK, $name = null, $media = null)
50
    {
51
        $this->error = $error;
52
53
        $this->file = $file;
54
55
        $this->media = $media;
56
57
        $this->name = $name;
58
59
        $this->size = $size;
60
    }
61
62
    /**
63
     * Retrieves the filename sent by the client.
64
     *
65
     * @return string|null
66
     */
67
    public function getClientFilename()
68
    {
69
        return $this->name;
70
    }
71
72
    /**
73
     * Retrieves the media type sent by the client.
74
     *
75
     * @return string|null
76
     */
77
    public function getClientMediaType()
78
    {
79
        return $this->media;
80
    }
81
82
    /**
83
     * Retrieves the error associated with the uploaded file.
84
     *
85
     * @return integer
86
     */
87
    public function getError()
88
    {
89
        return $this->error;
90
    }
91
92
    /**
93
     * Retrieves the file size.
94
     *
95
     * @return integer|null
96
     */
97
    public function getSize()
98
    {
99
        return $this->size;
100
    }
101
102
    /**
103
     * Retrieves a stream representing the uploaded file.
104
     *
105
     * @return \Psr\Http\Message\StreamInterface
106
     *
107
     * @throws \RuntimeException
108
     */
109
    public function getStream()
110
    {
111
        // TODO: Add \RuntimeException
112
113
        $stream = fopen($this->file, 'r+');
114
115
        $stream = $stream === false ? null : $stream;
116
117
        return new Stream($stream);
118
    }
119
120
    /**
121
     * Move the uploaded file to a new location.
122
     *
123
     * @param string $target
124
     *
125
     * @throws \InvalidArgumentException
126
     * @throws \RuntimeException
127
     */
128
    public function moveTo($target)
129
    {
130
        // TODO: Add \InvalidArgumentException
131
        // TODO: Add \RuntimeException
132
133
        rename($this->file, $target);
134
    }
135
136
    /**
137
     * Parses the $_FILES into multiple \File instances.
138
     *
139
     * @param  array $uploaded
140
     * @param  array $files
141
     * @return \Zapheus\Http\Message\FileInterface[]
142
     */
143
    public static function normalize(array $uploaded, $files = array())
144
    {
145
        foreach (self::diverse($uploaded) as $name => $file) {
146
            list($files[$name], $items) = array($file, array());
147
148
            if (isset($file['name']) === true) {
149
                foreach ($file['name'] as $key => $value) {
150
                    $instance = self::create($file, $key);
151
152
                    array_push($items, $instance);
153
                }
154
155
                $files[$name] = (array) $items;
156
            }
157
        }
158
159
        return $files;
160
    }
161
162
    /**
163
     * Creates a new UploadedFile instance.
164
     *
165
     * @param  array   $file
166
     * @param  integer $key
167
     * @return \Psr\Http\Message\UploadedFile
168
     */
169
    protected static function create(array $file, $key)
170
    {
171
        $tmp = $file['tmp_name'][$key];
172
173
        $size = $file['size'][$key];
174
175
        $error = $file['error'][$key];
176
177
        $original = $file['name'][$key];
178
179
        $type = $file['type'][$key];
180
181
        return new UploadedFile($tmp, $size, $error, $original, $type);
182
    }
183
184
    /**
185
     * Diverse the $_FILES into a consistent result.
186
     *
187
     * @param  array $uploaded
188
     * @return array
189
     */
190
    protected static function diverse(array $uploaded)
191
    {
192
        $result = array();
193
194
        foreach ($uploaded as $file => $item) {
195
            foreach ($item as $key => $value) {
196
                $diversed = (array) $value;
197
198
                $result[$file][$key] = $diversed;
199
            }
200
        }
201
202
        return $result;
203
    }
204
}
205