Passed
Push — master ( 8e4817...a6357e )
by Zlatin
01:19
created

UploadedFile::moveTo()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 15
ccs 0
cts 9
cp 0
crap 20
rs 9.2
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 StreamInterface
33
     */
34
    private $stream;
35
36
    /**
37
     * @param string|resource|StreamInterface $stream
38
     * @param int $size
39
     * @param int $error
40
     * @param string|null $clientFilename
41
     * @param string|null $clientMediaType
42
     * @throws \RuntimeException
43
     */
44 4
    public function __construct($stream, $size, $error = UPLOAD_ERR_OK, $clientFilename = null, $clientMediaType = null)
45
    {
46 4
        if (is_string($stream) || is_resource($stream)) {
47
            $this->stream = new Stream($stream);
48 4
        } else if ($stream instanceof StreamInterface) {
0 ignored issues
show
introduced by
The condition $stream instanceof Psr\H...Message\StreamInterface can never be false since $stream is always a sub-type of Psr\Http\Message\StreamInterface.
Loading history...
49 4
            $this->stream = $stream;
50 2
        } else {
51 2
            throw new \RuntimeException('Invalid uploaded file.');
52
        }
53
54 4
        $this->size = $size;
55 4
        $this->error = $error;
56 4
        $this->clientFilename = $clientFilename;
57 4
        $this->clientMediaType = $clientMediaType;
58 4
    }
59
60
    /**
61
     * @return array
62
     * @throws \InvalidArgumentException
63
     */
64
    public static function createFromGlobal()
65
    {
66
        $normalize = [];
67
        foreach ($_FILES AS $key => $value) {
68
            if ($value instanceof UploadedFileInterface) {
69
                $normalize[$key] = $value;
70
            } else if (is_array($value) && isset($value['tmp_name'])) {
71
                $normalize[$key] = new UploadedFile($value['tmp_name'], $value['size'], $value['error'], $value['name'], $value['type']);
72
            } else {
73
                throw new \InvalidArgumentException('Invalid value in files specification');
74
            }
75
        }
76
        return $normalize;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getClientFilename()
83
    {
84
        return $this->clientFilename;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getClientMediaType()
91
    {
92
        return $this->clientMediaType;
93
    }
94
95
    /**
96
     * @return int
97
     */
98
    public function getError()
99
    {
100
        return $this->error;
101
    }
102
103
    /**
104
     * @return int
105
     */
106 2
    public function getSize()
107
    {
108 2
        return $this->size;
109
    }
110
111
    /**
112
     * @return StreamInterface
113
     */
114
    public function getStream()
115
    {
116
        return $this->stream;
117
    }
118
119
    /**
120
     * @param mixed $targetPath
121
     * @throws \RuntimeException
122
     */
123
    public function moveTo($targetPath)
124
    {
125
126
        if (!is_dir($targetPath)) {
127
            throw new \RuntimeException('Invalid targetPath specified.');
128
        }
129
130
        if ($this->stream) {
131
            $upload = stream_copy_to_stream($this->stream, $targetPath);
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

131
            $upload = stream_copy_to_stream(/** @scrutinizer ignore-type */ $this->stream, $targetPath);
Loading history...
132
        } else {
133
            throw new \RuntimeException('Invalid uploaded file.');
134
        }
135
136
        if (!$upload) {
137
            throw new \RuntimeException('Erro while uploading file.');
138
        }
139
    }
140
}
141