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.

AbstractApi::setProxy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Poloniex PHP SDK.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2017-2018 Chasovskih Grisha <[email protected]>
9
 * @license https://github.com/signulls/poloniex-php-sdk/blob/master/LICENSE MIT
10
 */
11
12
namespace Poloniex\Api;
13
14
use GuzzleHttp\RequestOptions;
15
use Poloniex\PoloniexClient;
16
use Poloniex\Exception\PoloniexException;
17
use Symfony\Component\Serializer\{Serializer, SerializerAwareInterface, SerializerInterface};
18
19
/**
20
 * Class AbstractApi
21
 *
22
 * @author Grisha Chasovskih <[email protected]>
23
 */
24
abstract class AbstractApi implements ApiInterface, SerializerAwareInterface
25
{
26
    use Traits\ResponseFactoryTrait;
27
28
    /**
29
     * Proxy address
30
     *
31
     * @var string|null
32
     */
33
    protected $proxy;
34
35
    /**
36
     * @var PoloniexClient
37
     */
38
    protected $client;
39
40
    /**
41
     * @var array
42
     */
43
    protected $options = [];
44
45
    /**
46
     * Request method
47
     *
48
     * @var string
49
     */
50
    protected $method;
51
52
    /**
53
     * Request uri
54
     *
55
     * @var string
56
     */
57
    protected $uri;
58
59
    /**
60
     * AbstractApi constructor.
61
     *
62
     * @param PoloniexClient                 $client
63
     * @param SerializerInterface|Serializer $serializer
64
     */
65
    public function __construct(
66
        PoloniexClient $client,
67
        SerializerInterface $serializer
68
    ) {
69
        $this->setSerializer($serializer);
70
        $this->client = $client;
71
72
        $this->setup();
73
    }
74
75
    /**
76
     * Set proxy
77
     *
78
     * @param string|null $proxy
79
     */
80
    public function setProxy(string $proxy = null): void
81
    {
82
        $this->proxy = $proxy;
83
    }
84
85
    /**
86
     * Call request
87
     *
88
     * @param string $command
89
     * @param array $params
90
     *
91
     * @return array
92
     * @throws PoloniexException
93
     */
94
    public function request(string $command, array $params = []): array
95
    {
96
        if (isset($params['currencyPair']) && $params['currencyPair'] !== 'all') {
97
            $this->checkPair($params['currencyPair'], $command);
98
        }
99
100
        if ($this->proxy !== null) {
101
            $this->options[RequestOptions::PROXY] = $this->proxy;
102
        }
103
104
        $contents = $this->client
105
            ->request($this->method, $this->uri, $this->options)
106
            ->getBody()
107
            ->getContents();
108
109
        $this->proxy = null;
110
        $response = json_decode($contents ?: '{}', true) ?: [];
111
        $this->throwExceptionIf(isset($response['error']), $response['error'] ?? 'Poloniex API unknown error.');
112
113
        return $response;
114
    }
115
116
    /**
117
     * Check currency pair
118
     *
119
     * @param string $currencyPair
120
     * @param string $command
121
     */
122
    final protected function checkPair(string $currencyPair, string $command)
123
    {
124
        $pair = explode('_', $currencyPair);
125
126
        $this->throwExceptionIf(
127
            $pair[0] === $pair[1],
128
            sprintf('Unable to call "%s" with currency pair %s.', $command, $currencyPair)
129
        );
130
    }
131
132
    /**
133
     * Setup request method and uri
134
     */
135
    abstract protected function setup(): void;
136
}