Completed
Pull Request — master (#1)
by Joao Gilberto
04:49
created

UploadedFiles::getUploadedFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * User: jg
4
 * Date: 09/02/17
5
 * Time: 12:01
6
 */
7
8
namespace ByJG\RestServer;
9
10
11
class UploadedFiles
12
{
13
    public function count()
0 ignored issues
show
Coding Style introduced by Joao Gilberto Magalhaes
count 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...
14
    {
15
        return count($_FILES);
16
    }
17
18
    public function getKeys()
0 ignored issues
show
Coding Style introduced by Joao Gilberto Magalhaes
getKeys 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...
19
    {
20
        return array_keys($_FILES);
21
    }
22
23
    public function isOk($key)
24
    {
25
        return $this->getFileByKey($key, 'error');
26
    }
27
28
    public function getUploadedFile($key)
29
    {
30
        return file_get_contents($this->getFileByKey($key, 'tmp_name'));
31
    }
32
33
    public function getFileName($key)
34
    {
35
        return $this->getFileByKey($key, 'name');
36
    }
37
38
    public function getFileType($key)
39
    {
40
        return $this->getFileByKey($key, 'type');
41
    }
42
43
    public function saveTo($key, $destinationPath, $newName = "")
44
    {
45
        if (empty($newName)) {
46
            $newName = $this->getFileName($key);
47
        }
48
49
        move_uploaded_file($this->getFileByKey($key, 'tmp_name'), $destinationPath . '/' . $newName);
50
    }
51
52
    public function clearTemp($key)
53
    {
54
        unlink($this->getFileByKey($key, 'tmp_name'));
55
    }
56
57
    private function getFileByKey($key, $property)
0 ignored issues
show
Coding Style introduced by Joao Gilberto Magalhaes
getFileByKey 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...
58
    {
59
        if (!isset($_FILES[$key])) {
60
            throw new \InvalidArgumentException("The upload '$key' does not exists");
61
        }
62
63
        return $_FILES[$key][$property];
64
    }
65
}