Test Setup Failed
Pull Request — master (#2)
by Pol
11:29
created

RandomOrgAPI::setApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace drupol\Yaroc;
4
5
use drupol\Yaroc\Plugin\MethodPluginInterface;
6
use Http\Client\HttpClient;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Class RandomOrgAPI.
11
 */
12
class RandomOrgAPI implements RandomOrgAPIInterface
13
{
14
15
    /**
16
     * The default Random.org endpoint template.
17
     *
18
     * @var string;
19
     */
20
    private $endpoint = 'https://api.random.org/json-rpc/1/invoke';
21
22
    /**
23
     * The configuration.
24
     *
25
     * @var array
26
     */
27
    private $configuration = [];
28
29
    /**
30
     * The HTTP client.
31
     *
32
     * @var \Http\Client\HttpClient
33
     */
34
    private $httpClient;
35
36
    /**
37
     * RandomOrgAPI constructor.
38
     *
39
     * @param array $configuration
40
     */
41
    public function __construct(array $configuration)
42
    {
43
        $this->configuration = $configuration;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function withApiKey(string $apikey)
50
    {
51
        $clone = clone $this;
52
53
        return $clone->setApiKey($apikey);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function withEndPoint(string $endpoint)
60
    {
61
        $clone = clone $this;
62
63
        return $clone->setEndPoint($endpoint);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function withHttpClient(HttpClient $client)
70
    {
71
        $clone = clone $this;
72
73 16
        return $clone->setHttpClient($client);
74 16
    }
75 16
76 16
    /**
77 16
     * {@inheritdoc}
78
     */
79
    public function getEndPoint()
80
    {
81
        return $this->endpoint;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 16
    public function getApiKey()
88 16
    {
89
        $configuration = $this->getConfiguration();
90 16
91
        $configuration += ['apiKey' => ''];
92
93
        return $configuration['apiKey'];
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 13
    public function call(MethodPluginInterface $methodPlugin)
100 13
    {
101
        return $this->validateResponse($methodPlugin
102
            ->withEndPoint($this->getEndPoint())
103
            ->withApiKey($this->getApiKey())->call());
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function get(MethodPluginInterface $methodPlugin)
110
    {
111 16
        $response = $this->call($methodPlugin);
112 16
113 16
        return json_decode((string) $response->getBody()->getContents(), TRUE);
0 ignored issues
show
Bug introduced by
The method getBody does only exist in Psr\Http\Message\ResponseInterface, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
114
    }
115 16
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getData(MethodPluginInterface $methodPlugin)
120
    {
121
        $data = $this->get($methodPlugin);
122
123 16
        if (!isset($data['result'])) {
124 16
            return FALSE;
125
        }
126
127
        if (isset($data['result']['random']) && isset($data['result']['random']['data'])) {
128
            return $data['result']['random']['data'];
129
        }
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 2
    public function getConfiguration()
136 2
    {
137 2
        return $this->configuration;
138
    }
139 2
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getHttpClient()
144
    {
145
        return $this->httpClient;
146
    }
147 17
148 17
    /**
149
     * @param string $apikey
150
     *
151
     * @return $this
152
     */
153
    private function setApiKey(string $apikey)
154
    {
155
        $configuration = $this->getConfiguration();
156
        $configuration['apiKey'] = $apikey;
157
158
        return $this->setConfiguration($configuration);
159
    }
160
161
    /**
162
     * @param string $endpoint
163 16
     *
164
     * @return $this
165 16
     */
166 16
    private function setEndPoint(string $endpoint)
167
    {
168
        $this->endpoint = $endpoint;
169 16
170 16
        return $this;
171
    }
172
173
    /**
174 16
     * Validate the response.
175 16
     *
176 16
     * @param \Psr\Http\Message\ResponseInterface $response
177
     *
178 16
     * @return \Exception|ResponseInterface
179
     */
180
    private function validateResponse(ResponseInterface $response)
181
    {
182
        if (200 == $response->getStatusCode()) {
183
            $body = json_decode((string) $response->getBody()->getContents(), true);
184
185
            if (isset($body['error']['code'])) {
186 16
                switch ($body['error']['code']) {
187 16 View Code Duplication
                    case -32600:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
188
                        throw new \InvalidArgumentException('Invalid Request: ' . $body['error']['message'], $body['error']['code']);
189
                    case -32601:
190
                        throw new \BadFunctionCallException('Procedure not found: ' . $body['error']['message'], $body['error']['code']);
191 View Code Duplication
                    case -32602:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
192
                        throw new \InvalidArgumentException('Invalid arguments: ' . $body['error']['message'], $body['error']['code']);
193 View Code Duplication
                    case -32603:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
194
                        throw new \RuntimeException('Internal Error: ' . $body['error']['message'], $body['error']['code']);
195 View Code Duplication
                    default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
196
                        throw new \RuntimeException('Invalid request/response: ' . $body['error']['message'], $body['error']['code']);
197
                }
198 13
            }
199 13
        }
200
201 13
        $response->getBody()->rewind();
202
203
        return $response;
204
    }
205
206
    /**
207
     * @param $configuration
208
     *
209 12
     * @return $this
210 12
     */
211
    private function setConfiguration($configuration)
212
    {
213
        $this->configuration = $configuration;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Set the HTTP client.
220
     *
221 16
     * @param \Http\Client\HttpClient $client
222 16
     *
223
     * @return \drupol\Yaroc\RandomOrgAPI
224 16
     */
225
    private function setHttpClient(HttpClient $client)
226
    {
227
        $this->httpClient = $client;
228
229
        return $this;
230
    }
231
}
232