Completed
Push — master ( 7d7f55...e32ff2 )
by James Ekow Abaka
13s
created

UploadedFile::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ntentan\utils\filesystem;
4
5
use ntentan\utils\exceptions\FilesystemException;
6
use ntentan\utils\Filesystem;
7
8
/**
9
 * Represents file that was uploaded through PHPs internal HTTP mechanisms.
10
 *
11
 * @package ntentan\utils\filesystem
12
 */
13
class UploadedFile extends File
14
{
15
    /**
16
     * Filename of the file from the client.
17
     *
18
     * @var string
19
     */
20
    private $clientName;
21
22
    /**
23
     * File type.
24
     *
25
     * @var string
26
     */
27
    private $type;
28
29
    /**
30
     * Any upload errors that occured.
31
     * These are based on the errors described for PHPs $_FILES global variable.
32
     *
33
     * @var int
34
     */
35
    private $error;
36
37
    /**
38
     * Size reported when file was uploaded.
39
     *
40
     * @var int
41
     */
42
    private $size;
43
44
    /**
45
     * Create a new instance.
46
     *
47
     * @param array $file The $_FILES[name] value for a given upload field
48
     * @throws FilesystemException
49
     */
50 5
    public function __construct($file)
51
    {
52 5
        parent::__construct($file['tmp_name']);
53 5
        $this->clientName = $file['name'];
54 5
        $this->type = $file['type'];
55 5
        $this->error = $file['error'];
56 5
        $this->size = $file['size'];
57 5
        if (!is_uploaded_file($file['tmp_name'])) {
58 1
            throw new FilesystemException("File {$file['tmp_name']} is not an uploaded file");
59
        }
60 4
    }
61
62
    /**
63
     * Get the size of the uploaded file as reported by PHP.
64
     *
65
     * @return int
66
     */
67 1
    public function getSize(): int
68
    {
69 1
        return $this->size;
70
    }
71
72
    /**
73
     * Move the uploaded file safely to another location.
74
     * Ensures that files were actually uploaded through PHP before moving them.
75
     *
76
     * @param string $destination
77
     * @throws FilesystemException
78
     * @throws \ntentan\utils\exceptions\FileNotWriteableException
79
     */
80 3
    public function moveTo(string $destination): void
81
    {
82 3
        $destination = is_dir($destination) ? ("$destination/{$this->clientName}") : $destination;
83 3
        Filesystem::checkWritable(dirname($destination));
84 3
        if (!move_uploaded_file($this->path, $destination)) {
85 1
            throw new FilesystemException("Failed to move file {$this->path} to {$destination}");
86
        }
87 2
    }
88
89
    /**
90
     * Return the error code assigned when file was uploaded.
91
     * See the documentation for the $_FILES global variable for a description of this value.
92
     *
93
     * @return int
94
     */
95 1
    public function getError(): int
96
    {
97 1
        return $this->error;
98
    }
99
100
    /**
101
     * Get the name of the file assigned from the client system.
102
     *
103
     * @return string
104
     */
105 1
    public function getClientName(): string
106
    {
107 1
        return $this->clientName;
108
    }
109
110
    /**
111
     * Get the mime type of the file.
112
     *
113
     * @return string
114
     */
115 1
    public function getType(): string
116
    {
117 1
        return $this->type;
118
    }
119
120
}
121