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 ( 009536...f82b0b )
by Grisha
03:08 queued 13s
created

PoloniexManager::getBalance()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 8
nop 2
dl 0
loc 26
rs 8.439
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
        $this->tradingApi->setApiKey($apiKey);
59
        $completeBalances = $this->tradingApi->returnCompleteBalances();
60
        $balance = 0;
61
62
        foreach ($completeBalances as $coin => $completeBalance) {
63
            $balance += $completeBalance->btcValue;
64
        }
65
66
        if ($currency !== 'BTC') {
67
            $ticker = $this->publicApi->returnTicker();
68
            $btcCoin = 'BTC_' . $currency;
69
            $coinBtc = $currency . '_BTC';
70
71
            if (isset($ticker[$coinBtc])) {
72
                $balance *= $ticker[$coinBtc]->last;
73
            } elseif (isset($ticker[$btcCoin])) {
74
                $balance /= $ticker[$btcCoin]->last;
75
            } else {
76
                throw new PoloniexException(sprintf('Invalid currency given: %s', $currency));
77
            }
78
        }
79
80
        return $balance;
81
    }
82
}