Failed Conditions
Pull Request — release/3.0.0-dev (#32)
by Yo
02:02
created

JsonRpcCallResponseNormalizer::normalize()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 9
nop 1
dl 0
loc 24
ccs 10
cts 10
cp 1
crap 5
rs 8.5125
c 0
b 0
f 0
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 7
    public function __construct(JsonRpcResponseNormalizer $responseNormalizer)
18
    {
19 7
        $this->responseNormalizer = $responseNormalizer;
20 7
    }
21
22
    /**
23
     * @param JsonRpcCallResponse $jsonRpcCallResponse
24
     *
25
     * @return array|null
26
     */
27 7
    public function normalize(JsonRpcCallResponse $jsonRpcCallResponse)
28
    {
29 7
        $resultList = [];
30 7
        foreach ($jsonRpcCallResponse->getResponseList() as $response) {
31
            // Notifications must not have a response, even if they are on error
32 7
            if (!$response->isNotification()) {
33 7
                $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 7
        if (0 === count($resultList)) {
42 3
            return null;
43
        }
44
45
        // In case it's not a batch, return the first (lonely) result
46 4
        if (!$jsonRpcCallResponse->isBatch()) {
47 2
            return array_shift($resultList);
48
        }
49
50 2
        return $resultList;
51
    }
52
}
53