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

FileSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 31
ccs 0
cts 13
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 populateFileData() 0 13 3
1
<?php
2
3
namespace Silverback\ApiComponentBundle\EventSubscriber\ApiPlatform;
4
5
use ApiPlatform\Core\EventListener\EventPriorities;
6
use Silverback\ApiComponentBundle\Entity\Content\FileInterface;
7
use Silverback\ApiComponentBundle\Factory\FileDataFactory;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
class FileSubscriber extends AbstractSubscriber
13
{
14
    public static function getSubscribedEvents(): array
15
    {
16
        return [
17
            KernelEvents::VIEW => [
18
                ['populateFileData', EventPriorities::PRE_SERIALIZE]
19
            ]
20
        ];
21
    }
22
23
    public static function getSubscribedServices(): array
24
    {
25
        return [
26
            '?' .  FileDataFactory::class
27
        ];
28
    }
29
30
    public function populateFileData(GetResponseForControllerResultEvent $event): void
31
    {
32
        $component = $event->getControllerResult();
33
        $method = $event->getRequest()->getMethod();
34
35
        if (!($component instanceof FileInterface || Request::METHOD_GET !== $method)) {
36
            return;
37
        }
38
39
        /** @var FileDataFactory $factory */
40
        $factory = $this->container->get(FileDataFactory::class);
41
        $fileData = $factory->create($component);
42
        $component->setFileData($fileData);
43
    }
44
}
45