Completed
Pull Request — master (#7)
by Igor
04:25
created

ContextGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createSerializationContext() 0 21 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
    public function __construct(array $resultTypesByMethods = [], string $errorType = null)
27
    {
28
        $this->resultTypesByMethods = $resultTypesByMethods;
29
        $this->errorType = $errorType;
30
    }
31
32
    public function createSerializationContext($request): array
33
    {
34
        $context = [
35
            'result_types_by_methods' => $this->resultTypesByMethods,
36
            'error_type'              => $this->errorType,
37
        ];
38
39
        if ($request instanceof RequestObjectInterface) {
40
            $context['request'] = $request;
41
        } else {
42
            $requests = [];
43
44
            /** @var RequestObjectInterface $singleRequest */
45
            foreach ($request as $singleRequest) {
46
                $requests[$singleRequest->getId()] = $singleRequest;
47
            }
48
49
            $context['requests'] = $requests;
50
        }
51
52
        return ['json_rpc' => $context];
53
    }
54
}
55