Completed
Pull Request — master (#61)
by
unknown
01:35
created

ImageWidth::validate()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 12
cts 13
cp 0.9231
c 0
b 0
f 0
rs 8.8333
cc 7
nc 8
nop 2
crap 7.0222
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
        } elseif (! file_exists($value['tmp_name'])) {
26 2
            $this->success = $value['error'] === UPLOAD_ERR_NO_FILE;
27 2
        } else {
28 1
            $imageInfo     = getimagesize($value['tmp_name']);
29 1
            $width         = isset($imageInfo[0]) ? $imageInfo[0] : 0;
30 1
            $this->success = $width &&
31 1
                             $width <= $this->options[self::OPTION_MAX] &&
32 1
                             $width >= $this->options[self::OPTION_MIN];
33
        }
34
35 3
        return $this->success;
36
    }
37
}
38