ImageSizeMin   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 33
ccs 13
cts 13
cp 1
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 16 7
A checkValue() 0 6 2
1
<?php
2
3
namespace kalanis\kw_rules\Rules\File;
4
5
6
use kalanis\kw_rules\Interfaces\IValidateFile;
7
use kalanis\kw_rules\Exceptions\RuleException;
8
use kalanis\kw_rules\Rules\TCheckRange;
9
10
11
/**
12
 * Class ImageSizeMin
13
 * @package kalanis\kw_rules\Rules\File
14
 * Check if input image size is at least as preset ones
15
 * Zero is for unlimited
16
 */
17
class ImageSizeMin extends AFileRule
18
{
19
    use TCheckRange;
20
21 6
    public function validate(IValidateFile $entry): void
22
    {
23 6
        $filename = $entry->getTempName();
24 6
        if (!empty($filename)) {
25 6
            $imageSize = @getimagesize($filename);
26 6
            if (false !== $imageSize) {
27
                if (
28 6
                    (0 === $this->againstValue[0] || $imageSize[0] >= $this->againstValue[0])
29 6
                    && (0 === $this->againstValue[1] || $imageSize[1] >= $this->againstValue[1])
30
                ) {
31 3
                    return;
32
                }
33
            }
34
        }
35
36 3
        throw new RuleException($this->errorText);
37
    }
38
39
    /**
40
     * @param mixed|null $againstValue
41
     * @throws RuleException
42
     * @return array<mixed>|mixed|null
43
     */
44 7
    protected function checkValue($againstValue)
45
    {
46 7
        if (!is_array($againstValue)) {
47 1
            throw new RuleException('No array found. Need set both values to compare!');
48
        }
49 6
        return array_map([$this, 'checkRule'], $againstValue);
50
    }
51
}
52