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

CollectionSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 32
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedServices() 0 4 1
A getSubscribedEvents() 0 5 1
A populateCollection() 0 14 3
1
<?php
2
3
namespace Silverback\ApiComponentBundle\EventSubscriber\ApiPlatform;
4
5
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
6
use ApiPlatform\Core\EventListener\EventPriorities;
7
use Silverback\ApiComponentBundle\Entity\Content\Component\Collection\Collection;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
class CollectionSubscriber extends AbstractSubscriber
13
{
14
    public static function getSubscribedEvents(): array
15
    {
16
        return [
17
            KernelEvents::VIEW => [
18
                ['populateCollection', EventPriorities::PRE_SERIALIZE]
19
            ]
20
        ];
21
    }
22
23
    public static function getSubscribedServices(): array
24
    {
25
        return [
26
            '?' . ContextAwareCollectionDataProviderInterface::class
27
        ];
28
    }
29
30
    public function populateCollection(GetResponseForControllerResultEvent $event): void
31
    {
32
        $collectionEntity = $event->getControllerResult();
33
        $method = $event->getRequest()->getMethod();
34
35
        if (!$collectionEntity instanceof Collection || Request::METHOD_GET !== $method) {
36
            return;
37
        }
38
39
        /** @var ContextAwareCollectionDataProviderInterface $dataProvider */
40
        $dataProvider = $this->container->get(ContextAwareCollectionDataProviderInterface::class);
41
        $dataProviderContext = [];
42
        $collection = $dataProvider->getCollection($collectionEntity->getResource(), Request::METHOD_GET, $dataProviderContext);
43
        $collectionEntity->setCollection($collection);
44
    }
45
}
46