Completed
Push — master ( 17da57...0d4fd4 )
by Denis
02:07
created

RequestBuilder::preBuild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace PhpJsonRpc\Client;
4
5
use PhpJsonRpc\Client\RequestBuilder\BuilderContainer;
6
use PhpJsonRpc\Common\Interceptor\Interceptor;
7
use PhpJsonRpc\Core\Invoke\AbstractInvoke;
8
use PhpJsonRpc\Core\Invoke\Invoke;
9
use PhpJsonRpc\Core\InvokeSpec;
10
11
class RequestBuilder
12
{
13
    /**
14
     * @var Interceptor
15
     */
16
    private $preBuild;
17
18
    /**
19
     * RequestBuilder constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->preBuild = Interceptor::createBase();
24
    }
25
26
    /**
27
     * @param InvokeSpec $call
28
     *
29
     * @return string
30
     */
31
    public function build(InvokeSpec $call): string
32
    {
33
        $response = [];
34
        $units    = $call->getUnits();
35
36
        foreach ($units as $invoke) {
37
            /** @var Invoke $invoke */
38
            $invoke = $this->preBuild($invoke);
39
40
            $response[] = [
41
                'jsonrpc' => '2.0',
42
                'method'  => $invoke->getRawMethod(),
43
                'params'  => $invoke->getRawParams(),
44
                'id'      => $invoke->getRawId()
45
            ];
46
        }
47
48
        if ($call->isSingleCall()) {
49
            return json_encode($response[0]);
50
        }
51
52
        return json_encode($response);
53
    }
54
55
    /**
56
     * @return Interceptor
57
     */
58
    public function onPreBuild(): Interceptor
59
    {
60
        return $this->preBuild;
61
    }
62
63
    /**
64
     * @param AbstractInvoke $invoke
65
     *
66
     * @return AbstractInvoke
67
     */
68 View Code Duplication
    private function preBuild(AbstractInvoke $invoke): AbstractInvoke
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $result = $this->preBuild->handle(new BuilderContainer($this, $invoke));
71
72
        if ($result instanceof BuilderContainer) {
73
            return $result->getInvoke();
74
        }
75
76
        throw new \RuntimeException();
77
    }
78
}
79