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.
Passed
Push — develop ( 7475b4...faeb8a )
by A s m
03:14
created

ApiClient::doGet()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 9
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 15
rs 9.9666
1
<?php
2
3
namespace Multicoin\Api\Service;
4
5
use Exception;
6
use Http\Client\HttpClient;
7
use Http\Message\UriFactory;
8
use Http\Message\RequestFactory;
9
use Illuminate\Support\Collection;
10
use Http\Client\Common\PluginClient;
11
use Http\Discovery\HttpClientDiscovery;
12
use Http\Discovery\UriFactoryDiscovery;
13
use Http\Client\Common\HttpMethodsClient;
14
use Http\Discovery\MessageFactoryDiscovery;
15
use Http\Client\Common\Plugin\BaseUriPlugin;
16
use Http\Client\Common\Exception\ClientErrorException;
17
18
class ApiClient
19
{
20
    protected $client;
0 ignored issues
show
Coding Style introduced by
Protected member variable "client" must contain a leading underscore
Loading history...
21
    /**
22
     * @var HttpClient
23
     */
24
    protected $httpClient;
0 ignored issues
show
Coding Style introduced by
Protected member variable "httpClient" must contain a leading underscore
Loading history...
25
26
    protected $plugins = [];
0 ignored issues
show
Coding Style introduced by
Protected member variable "plugins" must contain a leading underscore
Loading history...
27
    /**
28
     * @var RequestFactory
29
     */
30
    private $requestFactory;
0 ignored issues
show
Coding Style introduced by
Private member variable "requestFactory" must contain a leading underscore
Loading history...
31
32
    /**
33
     * @var UriFactory
34
     */
35
    private $uriFactory;
0 ignored issues
show
Coding Style introduced by
Private member variable "uriFactory" must contain a leading underscore
Loading history...
36
37
    public function __construct(
38
        string         $baseUrl,
39
        array          $plugins = [],
40
        bool           $replace = true,
41
        HttpClient     $httpClient = null,
42
        RequestFactory $requestFactory = null
43
    ) {
44
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
45
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
46
        $this->uriFactory = new BaseUriPlugin(
0 ignored issues
show
Documentation Bug introduced by
It seems like new Http\Client\Common\P...'replace' => $replace)) of type Http\Client\Common\Plugin\BaseUriPlugin is incompatible with the declared type Http\Message\UriFactory of property $uriFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
            UriFactoryDiscovery::find()->createUri($baseUrl),
48
            ['replace' => $replace]
49
        );
50
        $this->addPlugins(array_merge($plugins, [$this->uriFactory]));
51
        $this->client = $this->getHttpClient();
52
    }
53
54
    public function addPlugins($plugin)
55
    {
56
        $this->plugins = array_merge($this->plugins, $plugin);
57
        $this->client = $this->getHttpClient();
58
59
        return $this;
60
    }
61
62
    public function getHttpClient()
63
    {
64
        return new HttpMethodsClient($this->getPluginClient(), $this->requestFactory);
65
    }
66
67
    public function getPluginClient()
68
    {
69
        return new PluginClient($this->httpClient, $this->plugins);
70
    }
71
72
    public function doGet(string $url):  ?Collection
73
    {
74
        try {
75
            $request = $this->client->get($url);
76
            $response = $request->getBody()->getContents();
77
        } catch (ClientErrorException $e) {
78
            throw new Exception($e->getMessage()." Error Processing Request for [$url]", 1);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $url instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
79
80
            return collect([
0 ignored issues
show
Unused Code introduced by
return collect(array('co...' => $e->getMessage())) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
81
                'code'   => $e->getCode(),
82
                'reason' => $e->getMessage(),
83
            ]);
84
        }
85
86
        return $this->responseResult($response);
87
    }
88
89
    public function doPost(string $url, array $data = []):  ?Collection
90
    {
91
        try {
92
            $request = $this->client->post($url, $data);
93
            $response = $request->getBody()->getContents();
94
        } catch (ClientErrorException $e) {
95
            throw new Exception($e->getMessage()." Error Processing Request for [$url]", 1);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $url instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
96
97
            return collect([
0 ignored issues
show
Unused Code introduced by
return collect(array('co...' => $e->getMessage())) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
98
                'code'   => $e->getCode(),
99
                'reason' => $e->getMessage(),
100
            ]);
101
        }
102
103
        return $this->responseResult($response);
104
    }
105
106
    protected function responseResult(?string $response) :? Collection
107
    {
108
        $data = json_decode($response, true);
109
110
        if (isset($data['error'])) {
111
            throw new Exception($data['error']['message']);
112
        }
113
114
        return collect($data);
115
    }
116
}
117