SerializerViewListener::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Zenstruck\ControllerUtil\EventListener;
4
5
use JMS\Serializer\SerializerInterface as JMSSerializer;
6
use Symfony\Component\Serializer\SerializerInterface as SymfonySerializer;
7
use Zenstruck\ControllerUtil\View;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
class SerializerViewListener extends ViewListener
13
{
14
    private $serializer;
15
16
    /**
17
     * @param JMSSerializer|SymfonySerializer $serializer
18
     */
19 6
    public function __construct($serializer)
20
    {
21 6
        if (!$serializer instanceof SymfonySerializer && !$serializer instanceof JMSSerializer) {
22 1
            throw new \InvalidArgumentException('Serializer must be instance of Symfony\Component\Serializer\Serializer or JMS\Serializer\SerializerInterface.');
23
        }
24
25 5
        $this->serializer = $serializer;
26 5
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 4
    protected function supports(View $view, $format)
32
    {
33 4
        return 'html' !== $format;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    protected function getContent(View $view, $format)
40
    {
41 2
        return $this->serializer->serialize($view->getData(), $format);
42
    }
43
}
44