Passed
Pull Request — master (#11)
by Pavel
08:05
created

ViewListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 44
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onPlainResponse() 0 25 4
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Listener;
4
5
use Bankiru\Api\JsonRpc\NormalizerInterface;
6
use Bankiru\Api\JsonRpc\Specification\JsonRpcResponse;
7
use Bankiru\Api\JsonRpc\Specification\RichJsonRpcRequest;
8
use Bankiru\Api\Rpc\Event\ViewEvent;
9
use ScayTrase\Api\JsonRpc\JsonRpcResponseInterface;
10
11
final class ViewListener
12
{
13
    /**
14
     * @var NormalizerInterface
15
     */
16
    private $normalizer;
17
18
    /**
19
     * ViewListener constructor.
20
     *
21
     * @param NormalizerInterface $normalizer
22
     */
23 8
    public function __construct(NormalizerInterface $normalizer)
24
    {
25 8
        $this->normalizer = $normalizer;
26 8
    }
27
28 8
    public function onPlainResponse(ViewEvent $event)
29
    {
30 8
        $request  = $event->getRequest();
31 8
        $response = $event->getResponse();
32
33
        // No need to perform JSON-RPC serialization here
34 8
        if (!$request instanceof RichJsonRpcRequest || $request->isNotification()) {
35 2
            return;
36
        }
37
38
        // Response is already properly formatted
39 6
        if ($response instanceof JsonRpcResponseInterface) {
40
            return;
41
        }
42
43 6
        $response = new JsonRpcResponse(
44 6
            $request->getId(),
45 6
            $this->normalizer->normalize(
0 ignored issues
show
Bug introduced by
It seems like $this->normalizer->norma...tes()->get('_context')) targeting Bankiru\Api\JsonRpc\Norm...rInterface::normalize() can also be of type array; however, Bankiru\Api\JsonRpc\Spec...Response::__construct() does only seem to accept null|object<stdClass>|ar...teger,object<stdClass>>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
46 6
                $event->getResponse(),
47 6
                $request->getAttributes()->get('_context')
48 6
            )
49 6
        );
50
51 6
        $event->setResponse($response);
52 6
    }
53
54
}
55