GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 124e4f...793085 )
by Dmitri
02:20
created

CommandListener   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 4 1
C onKernelController() 0 32 7
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Serializer\ExceptionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\Serializer\SerializerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Serializer\SerializerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Valida...ator\ValidatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $validator seems to be never defined.
Loading history...
70
                throw new BadRequestHttpException(sprintf('%s: %s', $error->getPropertyPath(), $error->getMessage()));
71
            }
72
        }
73
74
        $request->attributes->set('command', $command);
75
    }
76
}
77