Passed
Pull Request — master (#11)
by Pavel
07:52
created

SerializedJsonRpcResponseListener   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.11%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 75
ccs 35
cts 38
cp 0.9211
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onPlainResponse() 0 25 4
B createSerializerContext() 0 27 6
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Adapters\JMS;
4
5
use Bankiru\Api\JsonRpc\Specification\JsonRpcResponse;
6
use Bankiru\Api\Rpc\Event\ViewEvent;
7
use JMS\Serializer\SerializationContext;
8
use JMS\Serializer\Serializer;
9
use ScayTrase\Api\JsonRpc\JsonRpcRequestInterface;
10
use ScayTrase\Api\JsonRpc\JsonRpcResponseInterface;
11
12
final class SerializedJsonRpcResponseListener
13
{
14
    /** @var  Serializer */
15
    private $serializer;
16
17
    /**
18
     * SerializedJsonRpcResponseListener constructor.
19
     *
20
     * @param Serializer $serializer
21
     */
22 8
    public function __construct(Serializer $serializer)
23
    {
24 8
        $this->serializer = $serializer;
25 8
    }
26
27 8
    public function onPlainResponse(ViewEvent $event)
28
    {
29 8
        $request  = $event->getRequest();
30 8
        $response = $event->getResponse();
31
32
        // No need to perform JSON-RPC serialization here
33 8
        if (!$request instanceof JsonRpcRequestInterface || $request->isNotification()) {
34 2
            return;
35
        }
36
37
        // Response is already properly formatted
38 6
        if ($response instanceof JsonRpcResponseInterface) {
39
            return;
40
        }
41
42 6
        $response = new JsonRpcResponse(
43 6
            $request->getId(),
44 6
            $this->serializer->toArray(
45 6
                $event->getResponse(),
46 6
                $this->createSerializerContext($event)
47
48 6
            )
49 6
        );
50 6
        $event->setResponse($response);
51 6
    }
52
53
    /**
54
     * @param ViewEvent $event
55
     *
56
     * @return SerializationContext
57
     * @throws \LogicException
58
     */
59 6
    private function createSerializerContext(ViewEvent $event)
60
    {
61 6
        $context = SerializationContext::create();
62 6
        $context->setSerializeNull(true);
63 6
        $attributes = $event->getRequest()->getAttributes();
64 6
        $defaults   = $attributes->get('_with_default_context', true);
65
66 6
        if (!$defaults && (false === $attributes->get('_context', false))) {
67
            throw new \LogicException(
68
                'Could not perform object serialization as no default context allowed and no custom set'
69
            );
70
        }
71
72 6
        $groups = [];
73 6
        if ($defaults) {
74 6
            $groups[] = 'Default';
75 6
        }
76 6
        if (false !== $attributes->get('_context', false)) {
77 6
            foreach ((array)$attributes->get('_context') as $group) {
78 6
                $groups[] = $group;
79 6
            }
80 6
        }
81
82 6
        $context->setGroups($groups);
83
84 6
        return $context;
85
    }
86
}
87