Passed
Push — master ( a09a70...116340 )
by Bruno
08:54
created

Image::dimension()   C

Complexity

Conditions 17
Paths 10

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 34
c 1
b 0
f 0
nc 10
nop 3
dl 0
loc 56
ccs 0
cts 31
cp 0
crap 306
rs 5.2166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Formularium\Validator;
4
5
use Formularium\Datatype;
6
use Formularium\Exception\ValidatorException;
7
use Formularium\Model;
8
use Formularium\ValidatorArgs;
9
use Formularium\ValidatorInterface;
10
use Formularium\ValidatorMetadata;
11
12
/**
13
 * Image file validation
14
 */
15
class Image implements ValidatorInterface
16
{
17
    const DIMENSION_WIDTH = 'dimensionWidth';
18
    const DIMENSION_HEIGHT = 'dimensionHeight';
19
    const DIMENSION_MIN_WIDTH = 'dimensionMinWidth';
20
    const DIMENSION_MAX_WIDTH = 'dimensionMaxWidth';
21
    const DIMENSION_MIN_HEIGHT = 'dimensionMinHeight';
22
    const DIMENSION_MAX_HEIGHT = 'dimensionMaxHeight';
23
    const DIMENSION_RATIO = 'dimensionRatio';
24
25
    protected static function dimension(array $options = [], int $width, int $height): void
26
    {
27
        $expectedHeight = $options[self::DIMENSION_HEIGHT] ?? false;
28
        if ($expectedHeight !== false && $expectedHeight !== $height) {
29
            throw new ValidatorException(
30
                'Image height should be exactly ' . $expectedHeight
31
            );
32
        }
33
34
        $expectedWidth = $options[self::DIMENSION_WIDTH] ?? false;
35
        if ($expectedWidth !== false && $expectedWidth !== $width) {
36
            throw new ValidatorException(
37
                'Image width should be exactly ' . $expectedWidth
38
            );
39
        }
40
41
        $minHeight = $options[self::DIMENSION_MIN_HEIGHT] ?? false;
42
        if ($minHeight !== false && $height < $minHeight) {
43
            throw new ValidatorException(
44
                'Image height should be at least ' . $minHeight
45
            );
46
        }
47
48
        $minWidth = $options[self::DIMENSION_MIN_WIDTH] ?? false;
49
        if ($minWidth !== false && $width < $minWidth) {
50
            throw new ValidatorException(
51
                'Image width should be at least ' . $minWidth
52
            );
53
        }
54
55
        $maxHeight = $options[self::DIMENSION_MAX_HEIGHT] ?? false;
56
        if ($maxHeight !== false && $height > $maxHeight) {
57
            throw new ValidatorException(
58
                'Image height should be at most ' . $maxHeight
59
            );
60
        }
61
62
        $maxWidth = $options[self::DIMENSION_MAX_WIDTH] ?? false;
63
        if ($maxWidth !== false && $width > $maxWidth) {
64
            throw new ValidatorException(
65
                'Image width should be at most ' . $maxWidth
66
            );
67
        }
68
69
        $ratio = $options[self::DIMENSION_RATIO] ?? false;
70
        if ($ratio !== false) {
71
            if (!$width || !$height) {
72
                throw new ValidatorException(
73
                    'Zero width or height'
74
                );
75
            }
76
            $ratio = $width/$height;
77
            $expected = $ratio;
78
            if (abs(($ratio-$expected)/$expected) > 0.0001) {
79
                throw new ValidatorException(
80
                    'Image width/height ratio should be ' . $ratio
81
                );
82
            }
83
        }
84
    }
85
86
    public static function validate($value, array $options = [], Datatype $datatype, ?Model $model = null)
87
    {
88
        if ($datatype->getBasetype() !== 'file') {
89
            throw new ValidatorException(
90
                'Not a file'
91
            );
92
        }
93
    
94
        $imageData = getimagesize($value);
95
        if ($imageData === false) {
96
            throw new ValidatorException(
97
                'Not an image'
98
            );
99
        }
100
101
        if (($options[self::DIMENSION_HEIGHT] ?? false) ||
102
                ($options[self::DIMENSION_WIDTH] ?? false) ||
103
                ($options[self::DIMENSION_MIN_HEIGHT] ?? false) ||
104
                ($options[self::DIMENSION_MIN_WIDTH] ?? false) ||
105
                ($options[self::DIMENSION_MAX_HEIGHT] ?? false) ||
106
                ($options[self::DIMENSION_MAX_WIDTH] ?? false) ||
107
                ($options[self::DIMENSION_RATIO] ?? false)
108
            ) {
109
            $width = $imageData[0];
110
            $height = $imageData[1];
111
            static::dimension($options, $width, $height);
112
        }
113
        return $value;
114
    }
115
116
    public static function getMetadata(): ValidatorMetadata
117
    {
118
        return new ValidatorMetadata(
119
            'Image',
120
            "Image file validations.",
121
            [
122
                new ValidatorArgs(
123
                    self::DIMENSION_RATIO,
124
                    'Float',
125
                    'The expected ratio (width/height)'
126
                )
127
                // TODO: missing args
128 1
            ]
129
        );
130 1
    }
131
}
132