Completed
Push — develop ( 99c8cd...370699 )
by Baptiste
02:34
created

TranslateResponseListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 32
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 6 1
A translate() 0 12 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\EventListener;
5
6
use Innmind\Rest\ServerBundle\Translator\ResponseTranslator;
7
use Innmind\Http\Message\ResponseInterface;
8
use Symfony\Component\{
9
    EventDispatcher\EventSubscriberInterface,
10
    HttpKernel\KernelEvents,
11
    HttpKernel\Event\GetResponseForControllerResultEvent
12
};
13
14
final class TranslateResponseListener implements EventSubscriberInterface
15
{
16
    private $translator;
17
18
    public function __construct(ResponseTranslator $translator)
19
    {
20
        $this->translator = $translator;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public static function getSubscribedEvents()
27
    {
28
        return [
29
            KernelEvents::VIEW => [['translate', -255]],
30
        ];
31
    }
32
33
    public function translate(GetResponseForControllerResultEvent $event)
34
    {
35
        $data = $event->getControllerResult();
36
37
        if (!$data instanceof ResponseInterface) {
38
            return;
39
        }
40
41
        $event->setResponse(
42
            $this->translator->translate($data)
43
        );
44
    }
45
}
46