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

ViewListener::onPlainResponse()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 15
cts 16
cp 0.9375
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 13
nc 3
nop 1
crap 4.0039
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