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

ImageHeight   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.67%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 17 7
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Validation\Rule\Upload;
4
5
use Sirius\Validation\Rule\AbstractRule;
6
7
class ImageHeight extends AbstractRule
8
{
9
    const OPTION_MAX = 'max';
10
    const OPTION_MIN = 'min';
11
12
    const MESSAGE = 'The file should be at least {min} pixels tall';
13
14
    const LABELED_MESSAGE = '{label} should be at least {min} pixels tall';
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
            $height        = isset($imageInfo[1]) ? $imageInfo[1] : 0;
31 1
            $this->success = $height &&
32 1
                             $height <= $this->options[self::OPTION_MAX] &&
33 1
                             $height >= $this->options[self::OPTION_MIN];
34
        }
35
36 3
        return $this->success;
37
    }
38
}
39