Completed
Pull Request — master (#42)
by
unknown
05:15
created

ImageWidth::validate()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8.0155

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 15
cts 16
cp 0.9375
rs 7.1428
cc 8
eloc 16
nc 9
nop 2
crap 8.0155
1
<?php
2
namespace Sirius\Validation\Rule\Upload;
3
4
use Sirius\Validation\Rule\AbstractRule;
5
6
class ImageWidth extends AbstractRule
7
{
8
    const OPTION_MAX = 'max';
9
    const OPTION_MIN = 'min';
10
11
    const MESSAGE = 'The image should be at least {min} pixels wide';
12
13
    const LABELED_MESSAGE = '{label} should be at least {min} pixels wide';
14
15
    protected $options = array(
16
        self::OPTION_MAX => 1000000,
17
        self::OPTION_MIN => 0,
18
    );
19
20 3
    public function validate($value, $valueIdentifier = null)
21
    {
22 3
        $this->value = $value;
23 3
        if (! is_array($value) || ! isset($value['tmp_name'])) {
24
            $this->success = false;
25 3
        } else if(! file_exists($value['tmp_name'])) {
26 2
            if($value['error'] === UPLOAD_ERR_NO_FILE ){
27 1
                $this->success = true;
28 1
            }else{
29 1
                $this->success = false;
30
            }
31 2
        } else {
32 1
            $imageInfo     = getimagesize($value['tmp_name']);
33 1
            $width         = isset($imageInfo[0]) ? $imageInfo[0] : 0;
34 1
            $this->success = $width &&
35 1
                             $width <= $this->options[self::OPTION_MAX] &&
36 1
                             $width >= $this->options[self::OPTION_MIN];
37
        }
38
39 3
        return $this->success;
40
    }
41
}
42