JsonRpcCallResponseNormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace Yoanm\JsonRpcServer\App\Serialization;
3
4
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCallResponse;
5
6
/**
7
 * Class JsonRpcCallResponseNormalizer
8
 */
9
class JsonRpcCallResponseNormalizer
10
{
11
    /** @var JsonRpcResponseNormalizer */
12
    private $responseNormalizer;
13
14
    /**
15
     * @param JsonRpcResponseNormalizer $responseNormalizer
16
     */
17 50
    public function __construct(JsonRpcResponseNormalizer $responseNormalizer)
18
    {
19 50
        $this->responseNormalizer = $responseNormalizer;
20
    }
21
22
    /**
23
     * @param JsonRpcCallResponse $jsonRpcCallResponse
24
     *
25
     * @return array|null
26
     */
27 50
    public function normalize(JsonRpcCallResponse $jsonRpcCallResponse) : ?array
28
    {
29 50
        $resultList = [];
30 50
        foreach ($jsonRpcCallResponse->getResponseList() as $response) {
31
            // Notifications must not have a response, even if they are on error
32 50
            if (!$response->isNotification()) {
33 43
                $resultList[] = $this->responseNormalizer->normalize($response);
34
            }
35
        }
36
37
        // if no result, it means It was either :
38
        // - a batch call with only notifications
39
        // - a notification request
40
        // => return null response in all cases
41 50
        if (0 === count($resultList)) {
42 7
            return null;
43
        }
44
45
        // In case it's not a batch, return the first (lonely) result
46 43
        if (!$jsonRpcCallResponse->isBatch()) {
47 34
            return array_shift($resultList);
48
        }
49
50 9
        return $resultList;
51
    }
52
}
53