UploadedFile::getStream()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
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 null|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 4
    public function __construct($streamOrFile, $size, $error = UPLOAD_ERR_OK, $clientFilename = null, $clientMediaType = null)
55
    {
56
57 4
        if (is_string($streamOrFile)) {
58
            $this->file = $streamOrFile;
59 4
        } elseif (is_resource($streamOrFile)) {
60
            $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 4
        $this->size = $size;
68 4
        $this->error = $error;
69 4
        $this->clientFilename = $clientFilename;
70 4
        $this->clientMediaType = $clientMediaType;
71 4
    }
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 2
    public function getClientFilename()
96
    {
97 2
        return $this->clientFilename;
98
    }
99
100
    /**
101
     * @return string
102
     */
103 2
    public function getClientMediaType()
104
    {
105 2
        return $this->clientMediaType;
106
    }
107
108
    /**
109
     * @return int
110
     */
111 2
    public function getError()
112
    {
113 2
        return $this->error;
114
    }
115
116
    /**
117
     * @return int
118
     */
119 2
    public function getSize()
120
    {
121 2
        if (null === $this->size) {
122
            $this->size = $this->getStream()->getSize();
123
        }
124 2
        return $this->size;
125
    }
126
127
    /**
128
     * @return StreamInterface
129
     */
130
    public function getStream()
131
    {
132
        if ($this->stream instanceof StreamInterface) {
133
            return $this->stream;
134
        }
135
        
136
        return (new StreamFactory())->createStreamFromFile($this->file, 'r+');
0 ignored issues
show
Bug introduced by
It seems like $this->file can also be of type null; however, parameter $filename of DevOp\Core\Http\StreamFa...:createStreamFromFile() 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

136
        return (new StreamFactory())->createStreamFromFile(/** @scrutinizer ignore-type */ $this->file, 'r+');
Loading history...
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