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

CollectionSubscriber::populateCollection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 12
rs 10
c 0
b 0
f 0
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