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

PageSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
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\Page;
7
use Silverback\ApiComponentBundle\Repository\LayoutRepository;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
class PageSubscriber extends AbstractSubscriber
13
{
14
    public static function getSubscribedEvents(): array
15
    {
16
        return [
17
            KernelEvents::VIEW => [
18
              ['provideDefaultLayout', EventPriorities::PRE_SERIALIZE]
19
            ]
20
        ];
21
    }
22
23
    public static function getSubscribedServices(): array
24
    {
25
        return [
26
            '?' . LayoutRepository::class
27
        ];
28
    }
29
30
    public function provideDefaultLayout(GetResponseForControllerResultEvent $event): void
31
    {
32
        $page = $event->getControllerResult();
33
        $method = $event->getRequest()->getMethod();
34
35
        if (!$page instanceof Page || Request::METHOD_GET !== $method || $page->getLayout()) {
36
            return;
37
        }
38
39
        /** @var LayoutRepository $repository */
40
        $repository = $this->container->get(LayoutRepository::class);
41
        $page->setLayout($repository->findOneBy(['default' => true]));
42
    }
43
}
44