Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

Validator/Constraints/MediaValidator.php (1 issue)

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