Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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
     *
31
     * @throws ConstraintDefinitionException
32
     * @throws UnexpectedTypeException
33
     */
34
    public function validate($value, Constraint $constraint)
35
    {
36
        if (!$constraint instanceof HasGuessableExtension) {
37
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\HasGuessableExtension');
38
        }
39
40
        if (!$value instanceof UploadedFile) {
41
            return;
42
        }
43
44
        $contentType = $this->mimeTypeGuesser->guess($value->getPathname());
45
        $pathInfo = pathinfo($value->getClientOriginalName());
46
        if (!\array_key_exists('extension', $pathInfo)) {
47
            $pathInfo['extension'] = $this->extensionGuesser->guess($contentType);
48
        }
49
50
        if ($pathInfo['extension'] === null) {
51
            $this->context->buildViolation($constraint->notGuessableErrorMessage)
52
                ->setCode(HasGuessableExtension::NOT_GUESSABLE_ERROR)
53
                ->addViolation();
54
        }
55
    }
56
57
    public function setExtensionGuesser(ExtensionGuesserFactoryInterface $extensionGuesserFactory)
58
    {
59
        $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...
60
    }
61
62
    public function setMimeTypeGuesser(MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory)
63
    {
64
        $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...
65
    }
66
}
67