Completed
Push — master ( d913d0...312f5c )
by Derek Stephen
07:27
created

FileUpload::isTempNameSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 01/01/2017
5
 * Time: 19:58
6
 */
7
8
namespace Del\Form\Field;
9
10
use Del\Form\Renderer\Field\FileUploadRender;
11
use InvalidArgumentException;
12
use LogicException;
13
14
class FileUpload extends FieldAbstract implements FieldInterface
15
{
16
    /** @var string $uploadDirectory */
17
    private $uploadDirectory;
18
19
    /**
20
     * @return string
21
     */
22 2
    public function getTag()
23
    {
24 2
        return 'input';
25
    }
26
27 5
    public function init()
1 ignored issue
show
Coding Style introduced by
init uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
28
    {
29 5
        $this->setAttribute('type', 'file');
30 5
        $this->setRenderer(new FileUploadRender());
31
32 5
        if ($this->hasUploadedFile()) {
33 2
            $this->setValue($_FILES[$this->getName()]['name']);
34
        }
35 5
    }
36
37
    /**
38
     * @return bool
39
     */
40 5
    private function hasUploadedFile()
41
    {
42 5
        return $this->isFileArraySet() && $this->isTempNameSet();
43
    }
44
45
    /**
46
     * @return bool
47
     */
48 5
    private function isFileArraySet()
1 ignored issue
show
Coding Style introduced by
isFileArraySet uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
49
    {
50 5
        return isset($_FILES[$this->getName()]);
51
    }
52
53
    /**
54
     * @return bool
55
     */
56 2
    private function isTempNameSet()
1 ignored issue
show
Coding Style introduced by
isTempNameSet uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
57
    {
58 2
        return isset($_FILES[$this->getName()]['tmp_name']);
59
    }
60
61
    /**
62
     * @param $path
63
     * @return $this
64
     */
65 2
    public function setUploadDirectory($path)
66
    {
67 2
        $path = realpath($path);
68 2
        if (!is_dir($path) || !is_writable($path)) {
69 1
            throw new InvalidArgumentException('Directory does not exist or is not writable.');
70
        }
71 1
        $this->uploadDirectory = $path;
72 1
        return $this;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 1
    public function getUploadDirectory()
79
    {
80 1
        return $this->uploadDirectory;
81
    }
82
83
    /**
84
     * @return bool
85
     */
86 2
    public function hasUploadDirectory()
87
    {
88 2
        return $this->uploadDirectory !== null;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94 2
    public function moveUploadToDestination()
1 ignored issue
show
Coding Style introduced by
moveUploadToDestination uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
95
    {
96 2
        if (!$this->hasUploadDirectory()) {
97 1
            throw new LogicException('No destination directory set using setUploadDirectory($path)');
98
        }
99 1
        $tmp = $_FILES[$this->getName()]['tmp_name'];
100 1
        $destination = $this->getUploadDirectory().DIRECTORY_SEPARATOR.$_FILES[$this->getName()]['name'];
101 1
        $success = move_uploaded_file($tmp, $destination);
102 1
        return $success;
103
    }
104
}