ImageSizeMax::validate()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 7
rs 8.8333
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 ImageSizeMax
13
 * @package kalanis\kw_rules\Rules\File
14
 * Check if input image size is up to preset ones
15
 * Zero is for unlimited
16
 */
17
class ImageSizeMax 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