Passed
Push — master ( acf3f9...98cfe3 )
by Derek Stephen
03:04
created

FileUpload::setUploadDirectory()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Del\Form\Field;
4
5
use Del\Form\Renderer\Field\FileUploadRender;
6
use InvalidArgumentException;
7
use LogicException;
8
9
class FileUpload extends FieldAbstract implements FieldInterface
10
{
11
    /** @var string $uploadDirectory */
12
    private $uploadDirectory;
13
14
    /** @var array $_FILES */
15
    private $files;
16
17
    /**
18
     * @return string
19
     */
20 2
    public function getTag(): string
21
    {
22 2
        return 'input';
23
    }
24
25 5
    public function init(): void
26
    {
27 5
        $this->setAttribute('type', 'file');
28 5
        $this->setRenderer(new FileUploadRender());
29 5
        $this->files = $_FILES;
30
31 5
        if ($this->hasUploadedFile()) {
32 2
            $this->setValue($this->files[$this->getName()]['name']);
33
        }
34 5
    }
35
36
    /**
37
     * @return bool
38
     */
39 5
    private function hasUploadedFile(): bool
40
    {
41 5
        return $this->isFileArraySet() && $this->isTempNameSet();
42
    }
43
44
    /**
45
     * @return bool
46
     */
47 5
    private function isFileArraySet(): bool
48
    {
49 5
        return isset($this->files[$this->getName()]);
50
    }
51
52
    /**
53
     * @return bool
54
     */
55 2
    private function isTempNameSet(): bool
56
    {
57 2
        return isset($this->files[$this->getName()]['tmp_name']);
58
    }
59
60
    /**
61
     * @param $path
62
     */
63 2
    public function setUploadDirectory(string $path): void
64
    {
65 2
        $path = realpath($path);
66
67 2
        if (!is_dir($path) || !is_writable($path)) {
68 1
            throw new InvalidArgumentException('Directory does not exist or is not writable.');
69
        }
70
71 1
        $this->uploadDirectory = $path;
72 1
    }
73
74
    /**
75
     * @return string
76
     */
77 1
    public function getUploadDirectory(): string
78
    {
79 1
        return $this->uploadDirectory;
80
    }
81
82
    /**
83
     * @return bool
84
     */
85 2
    public function hasUploadDirectory(): bool
86
    {
87 2
        return $this->uploadDirectory !== null;
88
    }
89
90
    /**
91
     * @return bool
92
     */
93 2
    public function moveUploadToDestination(): bool
94
    {
95 2
        if (!$this->hasUploadDirectory()) {
96 1
            throw new LogicException('No destination directory set using setUploadDirectory($path)');
97
        }
98
99 1
        $tmp = $this->files[$this->getName()]['tmp_name'];
100 1
        $destination = $this->getUploadDirectory().DIRECTORY_SEPARATOR.$this->files[$this->getName()]['name'];
101 1
        $success = move_uploaded_file($tmp, $destination);
102
103 1
        return $success;
104
    }
105
}