1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Listener; |
6
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Annotation\Command; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
12
|
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; |
13
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
14
|
|
|
use Symfony\Component\Serializer\ExceptionInterface; |
|
|
|
|
15
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
|
|
|
16
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
|
|
|
17
|
|
|
|
18
|
|
|
class CommandListener implements EventSubscriberInterface |
19
|
|
|
{ |
20
|
|
|
private const CONTENT_TYPE = 'json'; |
21
|
|
|
|
22
|
|
|
private $serializer; |
23
|
|
|
private $validator; |
24
|
|
|
|
25
|
|
|
public function __construct(SerializerInterface $serializer, ValidatorInterface $validator = null) |
26
|
|
|
{ |
27
|
|
|
$this->serializer = $serializer; |
28
|
|
|
$this->validator = $validator; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function getSubscribedEvents() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
KernelEvents::CONTROLLER => ['onKernelController', -16], |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws UnprocessableEntityHttpException |
40
|
|
|
* @throws BadRequestHttpException |
41
|
|
|
* @throws RuntimeException |
42
|
|
|
*/ |
43
|
|
|
public function onKernelController(FilterControllerEvent $event) |
44
|
|
|
{ |
45
|
|
|
$request = $event->getRequest(); |
46
|
|
|
|
47
|
|
|
if (!$request->attributes->get('_command')) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (self::CONTENT_TYPE !== $request->getContentType()) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @var Command $config */ |
56
|
|
|
$config = $request->attributes->get('_command'); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$command = $this->serializer->deserialize($request->getContent(), $config->className(), self::CONTENT_TYPE); |
60
|
|
|
} catch (ExceptionInterface $e) { |
61
|
|
|
throw new UnprocessableEntityHttpException('Invalid json.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($config->validate()) { |
65
|
|
|
if (!$this->validator) { |
66
|
|
|
throw new RuntimeException('Validator package is not installed.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($validator->validate($command) as $error) { |
|
|
|
|
70
|
|
|
throw new BadRequestHttpException(sprintf('%s: %s', $error->getPropertyPath(), $error->getMessage())); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$request->attributes->set('command', $command); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths