AbstractSerializerListener   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
cbo 2
dl 0
loc 39
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
isControllerResultSupported() 0 1 ?
serializeControllerResult() 0 1 ?
A onKernelView() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Bundle\LrsBundle\Listener;
13
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
16
17
/**
18
 * View listener serializing controller results.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
abstract class AbstractSerializerListener
23
{
24
    /**
25
     * Serializes a controller result and wraps it in a JSON response.
26
     *
27
     * @param GetResponseForControllerResultEvent $event The HTTP kernel event
28
     */
29 4
    public function onKernelView(GetResponseForControllerResultEvent $event)
30
    {
31 4
        if (!$this->isControllerResultSupported($event->getControllerResult())) {
32 2
            return;
33
        }
34
35 2
        $response = new Response(
36 2
            $this->serializeControllerResult($event->getControllerResult()),
37 2
            200,
38 2
            array('Content-Type' => 'application/json')
39 2
        );
40 2
        $event->setResponse($response);
41 2
    }
42
43
    /**
44
     * Checks whether the controller result object is supported by a serializer.
45
     *
46
     * @param mixed $result The controller result
47
     *
48
     * @return bool True if the object is supported, false otherwise
49
     */
50
    abstract protected function isControllerResultSupported($result);
51
52
    /**
53
     * Serializes the controller result object.
54
     *
55
     * @param mixed $result The controller result to serialize
56
     *
57
     * @return string The serialized controller result
58
     */
59
    abstract protected function serializeControllerResult($result);
60
}
61