Completed
Push — 5.3 ( 89941d...4b705f )
by Jeroen
23:30 queued 17:43
created

MediaValidator::validateDimensions()   C

Complexity

Conditions 13
Paths 46

Size

Total Lines 69

Duplication

Lines 76
Ratio 110.14 %

Code Coverage

Tests 40
CRAP Score 13.1269

Importance

Changes 0
Metric Value
dl 76
loc 69
ccs 40
cts 44
cp 0.9091
rs 5.9696
c 0
b 0
f 0
cc 13
nc 46
nop 2
crap 13.1269

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
2
3
namespace Kunstmaan\MediaBundle\Validator\Constraints;
4
5
use Kunstmaan\MediaBundle\Entity\Media as MediaObject;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
9
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
10
11
class MediaValidator extends ConstraintValidator
12
{
13
    /**
14
     * @param MediaObject $value
15
     * @param Constraint  $constraint
16
     *
17
     * @throws ConstraintDefinitionException
18
     */
19 17
    public function validate($value, Constraint $constraint)
20
    {
21 17
        if (!$constraint instanceof Media) {
22
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Media');
23
        }
24
25 17
        if (!$value instanceof MediaObject) {
26
            return;
27
        }
28
29 17
        $mimeType = $value->getContentType();
30
31 17
        if ($constraint->mimeTypes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $constraint->mimeTypes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32 7
            $mimeTypes = (array) $constraint->mimeTypes;
33
34 7
            if (false === $this->validateMimeType($value, $mimeTypes)) {
35 2
                $this->context->buildViolation($constraint->mimeTypesMessage)
36 2
                    ->setParameter('{{ type }}', $this->formatValue($mimeType))
37 2
                    ->setParameter('{{ types }}', $this->formatValues($mimeTypes))
38 2
                    ->setCode(Media::INVALID_MIME_TYPE_ERROR)
39 2
                    ->addViolation();
40
41 2
                return;
42
            }
43
        }
44
45 15
        if (!preg_match('^image\/*^', $mimeType) || $mimeType === 'image/svg+xml') {
46 2
            return;
47
        }
48
49 13
        $this->validateDimensions($value, $constraint);
50 13
    }
51
52 7
    private function validateMimeType(MediaObject $value, $allowedMimeTypes)
53
    {
54 7
        $mimeType = strtolower($value->getContentType());
55
56 7
        foreach ($allowedMimeTypes as $type) {
57 7
            $type = strtolower($type);
58 7
            if ($type === $mimeType) {
59 4
                return true;
60
            }
61
62 4
            if ($discrete = strstr($type, '/*', true)) {
63 2
                if (strstr($mimeType, '/', true) === $discrete) {
64 1
                    return true;
65
                }
66
            }
67
        }
68
69 2
        return false;
70
    }
71
72 13
    private function validateDimensions(MediaObject $value, Media $constraint)
73
    {
74 13
        $height = $value->getMetadataValue('original_height');
75 13
        $width = $value->getMetadataValue('original_width');
76
77 13 View Code Duplication
        if ($constraint->minHeight) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78 2
            if (!ctype_digit((string) $constraint->minHeight)) {
79
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
80
            }
81
82 2
            if ($height < $constraint->minHeight) {
83 1
                $this->context->buildViolation($constraint->minHeightMessage)
84 1
                    ->setParameter('{{ height }}', $height)
85 1
                    ->setParameter('{{ min_height }}', $constraint->minHeight)
86 1
                    ->setCode(Media::TOO_LOW_ERROR)
87 1
                    ->addViolation();
88
89 1
                return;
90
            }
91
        }
92
93 12 View Code Duplication
        if ($constraint->maxHeight) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94 2
            if (!ctype_digit((string) $constraint->maxHeight)) {
95
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
96
            }
97
98 2
            if ($height > $constraint->maxHeight) {
99 1
                $this->context->buildViolation($constraint->maxHeightMessage)
100 1
                    ->setParameter('{{ height }}', $height)
101 1
                    ->setParameter('{{ max_height }}', $constraint->maxHeight)
102 1
                    ->setCode(Media::TOO_HIGH_ERROR)
103 1
                    ->addViolation();
104
105 1
                return;
106
            }
107
        }
108
109 11 View Code Duplication
        if ($constraint->minWidth) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110 2
            if (!ctype_digit((string) $constraint->minWidth)) {
111
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
112
            }
113
114 2
            if ($width < $constraint->minWidth) {
115 1
                $this->context->buildViolation($constraint->minWidthMessage)
116 1
                    ->setParameter('{{ width }}', $width)
117 1
                    ->setParameter('{{ min_width }}', $constraint->minWidth)
118 1
                    ->setCode(Media::TOO_NARROW_ERROR)
119 1
                    ->addViolation();
120
121 1
                return;
122
            }
123
        }
124
125 10 View Code Duplication
        if ($constraint->maxWidth) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126 2
            if (!ctype_digit((string) $constraint->maxWidth)) {
127
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
128
            }
129
130 2
            if ($width > $constraint->maxWidth) {
131 1
                $this->context->buildViolation($constraint->maxWidthMessage)
132 1
                    ->setParameter('{{ width }}', $width)
133 1
                    ->setParameter('{{ max_width }}', $constraint->maxWidth)
134 1
                    ->setCode(Media::TOO_WIDE_ERROR)
135 1
                    ->addViolation();
136
137 1
                return;
138
            }
139
        }
140 9
    }
141
}
142