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

PageSubscriber   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 30
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedServices() 0 4 1
A getSubscribedEvents() 0 5 1
A provideDefaultLayout() 0 12 4
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