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); |
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) |
|
|
|
|
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); |
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); |
111
|
|
|
|
112
|
|
|
$this->mimeTypeGuesser = $mimeTypeGuesserFactory->get(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.