Passed
Pull Request — master (#7)
by Igor
04:31
created

ContextGenerator::createSerializationContext()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.9
cc 3
nc 2
nop 1
crap 3
1
<?php
2
/*
3
 * This file is part of JSON RPC Client.
4
 *
5
 * (c) Igor Lazarev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Strider2038\JsonRpcClient\Serialization;
12
13
use Strider2038\JsonRpcClient\Request\RequestObjectInterface;
14
15
/**
16
 * @author Igor Lazarev <[email protected]>
17
 */
18
class ContextGenerator
19
{
20
    /** @var string[] */
21
    private $resultTypesByMethods;
22
23
    /** @var string|null */
24
    private $errorType;
25
26 47
    public function __construct(array $resultTypesByMethods = [], string $errorType = null)
27
    {
28 47
        $this->resultTypesByMethods = $resultTypesByMethods;
29 47
        $this->errorType = $errorType;
30 47
    }
31
32 34
    public function createSerializationContext($request): array
33
    {
34
        $context = [
35 34
            'result_types_by_methods' => $this->resultTypesByMethods,
36 34
            'error_type'              => $this->errorType,
37
        ];
38
39 34
        if ($request instanceof RequestObjectInterface) {
40 25
            $context['request'] = $request;
41
        } else {
42 9
            $requests = [];
43
44
            /** @var RequestObjectInterface $singleRequest */
45 9
            foreach ($request as $singleRequest) {
46 9
                $requests[$singleRequest->getId()] = $singleRequest;
47
            }
48
49 9
            $context['requests'] = $requests;
50
        }
51
52 34
        return ['json_rpc' => $context];
53
    }
54
}
55