Passed
Push — master ( d0466a...c02546 )
by Zlatin
01:22
created

UploadedFile::moveTo()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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

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

136
            $upload = move_uploaded_file(/** @scrutinizer ignore-type */ $this->file, $targetPath);
Loading history...
137
        } else if ($this->stream) {
138
            $upload = copy_to_stream($this->stream, $targetPath);
0 ignored issues
show
Bug introduced by
The function copy_to_stream was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

138
            $upload = /** @scrutinizer ignore-call */ copy_to_stream($this->stream, $targetPath);
Loading history...
139
        } else {
140
            throw new \RuntimeException('Invalid uploaded file.');
141
        }
142
143
        if (!$upload) {
144
            throw new \RuntimeException('Erro while uploading file.');
145
        }
146
    }
147
}
148