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

ImageWidth   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 12
cts 13
cp 0.9231
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 17 7
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