Upload::__invoke()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 2
crap 4
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule\Validate;
10
11
/**
12
 *
13
 * Validates that the value is an array of file-upload information, and
14
 * if a file is referred to, that is actually an uploaded file.
15
 *
16
 * @package Aura.Filter
17
 *
18
 */
19
class Upload
20
{
21
    /**
22
     *
23
     * Validates that the value is an array of file-upload information, and
24
     * if a file is referred to, that is actually an uploaded file.
25
     *
26
     * The required keys are 'error', 'name', 'size', 'tmp_name', 'type'. More
27
     * or fewer or different keys than this will return a "malformed" error.
28
     *
29
     * @param object $subject The subject to be filtered.
30
     *
31
     * @param string $field The subject field name.
32
     *
33
     * @return bool True if valid, false if not.
34
     *
35
     */
36 6
    public function __invoke($subject, $field)
37
    {
38 6
        $value = $subject->$field;
39
40 6
        $well_formed = $this->preCheck($value);
41 6
        if (! $well_formed) {
42 2
            return false;
43
        }
44
45
        // was the upload explicitly ok?
46 4
        $err = $value['error'];
47 4
        if ($err != UPLOAD_ERR_OK) {
48 2
            return false;
49
        }
50
51
        // is it actually an uploaded file?
52 2
        if (! $this->isUploadedFile($value['tmp_name'])) {
53 1
            return false;
54
        }
55
56
        // looks like we're ok!
57 1
        return true;
58
    }
59
60
    /**
61
     *
62
     * Check that the file-upload array is well-formed.
63
     *
64
     * @param array $value The file-upload array.
65
     *
66
     * @return bool
67
     *
68
     */
69 6
    protected function preCheck(&$value)
70
    {
71
        // has to be an array
72 6
        if (! is_array($value)) {
73 1
            return false;
74
        }
75
76
        // presorted list of expected keys
77 5
        $expect = array('error', 'name', 'size', 'tmp_name', 'type');
78
79
        // remove unexpected keys
80 5
        foreach ($value as $key => $val) {
81 5
            if (! in_array($key, $expect)) {
82 5
                unset($value[$key]);
83
            }
84
        }
85
86
        // sort the list of remaining actual keys
87 5
        $actual = array_keys($value);
88 5
        sort($actual);
89
90
        // make sure the expected and actual keys match up
91 5
        if ($expect != $actual) {
92 1
            return false;
93
        }
94
95
        // looks ok
96 4
        return true;
97
    }
98
99
    /**
100
     *
101
     * Check whether the file was uploaded via HTTP POST.
102
     *
103
     * @param string $file The file to check.
104
     *
105
     * @return bool True if the file was uploaded via HTTP POST, false if not.
106
     *
107
     */
108 2
    protected function isUploadedFile($file)
109
    {
110 2
        return is_uploaded_file($file);
111
    }
112
}
113