Passed
Push — master ( a666d5...25e273 )
by Zlatin
01:27
created

UploadedFile   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 41.3%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 145
ccs 19
cts 46
cp 0.413
rs 10
c 2
b 0
f 0
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getClientFilename() 0 3 1
B createFromGlobal() 0 13 5
A getStream() 0 3 1
A getSize() 0 3 1
B moveTo() 0 17 5
A getClientMediaType() 0 3 1
A getError() 0 3 1
A __construct() 0 16 4
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 string|resource
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 StreamInterface|resource|string $stream
48
     * @param int $size
49
     * @param int $error
50
     * @param string|null $clientFilename
51
     * @param string|null $clientMediaType
52
     * @throws \RuntimeException
53
     */
54 4
    public function __construct($stream, $size = 0, $error = UPLOAD_ERR_OK, $clientFilename = null, $clientMediaType = null)
55
    {
56 4
        if ($stream instanceof StreamInterface) {
57 4
            $this->stream = $stream;
58 3
        } else if (is_resource($stream)) {
59
            $this->stream = new Stream($stream);
60 2
        } else if (is_string($stream)) {
0 ignored issues
show
introduced by
The condition is_string($stream) can never be false.
Loading history...
61
            $this->file = $stream;
62
        } else {
63 2
            throw new \RuntimeException('Invalid uploaded file.');
64
        }
65
66 4
        $this->size = $size;
67 4
        $this->error = $error;
68 4
        $this->clientFilename = $clientFilename;
69 4
        $this->clientMediaType = $clientMediaType;
70 4
    }
71
72
    /**
73
     * @return array
74
     * @throws \InvalidArgumentException
75
     */
76
    public static function createFromGlobal()
77
    {
78
        $normalize = [];
79
        foreach ($_FILES AS $key => $value) {
80
            if ($value instanceof UploadedFileInterface) {
81
                $normalize[$key] = $value;
82
            } else if (is_array($value) && isset($value['tmp_name'])) {
83
                $normalize[$key] = new UploadedFile($value['tmp_name'], $value['size'], $value['error'], $value['name'], $value['type']);
84
            } else {
85
                throw new \InvalidArgumentException('Invalid value in files specification');
86
            }
87
        }
88
        return $normalize;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 2
    public function getClientFilename()
95
    {
96 2
        return $this->clientFilename;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 2
    public function getClientMediaType()
103
    {
104 2
        return $this->clientMediaType;
105
    }
106
107
    /**
108
     * @return int
109
     */
110 2
    public function getError()
111
    {
112 2
        return $this->error;
113
    }
114
115
    /**
116
     * @return int
117
     */
118 2
    public function getSize()
119
    {
120 2
        return $this->size;
121
    }
122
123
    /**
124
     * @return StreamInterface
125
     */
126
    public function getStream()
127
    {
128
        return $this->stream;
129
    }
130
131
    /**
132
     * @param mixed $targetPath
133
     * @throws \RuntimeException
134
     */
135
    public function moveTo($targetPath)
136
    {
137
138
        if (!is_dir($targetPath)) {
139
            throw new \RuntimeException('Invalid targetPath specified.');
140
        }
141
142
        if ($this->file) {
143
            $this->moved = move_uploaded_file($this->file, $targetPath);
0 ignored issues
show
Bug introduced by
It seems like $this->file can also be of type resource; however, parameter $filename of move_uploaded_file() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
            $this->moved = move_uploaded_file(/** @scrutinizer ignore-type */ $this->file, $targetPath);
Loading history...
144
        } else if ($this->stream) {
145
            $this->moved = stream_copy_to_stream($this->stream, $targetPath) > 0;
0 ignored issues
show
Bug introduced by
$this->stream of type Psr\Http\Message\StreamInterface is incompatible with the type resource expected by parameter $source of stream_copy_to_stream(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
            $this->moved = stream_copy_to_stream(/** @scrutinizer ignore-type */ $this->stream, $targetPath) > 0;
Loading history...
146
        } else {
147
            throw new \RuntimeException('Invalid uploaded file.');
148
        }
149
150
        if (!$this->moved) {
151
            throw new \RuntimeException('Error  while uploading file.');
152
        }
153
    }
154
}
155