ImageSizeEquals::validate()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
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 ImageSizeEquals
13
 * @package kalanis\kw_rules\Rules\File
14
 * Check if input image size equals preset ones
15
 */
16
class ImageSizeEquals extends AFileRule
17
{
18
    use TCheckRange;
19
20 6
    public function validate(IValidateFile $entry): void
21
    {
22 6
        $filename = $entry->getTempName();
23 6
        if (!empty($filename)) {
24 6
            $imageSize = @getimagesize($filename);
25 6
            if (false !== $imageSize) {
26 6
                if (($imageSize[0] == $this->againstValue[0]) && ($imageSize[1] == $this->againstValue[1])) {
27 1
                    return;
28
                }
29
            }
30
        }
31 5
        throw new RuleException($this->errorText);
32
    }
33
34
    /**
35
     * @param mixed|null $againstValue
36
     * @throws RuleException
37
     * @return array<mixed>|mixed|null
38
     */
39 7
    protected function checkValue($againstValue)
40
    {
41 7
        if (!is_array($againstValue)) {
42 1
            throw new RuleException('No array found. Need set both values to compare!');
43
        }
44 6
        return array_map([$this, 'checkRule'], $againstValue);
45
    }
46
}
47