Completed
Pull Request — master (#2)
by Pol
12:05
created

AbstractMethodPlugin::withApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace drupol\Yaroc\Plugin;
4
5
use drupol\Yaroc\Http\AbstractClient;
6
7
abstract class AbstractMethodPlugin extends AbstractClient implements MethodPluginInterface
8
{
9
    /**
10
     * The endpoint.
11
     *
12
     * @var string
13
     */
14
    private $endpoint = '';
15
16
    /**
17
     * The parameters.
18
     *
19
     * @var array
20
     */
21
    private $parameters = [];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function call()
27
    {
28
        $parameters = [
29
            'jsonrpc' => '2.0',
30
            'id'      => uniqid($this->getMethod() . '_'),
31
            'params'  => $this->getParameters(),
32
            'method'  => $this->getMethod(),
33
        ];
34
35
        $response = false;
36
37
        try {
38
            $response = $this->getHttpClient()->sendRequest(
39
                $this->getMessageFactory()->createRequest(
40
                    'POST',
41
                    $this->getEndpoint(),
42
                    [],
43
                    json_encode(
44
                        $parameters
45
                    )
46
                )
47
            );
48
        } catch (\Exception $exception) {
49
            // @todo: log this.
50
        }
51
52
        return $response;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function withEndPoint(string $endpoint)
59
    {
60
        $clone = clone $this;
61
        $clone->endpoint = $endpoint;
62
63
        return $clone;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getEndPoint()
70
    {
71
        return $this->endpoint;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function withApiKey(string $apikey)
78
    {
79
        $clone = clone $this;
80
        $parameters = $clone->getParameters();
81
        $parameters += ['apiKey' => $apikey];
82
        $clone->parameters = $parameters;
83
84
        return $clone;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function withParameters(array $parameters)
91
    {
92
        $clone = clone $this;
93
        $clone->parameters = $parameters;
94
95
        return $clone;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getParameters()
102
    {
103
        return $this->parameters;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getMethod()
110
    {
111
        return $this::METHOD;
112
    }
113
}
114