Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13: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
    /**
20
     * @var ExtensionGuesserInterface $extensionGuesser
21
     */
22
    private $extensionGuesser;
23
24
    /**
25
     * @var MimeTypeGuesserInterface $mimeTypeGuesser
26
     */
27
    private $mimeTypeGuesser;
28
29
    /**
30
     * @param $value
31
     * @param Constraint  $constraint
32
     *
33
     * @throws ConstraintDefinitionException
34
     * @throws UnexpectedTypeException
35
     */
36
    public function validate($value, Constraint $constraint)
37
    {
38
        if (!$constraint instanceof HasGuessableExtension) {
39
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\HasGuessableExtension');
40
        }
41
42
        if (!$value instanceof UploadedFile) {
43
            return;
44
        }
45
46
        $contentType = $this->mimeTypeGuesser->guess($value->getPathname());
47
        $pathInfo = pathinfo($value->getClientOriginalName());
48
        if (!array_key_exists('extension', $pathInfo)) {
49
            $pathInfo['extension'] = $this->extensionGuesser->guess($contentType);
50
        }
51
52
        if ($pathInfo['extension'] === NULL) {
53
            $this->context->buildViolation($constraint->notGuessableErrorMessage)
54
                ->setCode(HasGuessableExtension::NOT_GUESSABLE_ERROR)
55
                ->addViolation();
56
        }
57
    }
58
59
    /**
60
     * @param ExtensionGuesserFactoryInterface $extensionGuesserFactory
61
     */
62
    public function setExtensionGuesser(ExtensionGuesserFactoryInterface $extensionGuesserFactory)
63
    {
64
        $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...
65
    }
66
67
    /**
68
     * @param MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory
69
     */
70
    public function setMimeTypeGuesser(MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory)
71
    {
72
        $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...
73
    }
74
}
75