UploadedFiles::getFileType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ByJG\RestServer;
4
5
use InvalidArgumentException;
6
7
class UploadedFiles
8
{
9
    public function count()
10
    {
11
        return count($_FILES);
12
    }
13
14
    public function getKeys()
15
    {
16
        return array_keys($_FILES);
17
    }
18
19
    public function isOk($key)
20
    {
21
        return $this->getFileByKey($key, 'error');
22
    }
23
24
    public function getUploadedFile($key)
25
    {
26
        return file_get_contents($this->getFileByKey($key, 'tmp_name'));
27
    }
28
29
    public function getFileName($key)
30
    {
31
        return $this->getFileByKey($key, 'name');
32
    }
33
34
    public function getFileType($key)
35
    {
36
        return $this->getFileByKey($key, 'type');
37
    }
38
39
    public function saveTo($key, $destinationPath, $newName = "")
40
    {
41
        if (empty($newName)) {
42
            $newName = $this->getFileName($key);
43
        }
44
45
        move_uploaded_file($this->getFileByKey($key, 'tmp_name'), $destinationPath . '/' . $newName);
46
    }
47
48
    public function clearTemp($key)
49
    {
50
        unlink($this->getFileByKey($key, 'tmp_name'));
51
    }
52
53
    private function getFileByKey($key, $property)
54
    {
55
        if (!isset($_FILES[$key])) {
56
            throw new InvalidArgumentException("The upload '$key' does not exists");
57
        }
58
59
        return $_FILES[$key][$property];
60
    }
61
}
62