Completed
Push — master ( d45ca1...554243 )
by Adrian
01:42
created

ImageWidth::validate()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.0283

Importance

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