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

RandomOrgAPI::getFromResult()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.0729

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 4
nop 1
crap 5.0729
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
     * The default Random.org endpoint template.
16
     *
17
     * @var string;
18
     */
19
    private $endpoint = 'https://api.random.org/json-rpc/1/invoke';
20
21
    /**
22
     * The configuration.
23
     *
24
     * @var array
25
     */
26
    private $configuration;
27
28
    /**
29
     * The HTTP client.
30
     *
31
     * @var \Http\Client\HttpClient
32
     */
33
    private $httpClient;
34
35
    /**
36
     * RandomOrgAPI constructor.
37
     *
38
     * @param array $configuration
39
     */
40 5
    public function __construct(array $configuration = [])
41
    {
42 5
        $this->configuration = $configuration;
43 5
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function withApiKey(string $apikey)
49
    {
50 2
        $clone = clone $this;
51
52 2
        $configuration = $clone->getConfiguration();
53 2
        $configuration['apiKey'] = $apikey;
54
55 2
        $clone->configuration = $configuration;
56
57 2
        return $clone;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function withEndPoint(string $endpoint)
64
    {
65 1
        $clone = clone $this;
66 1
        $clone->endpoint = $endpoint;
67
68 1
        return $clone;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 1
    public function withHttpClient(HttpClient $client)
75
    {
76 1
        $clone = clone $this;
77 1
        $clone->httpClient = $client;
78
79 1
        return $clone;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 1
    public function getEndPoint()
86
    {
87 1
        return $this->endpoint;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    public function getApiKey()
94
    {
95 1
        $configuration = $this->getConfiguration();
96
97 1
        $configuration += ['apiKey' => ''];
98
99 1
        return $configuration['apiKey'];
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function call(MethodPluginInterface $methodPlugin)
106
    {
107
        return $this->validateResponse($methodPlugin
108
            ->withEndPoint($this->getEndPoint())
109
            ->withApiKey($this->getApiKey())->call());
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function get(MethodPluginInterface $methodPlugin)
116
    {
117
        return json_decode(
118
            (string) $this
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...
119
                ->call($methodPlugin)
120
                ->getBody()
121
                ->getContents(),
122
            true
123
        );
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getData(MethodPluginInterface $methodPlugin)
130
    {
131
        $data = $this->get($methodPlugin);
132
133
        if (!isset($data['result'])) {
134
            return false;
135
        }
136
137
        if (isset($data['result']['random']) && isset($data['result']['random']['data'])) {
138
            return $data['result']['random']['data'];
139
        }
140
141
        return false;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 2
    public function getConfiguration()
148
    {
149 2
        return $this->configuration;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 1
    public function getHttpClient()
156
    {
157 1
        return $this->httpClient;
158
    }
159
160
    /**
161
     * Validate the response.
162
     *
163
     * @param \Psr\Http\Message\ResponseInterface $response
164
     *
165
     * @return \Exception|ResponseInterface
166
     */
167
    private function validateResponse(ResponseInterface $response)
168
    {
169
        if (200 == $response->getStatusCode()) {
170
            $body = json_decode((string) $response->getBody()->getContents(), true);
171
172
            if (isset($body['error']['code'])) {
173
                switch ($body['error']['code']) {
174 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...
175
                        throw new \InvalidArgumentException(
176
                            'Invalid Request: ' . $body['error']['message'],
177
                            $body['error']['code']
178
                        );
179
                    case -32601:
180
                        throw new \BadFunctionCallException(
181
                            'Procedure not found: ' . $body['error']['message'],
182
                            $body['error']['code']
183
                        );
184 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...
185
                        throw new \InvalidArgumentException(
186
                            'Invalid arguments: ' . $body['error']['message'],
187
                            $body['error']['code']
188
                        );
189 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...
190
                        throw new \RuntimeException(
191
                            'Internal Error: ' . $body['error']['message'],
192
                            $body['error']['code']
193
                        );
194 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...
195
                        throw new \RuntimeException(
196
                            'Invalid request/response: ' . $body['error']['message'],
197
                            $body['error']['code']
198
                        );
199
                }
200
            }
201
        }
202
203
        $response->getBody()->rewind();
204
205
        return $response;
206
    }
207
}
208