Completed
Push — master ( 173f5d...31a9ea )
by
unknown
15:41 queued 11s
created

ImageUploadDimensionValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Validator\Constraints;
15
16
use Imagine\Image\ImagineInterface;
17
use Sonata\MediaBundle\Model\MediaInterface;
18
use Sonata\MediaBundle\Provider\ImageProvider;
19
use Symfony\Component\Validator\Constraint;
20
use Symfony\Component\Validator\ConstraintValidator;
21
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
22
23
final class ImageUploadDimensionValidator extends ConstraintValidator
24
{
25
    /**
26
     * @var ImagineInterface
27
     */
28
    private $imagineAdapter;
29
30
    /**
31
     * @var ImageProvider
32
     */
33
    private $imageProvider;
34
35
    public function __construct(ImagineInterface $imagineAdapter, ImageProvider $imageProvider)
36
    {
37
        $this->imagineAdapter = $imagineAdapter;
38
        $this->imageProvider = $imageProvider;
39
    }
40
41
    public function validate($value, Constraint $constraint)
42
    {
43
        if (!$constraint instanceof ImageUploadDimension) {
44
            throw new UnexpectedTypeException($constraint, ImageUploadDimension::class);
45
        }
46
47
        if (!$value instanceof MediaInterface) {
48
            throw new UnexpectedTypeException($value, MediaInterface::class);
49
        }
50
51
        if (null === $value->getBinaryContent()) {
52
            return;
53
        }
54
55
        $minWidth = 0;
56
        $minHeight = 0;
57
58
        foreach ($this->imageProvider->getFormatsForContext($value->getContext()) as $format) {
59
            if (!$format['constraint']) {
60
                continue;
61
            }
62
63
            $minWidth = max($minWidth, $format['width'] ?? 0);
64
            $minHeight = max($minHeight, $format['height'] ?? 0);
65
        }
66
67
        if (0 === $minWidth && 0 === $minHeight) {
68
            return;
69
        }
70
71
        try {
72
            $image = $this->imagineAdapter->open($value->getBinaryContent()->getPathname());
73
        } catch (\RuntimeException $e) {
74
            // Do nothing. The parent validator will throw a violation error.
75
            return;
76
        }
77
78
        $size = $image->getSize();
79
80
        if ($size->getWidth() < $minWidth || $size->getHeight() < $minHeight) {
81
            $this->context
82
                ->buildViolation($constraint->message, [
83
                    '%min_width%' => $minWidth,
84
                    '%min_height%' => $minHeight,
85
                ])
86
                ->setTranslationDomain('SonataMediaBundle')
87
                ->atPath('binaryContent')
88
                ->addViolation();
89
        }
90
    }
91
}
92