Completed
Pull Request — master (#2680)
by
unknown
06:45
created

IsExtensionAllowedValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 31
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 16 4
1
<?php
2
3
namespace Kunstmaan\MediaBundle\Validator\Constraints;
4
5
use Kunstmaan\MediaBundle\Helper\MediaManager;
6
use Symfony\Component\Form\Exception\UnexpectedTypeException;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
use Symfony\Component\Translation\TranslatorInterface;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
12
class IsExtensionAllowedValidator extends ConstraintValidator
13
{
14
    /** @var MediaManager */
15
    private $mediaManager;
16
17
    /** @var TranslatorInterface */
18
    private $translator;
19
20
    public function __construct(MediaManager $mediaManager, TranslatorInterface $translator)
21
    {
22
        $this->mediaManager = $mediaManager;
23
        $this->translator = $translator;
24
    }
25
26
    public function validate($value, Constraint $constraint)
27
    {
28
        if (!$constraint instanceof IsExtensionAllowed) {
29
            throw new UnexpectedTypeException($constraint, IsExtensionAllowed::class);
30
        }
31
32
        if (!$value instanceof UploadedFile) {
33
            return;
34
        }
35
36
        if (!$this->mediaManager->isExtensionAllowed($value)) {
37
            $this->context->buildViolation($this->translator->trans($constraint->notAllowedMessage))
38
                ->setCode(IsExtensionAllowed::NOT_ALLOWED)
39
                ->addViolation();
40
        }
41
    }
42
}
43