Completed
Push — master ( e32ff2...151f35 )
by James Ekow Abaka
8s
created

UploadedFile   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 110
c 0
b 0
f 0
ccs 24
cts 25
cp 0.96
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getSize() 0 4 1
A moveTo() 0 8 3
A getError() 0 4 1
A getClientName() 0 4 1
A getType() 0 4 1
1
<?php
2
3
namespace ntentan\utils\filesystem;
4
5
use ntentan\utils\exceptions\FilesystemException;
6
use ntentan\utils\Filesystem;
7
8
/**
9
 * Represents a 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(array $file = null)
51
    {
52 5
        if($file === null) {
53
            throw new FilesystemException("Failed to complete file upload");
54 5
        } else if (!is_uploaded_file($file['tmp_name'])) {
55 1
            throw new FilesystemException("File {$file['tmp_name']} is not an uploaded file.");
56
        }
57 4
        parent::__construct($file['tmp_name']);
58 4
        $this->clientName = $file['name'];
59 4
        $this->type = $file['type'];
60 4
        $this->error = $file['error'];
61 4
        $this->size = $file['size'];
62 4
    }
63
64
    /**
65
     * Get the size of the uploaded file as reported by PHP.
66
     *
67
     * @return int
68
     */
69 1
    public function getSize(): int
70
    {
71 1
        return $this->size;
72
    }
73
74
    /**
75
     * Move the uploaded file safely to another location.
76
     * Ensures that files were actually uploaded through PHP before moving them.
77
     *
78
     * @param string $destination
79
     * @throws FilesystemException
80
     * @throws \ntentan\utils\exceptions\FileNotWriteableException
81
     */
82 3
    public function moveTo(string $destination): void
83
    {
84 3
        $destination = is_dir($destination) ? ("$destination/{$this->clientName}") : $destination;
85 3
        Filesystem::checkWritable(dirname($destination));
86 3
        if (!move_uploaded_file($this->path, $destination)) {
87 1
            throw new FilesystemException("Failed to move file {$this->path} to {$destination}");
88
        }
89 2
    }
90
91
    /**
92
     * Return the error code assigned when file was uploaded.
93
     * See the documentation for the $_FILES global variable for a description of this value.
94
     *
95
     * @return int
96
     */
97 1
    public function getError(): int
98
    {
99 1
        return $this->error;
100
    }
101
102
    /**
103
     * Get the name of the file assigned from the client system.
104
     *
105
     * @return string
106
     */
107 1
    public function getClientName(): string
108
    {
109 1
        return $this->clientName;
110
    }
111
112
    /**
113
     * Get the mime type of the file.
114
     *
115
     * @return string
116
     */
117 1
    public function getType(): string
118
    {
119 1
        return $this->type;
120
    }
121
122
}
123