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

HasGuessableExtensionValidator::guessMimeType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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\MimeType\ExtensionGuesserInterface;
9
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use Symfony\Component\Mime\MimeTypes;
12
use Symfony\Component\Validator\Constraint;
13
use Symfony\Component\Validator\ConstraintValidator;
14
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15
16
/**
17
 * Class hasGuessableExtensionValidator
18
 */
19
class HasGuessableExtensionValidator extends ConstraintValidator
20
{
21
    /**
22
     * @var ExtensionGuesserInterface
23
     */
24
    private $extensionGuesser;
25
26
    /**
27
     * @var MimeTypeGuesserInterface
28
     */
29
    private $mimeTypeGuesser;
30
31
    /** @var MimeTypes */
32
    private $mimeTypes;
33
34
    public function __construct($mimeTypes = null)
35
    {
36
        if (null === $mimeTypes) {
37
            @trigger_error(sprintf('Not passing an instance of "%s" as the first argument of "%s" is deprecated in KunstmaanMediaBundle 5.7 and will be required in KunstmaanMediaBundle 6.0.', MimeTypes::class, __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
38
        }
39
40
        $this->mimeTypes = $mimeTypes;
41
    }
42
43
    /**
44
     * @param            $value
45
     * @param Constraint $constraint
46
     *
47
     * @throws ConstraintDefinitionException
48
     * @throws UnexpectedTypeException
49
     */
50
    public function validate($value, Constraint $constraint)
51
    {
52
        if (!$constraint instanceof HasGuessableExtension) {
53
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\HasGuessableExtension');
54
        }
55
56
        if (!$value instanceof UploadedFile) {
57
            return;
58
        }
59
60
        $contentType = $this->guessMimeType($value->getPathname());
61
        $pathInfo = pathinfo($value->getClientOriginalName());
62
        if (!\array_key_exists('extension', $pathInfo)) {
63
            $pathInfo['extension'] = $this->getExtension($contentType);
64
        }
65
66
        if ($pathInfo['extension'] === null) {
67
            $this->context->buildViolation($constraint->notGuessableErrorMessage)
68
                ->setCode(HasGuessableExtension::NOT_GUESSABLE_ERROR)
69
                ->addViolation();
70
        }
71
    }
72
73
    private function guessMimeType($pathName)
74
    {
75
        if ($this->mimeTypeGuesser !== null) {
76
            return $this->mimeTypeGuesser->guess($pathName);
77
        }
78
79
        return $this->mimeTypes->guessMimeType($pathName);
80
    }
81
82 View Code Duplication
    private function getExtension($mimeType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if ($this->extensionGuesser !== null) {
85
            return $this->extensionGuesser->guess($mimeType);
86
        }
87
88
        return $this->mimeTypes->getExtensions($mimeType)[0] ?? null;
89
    }
90
91
    /**
92
     * @deprecated This method is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0.
93
     *
94
     * @param ExtensionGuesserFactoryInterface $extensionGuesserFactory
95
     */
96
    public function setExtensionGuesser(ExtensionGuesserFactoryInterface $extensionGuesserFactory)
97
    {
98
        @trigger_error(sprintf('Calling the "%s" method is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. Inject the required services through the constructor instead.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99
100
        $this->extensionGuesser = $extensionGuesserFactory->get();
101
    }
102
103
    /**
104
     * @deprecated This method is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0.
105
     *
106
     * @param MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory
107
     */
108
    public function setMimeTypeGuesser(MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory)
109
    {
110
        @trigger_error(sprintf('Calling the "%s" method is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. Inject the required services through the constructor instead.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
111
112
        $this->mimeTypeGuesser = $mimeTypeGuesserFactory->get();
113
    }
114
}
115