Completed
Push — master ( e6c0c9...d841f8 )
by Jeroen
35:52 queued 19:21
created

Validator/Constraints/MediaValidator.php (4 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
     * @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) {
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
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(
80
                    sprintf(
81
                        '"%s" is not a valid minimum height',
82
                        $constraint->minHeight
83
                    )
84
                );
85
            }
86
87 2
            if ($height < $constraint->minHeight) {
88 1
                $this->context->buildViolation($constraint->minHeightMessage)
89 1
                    ->setParameter('{{ height }}', $height)
90 1
                    ->setParameter('{{ min_height }}', $constraint->minHeight)
91 1
                    ->setCode(Media::TOO_LOW_ERROR)
92 1
                    ->addViolation();
93
94 1
                return;
95
            }
96
        }
97
98 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...
99 2
            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 2
            if ($height > $constraint->maxHeight) {
109 1
                $this->context->buildViolation($constraint->maxHeightMessage)
110 1
                    ->setParameter('{{ height }}', $height)
111 1
                    ->setParameter('{{ max_height }}', $constraint->maxHeight)
112 1
                    ->setCode(Media::TOO_HIGH_ERROR)
113 1
                    ->addViolation();
114
115 1
                return;
116
            }
117
        }
118
119 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...
120 2
            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 2
            if ($width < $constraint->minWidth) {
130 1
                $this->context->buildViolation($constraint->minWidthMessage)
131 1
                    ->setParameter('{{ width }}', $width)
132 1
                    ->setParameter('{{ min_width }}', $constraint->minWidth)
133 1
                    ->setCode(Media::TOO_NARROW_ERROR)
134 1
                    ->addViolation();
135
136 1
                return;
137
            }
138
        }
139
140 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...
141 2
            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 2
            if ($width > $constraint->maxWidth) {
151 1
                $this->context->buildViolation($constraint->maxWidthMessage)
152 1
                    ->setParameter('{{ width }}', $width)
153 1
                    ->setParameter('{{ max_width }}', $constraint->maxWidth)
154 1
                    ->setCode(Media::TOO_WIDE_ERROR)
155 1
                    ->addViolation();
156
157 1
                return;
158
            }
159
        }
160 9
    }
161
}
162