UploadedFiles   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 55
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 4 1
A getKeys() 0 4 1
A isOk() 0 4 1
A getUploadedFile() 0 4 1
A getFileName() 0 4 1
A getFileType() 0 4 1
A saveTo() 0 8 2
A clearTemp() 0 4 1
A getFileByKey() 0 8 2
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