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

RandomOrgAPI::withApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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