GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c9b9d5...dee852 )
by Rémi
03:52
created

WordSelectorProxyAdapter::call()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
ccs 0
cts 25
cp 0
rs 8.439
cc 5
eloc 24
nc 9
nop 3
crap 30
1
<?php
2
3
namespace WordSelector\Proxy;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\BadResponseException;
7
use GuzzleHttp\Exception\ClientException;
8
use ProxyManager\Factory\RemoteObject\AdapterInterface;
9
use WordSelector\Entity\Word;
10
use WordSelector\WordSelector;
11
12
class WordSelectorProxyAdapter implements AdapterInterface
13
{
14
    /**
15
     * @var Client
16
     */
17
    private $client;
18
19
    /**
20
     * Construct
21
     *
22
     * @param Client $client
23
     */
24
    public function __construct(Client $client)
25
    {
26
        $this->client = $client;
27
    }
28
29
    /**
30
     * Call remote object
31
     *
32
     * @param  string $wrappedClass
33
     * @param  string $method
34
     * @param  array $params
35
     * @return string
36
     */
37
    public function call($wrappedClass, $method, array $params = [])
38
    {
39
        if ($wrappedClass !== WordSelector::class) {
40
            throw new \InvalidArgumentException('Cannot call this class');
41
        }
42
43
        if ($method !== 'getRandomWord') {
44
            throw new \InvalidArgumentException('Cannot call this method');
45
        }
46
47
        $options = [
48
            'length'     => $params[0],
49
            'lang'       => $params[1],
50
            'complexity' => $params[2]
51
        ];
52
53
        try {
54
            $response = $this->client->request(
55
                'GET',
56
                '/random?' . http_build_query($options)
57
            );
58
            $decodedJson = json_decode((string) $response->getBody());
59
            return new Word(
60
                $decodedJson->word,
61
                $decodedJson->lang,
62
                $decodedJson->complexity
63
            );
64
        } catch (ClientException $e) {
65
            $decodedJson = json_decode((string) $e->getResponse()->getBody());
66
            throw new \InvalidArgumentException($decodedJson->error);
67
        } catch (BadResponseException $e) {
68
            $exceptionBody = (string) $e->getResponse()->getBody();
69
            throw new \RuntimeException($exceptionBody);
70
        }
71
    }
72
}
73