Completed
Push — master ( 8fe45b...3e4cf7 )
by Jeroen
08:37
created

Constraints/HasGuessableExtensionValidator.php (2 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\Helper\ExtensionGuesserFactoryInterface;
6
use Kunstmaan\MediaBundle\Helper\MimeTypeGuesserFactoryInterface;
7
use Symfony\Component\Form\Exception\UnexpectedTypeException;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
12
13
/**
14
 * Class hasGuessableExtensionValidator
15
 */
16
class HasGuessableExtensionValidator extends ConstraintValidator
17
{
18
    /**
19
     * @var ExtensionGuesserInterface
20
     */
21
    private $extensionGuesser;
22
23
    /**
24
     * @var MimeTypeGuesserInterface
25
     */
26
    private $mimeTypeGuesser;
27
28
    /**
29
     * @param $value
30
     * @param Constraint $constraint
31
     *
32
     * @throws ConstraintDefinitionException
33
     * @throws UnexpectedTypeException
34
     */
35
    public function validate($value, Constraint $constraint)
36
    {
37
        if (!$constraint instanceof HasGuessableExtension) {
38
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\HasGuessableExtension');
39
        }
40
41
        if (!$value instanceof UploadedFile) {
42
            return;
43
        }
44
45
        $contentType = $this->mimeTypeGuesser->guess($value->getPathname());
46
        $pathInfo = pathinfo($value->getClientOriginalName());
47
        if (!\array_key_exists('extension', $pathInfo)) {
48
            $pathInfo['extension'] = $this->extensionGuesser->guess($contentType);
49
        }
50
51
        if ($pathInfo['extension'] === null) {
52
            $this->context->buildViolation($constraint->notGuessableErrorMessage)
53
                ->setCode(HasGuessableExtension::NOT_GUESSABLE_ERROR)
54
                ->addViolation();
55
        }
56
    }
57
58
    /**
59
     * @param ExtensionGuesserFactoryInterface $extensionGuesserFactory
60
     */
61
    public function setExtensionGuesser(ExtensionGuesserFactoryInterface $extensionGuesserFactory)
62
    {
63
        $this->extensionGuesser = $extensionGuesserFactory->get();
0 ignored issues
show
Documentation Bug introduced by
It seems like $extensionGuesserFactory->get() of type object<Symfony\Component...ensionGuesserInterface> is incompatible with the declared type object<Kunstmaan\MediaBu...ensionGuesserInterface> of property $extensionGuesser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
    }
65
66
    /**
67
     * @param MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory
68
     */
69
    public function setMimeTypeGuesser(MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory)
70
    {
71
        $this->mimeTypeGuesser = $mimeTypeGuesserFactory->get();
0 ignored issues
show
Documentation Bug introduced by
It seems like $mimeTypeGuesserFactory->get() of type object<Symfony\Component...meTypeGuesserInterface> is incompatible with the declared type object<Kunstmaan\MediaBu...meTypeGuesserInterface> of property $mimeTypeGuesser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
    }
73
}
74