Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Validator/Constraints/MediaValidator.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     *
16
     * @throws ConstraintDefinitionException
17
     */
18 17
    public function validate($value, Constraint $constraint)
19
    {
20 17
        if (!$constraint instanceof Media) {
21
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Media');
22
        }
23
24 17
        if (!$value instanceof MediaObject) {
25
            return;
26
        }
27
28 17
        $mimeType = $value->getContentType();
29
30 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...
31 7
            $mimeTypes = (array) $constraint->mimeTypes;
32
33 7
            if (false === $this->validateMimeType($value, $mimeTypes)) {
34 2
                $this->context->buildViolation($constraint->mimeTypesMessage)
35 2
                    ->setParameter('{{ type }}', $this->formatValue($mimeType))
36 2
                    ->setParameter('{{ types }}', $this->formatValues($mimeTypes))
37 2
                    ->setCode(Media::INVALID_MIME_TYPE_ERROR)
38 2
                    ->addViolation();
39
40 2
                return;
41
            }
42
        }
43
44 15
        if (!preg_match('^image\/*^', $mimeType) || $mimeType === 'image/svg+xml') {
45 2
            return;
46
        }
47
48 13
        $this->validateDimensions($value, $constraint);
49 13
    }
50
51 7
    private function validateMimeType(MediaObject $value, $allowedMimeTypes)
52
    {
53 7
        $mimeType = strtolower($value->getContentType());
54
55 7
        foreach ($allowedMimeTypes as $type) {
56 7
            $type = strtolower($type);
57 7
            if ($type === $mimeType) {
58 4
                return true;
59
            }
60
61 4
            if ($discrete = strstr($type, '/*', true)) {
62 2
                if (strstr($mimeType, '/', true) === $discrete) {
63 1
                    return true;
64
                }
65
            }
66
        }
67
68 2
        return false;
69
    }
70
71 13
    private function validateDimensions(MediaObject $value, Media $constraint)
72
    {
73 13
        $height = $value->getMetadataValue('original_height');
74 13
        $width = $value->getMetadataValue('original_width');
75
76 13 View Code Duplication
        if ($constraint->minHeight) {
0 ignored issues
show
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...
77 2
            if (!ctype_digit((string) $constraint->minHeight)) {
78
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
79
            }
80
81 2
            if ($height < $constraint->minHeight) {
82 1
                $this->context->buildViolation($constraint->minHeightMessage)
83 1
                    ->setParameter('{{ height }}', $height)
84 1
                    ->setParameter('{{ min_height }}', $constraint->minHeight)
85 1
                    ->setCode(Media::TOO_LOW_ERROR)
86 1
                    ->addViolation();
87
88 1
                return;
89
            }
90
        }
91
92 12 View Code Duplication
        if ($constraint->maxHeight) {
0 ignored issues
show
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...
93 2
            if (!ctype_digit((string) $constraint->maxHeight)) {
94
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
95
            }
96
97 2
            if ($height > $constraint->maxHeight) {
98 1
                $this->context->buildViolation($constraint->maxHeightMessage)
99 1
                    ->setParameter('{{ height }}', $height)
100 1
                    ->setParameter('{{ max_height }}', $constraint->maxHeight)
101 1
                    ->setCode(Media::TOO_HIGH_ERROR)
102 1
                    ->addViolation();
103
104 1
                return;
105
            }
106
        }
107
108 11 View Code Duplication
        if ($constraint->minWidth) {
0 ignored issues
show
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...
109 2
            if (!ctype_digit((string) $constraint->minWidth)) {
110
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
111
            }
112
113 2
            if ($width < $constraint->minWidth) {
114 1
                $this->context->buildViolation($constraint->minWidthMessage)
115 1
                    ->setParameter('{{ width }}', $width)
116 1
                    ->setParameter('{{ min_width }}', $constraint->minWidth)
117 1
                    ->setCode(Media::TOO_NARROW_ERROR)
118 1
                    ->addViolation();
119
120 1
                return;
121
            }
122
        }
123
124 10 View Code Duplication
        if ($constraint->maxWidth) {
0 ignored issues
show
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...
125 2
            if (!ctype_digit((string) $constraint->maxWidth)) {
126
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
127
            }
128
129 2
            if ($width > $constraint->maxWidth) {
130 1
                $this->context->buildViolation($constraint->maxWidthMessage)
131 1
                    ->setParameter('{{ width }}', $width)
132 1
                    ->setParameter('{{ max_width }}', $constraint->maxWidth)
133 1
                    ->setCode(Media::TOO_WIDE_ERROR)
134 1
                    ->addViolation();
135
136 1
                return;
137
            }
138
        }
139 9
    }
140
}
141