Passed
Push — master ( b65012...5ddc87 )
by Zlatin
01:29
created

UploadedFile   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 58.7%

Importance

Changes 0
Metric Value
dl 0
loc 153
ccs 27
cts 46
cp 0.587
rs 10
c 0
b 0
f 0
wmc 22

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getClientFilename() 0 3 1
B createFromGlobal() 0 13 5
A getStream() 0 7 2
A getSize() 0 6 2
A __construct() 0 17 4
B moveTo() 0 17 6
A getClientMediaType() 0 3 1
A getError() 0 3 1
1
<?php
2
namespace DevOp\Core\Http;
3
4
use Psr\Http\Message\StreamInterface;
5
use Psr\Http\Message\UploadedFileInterface;
6
7
class UploadedFile implements UploadedFileInterface
8
{
9
10
    /**
11
     * @var string
12
     */
13
    private $clientFilename;
14
15
    /**
16
     * @var string
17
     */
18
    private $clientMediaType;
19
20
    /**
21
     *
22
     * @var int
23
     */
24
    private $error;
25
26
    /**
27
     * @var int 
28
     */
29
    private $size;
30
31
    /**
32
     * @var null|string
33
     */
34
    private $file;
35
36
    /**
37
     * @var StreamInterface
38
     */
39
    private $stream;
40
41
    /**
42
     * @var boolean
43
     */
44
    private $moved = false;
45
46
    /**
47
     * @param mixed $streamOrFile
48
     * @param int $size
49
     * @param int $error
50
     * @param string|null $clientFilename
51
     * @param string|null $clientMediaType
52
     * @throws \RuntimeException
53
     */
54 10
    public function __construct($streamOrFile, $size, $error = UPLOAD_ERR_OK, $clientFilename = null, $clientMediaType = null)
55
    {
56
57 10
        if (is_string($streamOrFile)) {
58 2
            $this->file = $streamOrFile;
59 8
        } elseif (is_resource($streamOrFile)) {
60 4
            $this->stream = new Stream($streamOrFile);
61 4
        } elseif ($streamOrFile instanceof StreamInterface) {
62 4
            $this->stream = $streamOrFile;
63
        } else {
64 2
            throw new \RuntimeException('Invalid stream or file provided for UploadedFile');
65
        }
66
67 10
        $this->size = $size;
68 10
        $this->error = $error;
69 10
        $this->clientFilename = $clientFilename;
70 10
        $this->clientMediaType = $clientMediaType;
71 10
    }
72
73
    /**
74
     * @return array
75
     * @throws \InvalidArgumentException
76
     */
77
    public static function createFromGlobal()
78
    {
79
        $normalize = [];
80
        foreach ($_FILES AS $key => $value) {
81
            if ($value instanceof UploadedFileInterface) {
82
                $normalize[$key] = $value;
83
            } else if (is_array($value) && isset($value['tmp_name'])) {
84
                $normalize[$key] = new UploadedFile($value['tmp_name'], $value['size'], $value['error'], $value['name'], $value['type']);
85
            } else {
86
                throw new \InvalidArgumentException('Invalid value in files specification');
87
            }
88
        }
89
        return $normalize;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 6
    public function getClientFilename()
96
    {
97 6
        return $this->clientFilename;
98
    }
99
100
    /**
101
     * @return string
102
     */
103 6
    public function getClientMediaType()
104
    {
105 6
        return $this->clientMediaType;
106
    }
107
108
    /**
109
     * @return int
110
     */
111 8
    public function getError()
112
    {
113 8
        return $this->error;
114
    }
115
116
    /**
117
     * @return int
118
     */
119 6
    public function getSize()
120
    {
121 6
        if (null === $this->size) {
122 4
            $this->size = $this->getStream()->getSize();
123
        }
124 6
        return $this->size;
125
    }
126
127
    /**
128
     * @return StreamInterface
129
     */
130 4
    public function getStream()
131
    {
132 4
        if ($this->stream instanceof StreamInterface) {
0 ignored issues
show
introduced by
$this->stream is always a sub-type of Psr\Http\Message\StreamInterface. If $this->stream can have other possible types, add them to src/UploadedFile.php:37.
Loading history...
133 2
            return $this->stream;
134
        }
135
        
136 2
        return (new StreamFactory())->createStreamFromFile($this->file, 'r+');
137
    }
138
139
    /**
140
     * @param mixed $targetPath
141
     * @throws \RuntimeException
142
     */
143
    public function moveTo($targetPath)
144
    {
145
146
        if (!is_dir($targetPath)) {
147
            throw new \RuntimeException('Invalid targetPath specified.');
148
        }
149
150
        if ($this->file && is_string($this->file)) {
151
            $this->moved = move_uploaded_file($this->file, $targetPath);
152
        } else if ($this->stream) {
153
            $this->moved = stream_copy_to_stream($this->stream->detach(), $targetPath) > 0;
154
        } else {
155
            throw new \RuntimeException('Invalid uploaded file.');
156
        }
157
158
        if (!$this->moved) {
159
            throw new \RuntimeException('Error  while uploading file.');
160
        }
161
    }
162
}
163