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 — master ( f82b0b...bf78f3 )
by Grisha
05:33
created

PoloniexManager::getHighestBid()   A

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;
13
14
use Poloniex\Api\{PublicApi, TradingApi};
15
use Poloniex\Exception\PoloniexException;
16
17
/**
18
 * Class PoloniexManager
19
 *
20
 * @author Grisha Chasovskih <[email protected]>
21
 */
22
final class PoloniexManager
23
{
24
    /**
25
     * @var PublicApi
26
     */
27
    private $publicApi;
28
29
    /**
30
     * @var TradingApi
31
     */
32
    private $tradingApi;
33
34
    /**
35
     * PoloniexManager constructor.
36
     *
37
     * @param PublicApi  $publicApi
38
     * @param TradingApi $tradingApi
39
     */
40
    public function __construct(PublicApi $publicApi, TradingApi $tradingApi)
41
    {
42
        $this->publicApi = $publicApi;
43
        $this->tradingApi = $tradingApi;
44
    }
45
46
    /**
47
     * Get total balance for given currency
48
     *
49
     * @param ApiKey $apiKey   User credentials
50
     * @param string $currency Any available currency at market (DOGE, LTC, ETH, USDT, XMR etc..)
51
     *
52
     * @return float
53
     * @throws PoloniexException
54
     */
55
    public function getBalance(ApiKey $apiKey, string $currency = 'BTC'): float
56
    {
57
        $currency = strtoupper($currency);
58
        $completeBalances = $this->tradingApi->setApiKey($apiKey)->returnCompleteBalances();
59
        $balance = 0;
60
61
        foreach ($completeBalances as $coin => $completeBalance) {
62
            $balance += $completeBalance->btcValue;
63
        }
64
65
        if ($currency !== 'BTC') {
66
            $ticker = $this->publicApi->returnTicker();
67
            $btcCoin = 'BTC_' . $currency;
68
            $coinBtc = $currency . '_BTC';
69
70
            if (isset($ticker[$coinBtc])) {
71
                $balance *= $ticker[$coinBtc]->last;
72
            } elseif (isset($ticker[$btcCoin])) {
73
                $balance /= $ticker[$btcCoin]->last;
74
            } else {
75
                throw new PoloniexException(sprintf('Invalid currency given: %s', $currency));
76
            }
77
        }
78
79
        return $balance;
80
    }
81
82
    /**
83
     * Get available balance for given coin.
84
     * You can use available balance for trades.
85
     *
86
     * @param ApiKey $apiKey
87
     * @param string $coin
88
     *
89
     * @return float|null
90
     */
91
    public function getAvailableBalance(ApiKey $apiKey, string $coin):? float
92
    {
93
        $balances = $this->tradingApi->setApiKey($apiKey)->returnCompleteBalances();
94
95
        if (!isset($balances[$coin])) {
96
            throw new PoloniexException(sprintf('Invalid coin given: %s', $coin));
97
        }
98
99
        return $balances[$coin]->available;
100
    }
101
102
    /**
103
     * Get asks for given trade pair
104
     *
105
     * @param string $pair
106
     *
107
     * @return float[] Keys are prices and values are amount of trades
108
     */
109
    public function asks(string $pair): array
110
    {
111
        $asks = [];
112
        foreach ($this->publicApi->returnOrderBook($pair)->asks as $ask) {
113
            $asks[$ask[0]] = $ask[1];
114
        }
115
116
        ksort($asks);
117
118
        return $asks;
119
    }
120
121
    /**
122
     * Get lowest ask for given trade pair
123
     *
124
     * @param string $pair
125
     *
126
     * @return float
127
     */
128
    public function getLowestAsk(string $pair): float
129
    {
130
        return min(array_keys($this->asks($pair)));
131
    }
132
133
    /**
134
     * Get bids
135
     *
136
     * @param string $pair
137
     *
138
     * @return float[] Keys are prices and values are amount of trades
139
     */
140
    public function bids(string $pair): array
141
    {
142
        $bids = [];
143
        foreach ($this->publicApi->returnOrderBook($pair)->bids as $bid) {
144
            $bids[$bid[0]] = $bid[1];
145
        }
146
147
        ksort($bids);
148
149
        return $bids;
150
    }
151
152
    /**
153
     * Get highest bid
154
     *
155
     * @param string $pair
156
     *
157
     * @return float
158
     */
159
    public function getHighestBid(string $pair): float
160
    {
161
        return max(array_keys($this->bids($pair)));
162
    }
163
}