Test Failed
Pull Request — master (#2)
by Pol
03:30
created

AbstractMethodPlugin   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
cbo 3
dl 0
loc 144
ccs 0
cts 67
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
B call() 0 28 2
A withEndPoint() 0 6 1
A getEndPoint() 0 4 1
A withApiKey() 0 6 1
A withParameters() 0 6 1
A getParameters() 0 4 1
A getMethod() 0 4 1
A setParameters() 0 6 1
A setEndPoint() 0 6 1
A setApiKey() 0 8 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
     * @var array
18
     */
19
    private $parameters = [];
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
25
    /**
26
     * @throws \Http\Client\Exception
27
     *
28
     * @return \Psr\Http\Message\ResponseInterface
29
     */
30
    public function call()
31
    {
32
        $parameters = [
33
            'jsonrpc' => '2.0',
34
            'id'      => uniqid($this->getMethod() . '_'),
35
            'params'  => $this->getParameters(),
36
            'method'  => $this->getMethod(),
37
        ];
38
39
        $response = false;
40
41
        try {
42
            $response = $this->getHttpClient()->sendRequest(
43
                $this->getMessageFactory()->createRequest(
44
                    'POST',
45
                    $this->getEndpoint(),
46
                    [],
47
                    json_encode(
48
                        $parameters
49
                    )
50
                )
51
            );
52
        } catch (\Exception $exception) {
53
            // @todo: log this.
54
        }
55
56
        return $response;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function withEndPoint(string $endpoint)
63
    {
64
        $clone = clone $this;
65
66
        return $clone->setEndPoint($endpoint);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getEndPoint()
73
    {
74
        return $this->endpoint;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function withApiKey(string $apikey)
81
    {
82
        $clone = clone $this;
83
84
        return $clone->setApiKey($apikey);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function withParameters(array $parameters)
91
    {
92
        $clone = clone $this;
93
94
        return $clone->setParameters($parameters);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getParameters()
101
    {
102
        return $this->parameters;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getMethod()
109
    {
110
        return $this::METHOD;
111
    }
112
113
    /**
114
     * @param array $parameters
115
     *
116
     * @return \drupol\Yaroc\Plugin\AbstractMethodPlugin
117
     */
118
    private function setParameters(array $parameters)
119
    {
120
        $this->parameters = $parameters;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param string $endpoint
127
     *
128
     * @return \drupol\Yaroc\Plugin\AbstractMethodPlugin
129
     */
130
    private function setEndPoint(string $endpoint)
131
    {
132
        $this->endpoint = $endpoint;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param string $apikey
139
     *
140
     * @return \drupol\Yaroc\Plugin\AbstractMethodPlugin
141
     */
142
    private function setApiKey(string $apikey)
143
    {
144
        $parameters = $this->getParameters();
145
146
        $parameters += ['apiKey' => $apikey];
147
148
        return $this->setParameters($parameters);
149
    }
150
}
151