Completed
Push — master ( 1c14d6...2c9995 )
by Dawid
02:08
created

VersionedViewListener::onKernelView()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 17
cp 0
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 12
nc 4
nop 1
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\EventListener;
6
7
use Spiechu\SymfonyCommonsBundle\Service\ApiVersionProvider;
8
use Spiechu\SymfonyCommonsBundle\Service\VersionedViewInterface;
9
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
10
11
class VersionedViewListener
12
{
13
    /**
14
     * @var ApiVersionProvider
15
     */
16
    protected $apiVersionProvider;
17
18
    /**
19
     * @param ApiVersionProvider $apiVersionProvider
20
     */
21
    public function __construct(ApiVersionProvider $apiVersionProvider)
22
    {
23
        $this->apiVersionProvider = $apiVersionProvider;
24
    }
25
26
    /**
27
     * @param GetResponseForControllerResultEvent $event
28
     */
29
    public function onKernelView(GetResponseForControllerResultEvent $event): void
30
    {
31
        $apiVersion = $this->apiVersionProvider->getApiVersion();
32
33
        if (null === $apiVersion) {
34
            return;
35
        }
36
37
        $controllerResult = $event->getControllerResult();
38
39
        if ($controllerResult instanceof VersionedViewInterface) {
40
            $controllerResult->setVersion($apiVersion);
41
42
            return;
43
        }
44
45
        if (class_exists('FOS\RestBundle\View\View') && $controllerResult instanceof FOS\RestBundle\View\View) {
0 ignored issues
show
Bug introduced by
The class Spiechu\SymfonyCommonsBu...OS\RestBundle\View\View does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46
            $serializationContext = $controllerResult->getSerializationContext();
47
48
            $serializationContext->setVersion($apiVersion);
49
50
            $controllerResult->setSerializationContext($serializationContext);
51
        }
52
    }
53
}
54