Completed
Push — develop ( f05cec...11f198 )
by Daniel
07:33
created

DynamicPageSubscriber::getSubscribedServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\EventSubscriber\ApiPlatform;
4
5
use ApiPlatform\Core\EventListener\EventPriorities;
6
use Silverback\ApiComponentBundle\Entity\Content\Dynamic\AbstractDynamicPage;
7
use Silverback\ApiComponentBundle\Repository\ComponentLocationRepository;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
class DynamicPageSubscriber extends AbstractSubscriber
13
{
14
    public static function getSubscribedEvents(): array
15
    {
16
        return [
17
            KernelEvents::VIEW => [
18
                ['setComponentLocations', EventPriorities::PRE_SERIALIZE]
19
            ]
20
        ];
21
    }
22
23
    public static function getSubscribedServices(): array
24
    {
25
        return [
26
            '?' . ComponentLocationRepository::class
27
        ];
28
    }
29
30
    public function setComponentLocations(GetResponseForControllerResultEvent $event): void
31
    {
32
        $page = $event->getControllerResult();
33
        $method = $event->getRequest()->getMethod();
34
35
        if (!$page instanceof AbstractDynamicPage || Request::METHOD_GET !== $method) {
36
            return;
37
        }
38
39
        /** @var ComponentLocationRepository $repository */
40
        $repository = $this->container->get(ComponentLocationRepository::class);
41
        $locations = $repository->findByDynamicPage($page);
42
        if ($locations) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locations of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            $page->setComponentLocations($locations);
44
        }
45
    }
46
}
47