AbstractImageValidator::isValid()   D
last analyzed

Complexity

Conditions 19
Paths 127

Size

Total Lines 40
Code Lines 20

Duplication

Lines 30
Ratio 75 %

Importance

Changes 0
Metric Value
dl 30
loc 40
rs 4.6463
c 0
b 0
f 0
cc 19
eloc 20
nc 127
nop 3

How to fix   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
2
3
/**
4
 * Copyright 2014 Jonathan Bouzekri. All rights reserved.
5
 *
6
 * @copyright Copyright 2014 Jonathan Bouzekri <[email protected]>
7
 * @license https://github.com/jbouzekri/FileUploaderBundle/blob/master/LICENSE
8
 * @link https://github.com/jbouzekri/FileUploaderBundle
9
 */
10
11
namespace Jb\Bundle\FileUploaderBundle\Service\Validator;
12
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
use Jb\Bundle\FileUploaderBundle\Exception\ValidationException;
15
16
/**
17
 * AbstractImageValidator
18
 *
19
 * @author jobou
20
 */
21
abstract class AbstractImageValidator extends AbstractValidator
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function configureOptions(OptionsResolver $resolver)
27
    {
28
        $resolver->setDefined(array(
29
            'MinWidth',
30
            'MaxWidth',
31
            'MinHeight',
32
            'MaxHeight',
33
            'MinRatio',
34
            'MaxRatio'
35
        ));
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function validate($value, array $configuration)
42
    {
43
        // No configuration. Do nothing
44
        if (!isset($configuration['MinWidth']) && !isset($configuration['MaxWidth'])
45
            && !isset($configuration['MinHeight']) && !isset($configuration['MaxHeight'])
46
            && !isset($configuration['MinRatio']) && !isset($configuration['MaxRatio'])
47
        ) {
48
            return;
49
        }
50
51
        // Extract width height from value
52
        $size = $this->extractWidthHeight($value);
53
54
        // Validate
55
        $this->isValid($size['width'], $size['height'], $configuration);
56
    }
57
58
    /**
59
     * Extract width and height from value
60
     *
61
     * @return array associative with key width and height
62
     */
63
    abstract protected function extractWidthHeight($value);
64
65
    /**
66
     * Common image/crop validator check
67
     *
68
     * @param int $width
69
     * @param int $height
70
     * @param array $configuration
71
     *
72
     * @throws ValidationException
73
     */
74
    protected function isValid($width, $height, array $configuration)
75
    {
76 View Code Duplication
        if (isset($configuration['MinWidth']) && $this->validateConfig('MinWidth', $configuration)) {
77
            if ($width < $configuration['MinWidth']) {
78
                throw new ValidationException('Minimum width must be '.$configuration['MinWidth'].'px');
79
            }
80
        }
81
82 View Code Duplication
        if (isset($configuration['MaxWidth']) && $this->validateConfig('MaxWidth', $configuration)) {
83
            if ($width > $configuration['MaxWidth']) {
84
                throw new ValidationException('Maximum width must be '.$configuration['MaxWidth'].'px');
85
            }
86
        }
87
88 View Code Duplication
        if (isset($configuration['MinHeight']) && $this->validateConfig('MinHeight', $configuration)) {
89
            if ($height < $configuration['MinHeight']) {
90
                throw new ValidationException('Minimum height must be '.$configuration['MinHeight'].'px');
91
            }
92
        }
93
94 View Code Duplication
        if (isset($configuration['MaxHeight']) && $this->validateConfig('MaxHeight', $configuration)) {
95
            if ($height > $configuration['MaxHeight']) {
96
                throw new ValidationException('Minimum height must be '.$configuration['MaxHeight'].'px');
97
            }
98
        }
99
100
        $ratio = round($width / $height, 2);
101
102 View Code Duplication
        if (isset($configuration['MinRatio']) && $this->validateConfig('MinRatio', $configuration, true)) {
103
            if ($ratio < $configuration['MinRatio']) {
104
                throw new ValidationException('Minimum ratio must be '.$configuration['MinRatio']);
105
            }
106
        }
107
108 View Code Duplication
        if (isset($configuration['MaxRatio']) && $this->validateConfig('MaxRatio', $configuration, true)) {
109
            if ($ratio > $configuration['MaxRatio']) {
110
                throw new ValidationException('Maximum ratio must be '.$configuration['MaxRatio']);
111
            }
112
        }
113
    }
114
115
    /**
116
     * Validate configuration value
117
     *
118
     * @param string $key
119
     * @param array $configuration
120
     * @param bool $isFloat
121
     *
122
     * @return bool
123
     *
124
     * @throws ValidationException
125
     */
126
    protected function validateConfig($key, array $configuration, $isFloat = false)
127
    {
128
        if (!$isFloat && !ctype_digit((string) $configuration[$key])) {
129
            throw new ValidationException(sprintf('"%s" is not a valid %s configuration', $configuration[$key], $key));
130
        }
131
132
        if ($isFloat && !is_numeric((string) $configuration[$key])) {
133
            throw new ValidationException(sprintf('"%s" is not a valid %s configuration', $configuration[$key], $key));
134
        }
135
136
        return true;
137
    }
138
}
139