Passed
Push — master ( ea468d...bd1a32 )
by Rougin
02:13
created

UploadedFile   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 160
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A moveTo() 0 3 1
A create() 0 13 1
A normalize() 0 15 4
A getError() 0 3 1
A getSize() 0 3 1
A __construct() 0 11 1
A getClientFilename() 0 3 1
A getStream() 0 5 2
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 48
    public function __construct($file, $size = null, $error = UPLOAD_ERR_OK, $name = null, $media = null)
50
    {
51 48
        $this->error = $error;
52
53 48
        $this->file = $file;
54
55 48
        $this->media = $media;
56
57 48
        $this->name = $name;
58
59 48
        $this->size = $size;
60 48
    }
61
62
    /**
63
     * Retrieves the filename sent by the client.
64
     *
65
     * @return string|null
66
     */
67 3
    public function getClientFilename()
68
    {
69 3
        return $this->name;
70
    }
71
72
    /**
73
     * Retrieves the media type sent by the client.
74
     *
75
     * @return string|null
76
     */
77 3
    public function getClientMediaType()
78
    {
79 3
        return $this->media;
80
    }
81
82
    /**
83
     * Retrieves the error associated with the uploaded file.
84
     *
85
     * @return integer
86
     */
87 3
    public function getError()
88
    {
89 3
        return $this->error;
90
    }
91
92
    /**
93
     * Retrieves the file size.
94
     *
95
     * @return integer|null
96
     */
97 3
    public function getSize()
98
    {
99 3
        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 3
    public function getStream()
110
    {
111 3
        $stream = fopen($this->file, 'r');
112
113 3
        return new Stream($stream ?: null);
0 ignored issues
show
introduced by
The condition $stream can never be true.
Loading history...
114
    }
115
116
    /**
117
     * Move the uploaded file to a new location.
118
     *
119
     * @param string $target
120
     *
121
     * @throws \InvalidArgumentException
122
     * @throws \RuntimeException
123
     */
124 3
    public function moveTo($target)
125
    {
126 3
        rename($this->file, $target);
127 3
    }
128
129
    /**
130
     * Parses the $_FILES into multiple \File instances.
131
     *
132
     * @param  array $uploaded
133
     * @param  array $files
134
     * @return \Zapheus\Http\Message\FileInterface[]
135
     */
136 81
    public static function normalize(array $uploaded, $files = array())
137
    {
138 81
        foreach ((array) $uploaded as $name => $file) {
139 81
            list($files[$name], $items) = array($file, array());
140
141 81
            if (isset($file['name']) === true) {
142 27
                foreach ($file['name'] as $key => $value) {
143 27
                    $items[] = self::create($file, $key);
144 27
                }
145
146 27
                $files[$name] = $items;
147 27
            }
148 81
        }
149
150 81
        return $files;
151
    }
152
153
    /**
154
     * Creates a new UploadedFile instance.
155
     *
156
     * @param  array   $file
157
     * @param  integer $key
158
     * @return \Psr\Http\Message\UploadedFile
159
     */
160 27
    protected static function create(array $file, $key)
161
    {
162 27
        $tmp = $file['tmp_name'][$key];
163
164 27
        $size = $file['size'][$key];
165
166 27
        $error = $file['error'][$key];
167
168 27
        $original = $file['name'][$key];
169
170 27
        $type = $file['type'][$key];
171
172 27
        return new UploadedFile($tmp, $size, $error, $original, $type);
173
    }
174
}
175