UploadValidator::maxDimension()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 9
nc 5
nop 3
1
<?php
2
namespace App\Model\Validation;
3
4
use Cake\Validation\Validator;
5
6
class UploadValidator extends Validator
7
{
8
9
    /**
10
     * Check the dimensions of a file image.
11
     *
12
     * @param array $check  Value to check.
13
     * @param int   $height Max height dimension.
14
     * @param int   $width  Max width dimension.
15
     *
16
     * @return bool
17
     */
18
    public static function maxDimension($check, $height, $width)
19
    {
20
        if (is_array($check) && isset($check['error']) && (int)$check['error'] === UPLOAD_ERR_NO_FILE) {
21
            return true;
22
        }
23
24
        if (!is_file($check['tmp_name'])) {
25
            return false;
26
        }
27
28
        //@codingStandardsIgnoreStart
29
        $size = @getimagesize($check['tmp_name']);
30
        //@codingStandardsIgnoreEnd
31
32
        if (!is_array($size)) {
33
            return false;
34
        }
35
36
        return ($size[0] <= (int)$width && $size[1] <= (int)$height);
37
    }
38
}
39