Completed
Push — master ( dbb18c...4017c3 )
by Mikhail
01:17
created

Account::getOrderHistory()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.3731
1
<?php
2
3
declare(strict_types=1);
4
5
namespace R3bers\BittrexApi\Api;
6
7
use Exception;
8
use GuzzleHttp\Exception\GuzzleException;
9
use R3bers\BittrexApi\Exception\TransformResponseException;
10
11
/**
12
 * Class Account
13
 * @package R3bers\BittrexApi\Api
14
 */
15
class Account extends Api
16
{
17
    /**
18
     * @param bool|null $needSequence
19
     * @return array
20
     * @throws GuzzleException
21
     * @throws TransformResponseException
22
     */
23 1
    public function getBalances(?bool $needSequence = null): array
24
    {
25 1
        return $this->rest('GET', '/balances', [], $needSequence);
26
    }
27
28
    /**
29
     * @return int
30
     * @throws GuzzleException
31
     * @throws TransformResponseException
32
     */
33 1
    public function headBalances(): int
34
    {
35 1
        $responseArray = $this->rest('HEAD', '/balances');
36 1
        return $responseArray['Sequence'];
37
    }
38
39
    /**
40
     * @param string $currency
41
     * @param bool|null $needSequence
42
     * @return array
43
     * @throws GuzzleException
44
     * @throws TransformResponseException
45
     */
46 1
    public function getBalance(string $currency, ?bool $needSequence = null): array
47
    {
48 1
        return $this->rest('GET', '/balances/' . $currency, [], $needSequence);
49
    }
50
51
    /**
52
     * @param string $currency
53
     * @return array
54
     * @throws Exception|GuzzleException
55
     */
56 1
    public function getDepositAddress(string $currency): array
57
    {
58 1
        return $this->rest('GET', '/addresses/' . $currency);
59
    }
60
61
    /**
62
     * @param string $currency
63
     * @return array
64
     * @throws Exception|GuzzleException
65
     */
66 1
    public function setDepositAddress(string $currency): array
67
    {
68 1
        $options = ['body' => json_encode(['currencySymbol' => $currency])];
69
70 1
        return $this->rest('POST', '/addresses', $options);
71
    }
72
73
    /**
74
     * @param string $currency
75
     * @param float $quantity
76
     * @param string $address
77
     * @param string|null $paymentId
78
     * @return array
79
     * @throws Exception|GuzzleException
80
     */
81 1
    public function withdraw(string $currency, float $quantity, string $address, ?string $paymentId = null): array
82
    {
83
        $newWithdrawal = [
84 1
            'currencySymbol' => $currency,
85 1
            'quantity' => $quantity,
86 1
            'cryptoAddress' => $address,
87
        ];
88
89 1
        if (!is_null($paymentId)) {
90 1
            $newWithdrawal['cryptoAddressTag'] = $paymentId;
91
        }
92
93 1
        $options = ['body' => json_encode($newWithdrawal)];
94 1
        return $this->rest('POST', '/withdrawals', $options);
95
    }
96
97
    /**
98
     * @param string $uuid
99
     * @return array
100
     * @throws Exception|GuzzleException
101
     */
102 1
    public function getOrder(string $uuid): array
103
    {
104 1
        return $this->rest('GET', '/orders/' . $uuid);
105
    }
106
107
    /**
108
     * @param string|null $marketSymbol
109
     * @param array|null $pagination
110
     * @return array
111
     * @throws GuzzleException
112
     * @throws TransformResponseException
113
     */
114 1
    public function getOrderHistory(?string $marketSymbol = null, ?array $pagination = null): array
115
    {
116 1
        $options = [];
117 1
        if (!is_null($marketSymbol)) $options['query'] = ['marketSymbol' => $marketSymbol];
118 1
        if (is_array($pagination))
119
            foreach ($pagination as $key => $value)
120
                $options['query'] = [$key => $value];
121
122 1
        return $this->rest('GET', '/orders/closed', $options);
123
    }
124
125
    /**
126
     * @param string|null $currencySymbol
127
     * @param string|null $status
128
     * @return array
129
     * @throws Exception|GuzzleException
130
     */
131 1
    public function getWithdrawalHistory(?string $currencySymbol = null, ?string $status = null): array
132
    {
133 1
        return $this->getHistory('withdrawals', $currencySymbol, $status);
134
    }
135
136
    /**
137
     * @param string $whatHistory
138
     * @param string $currencySymbol
139
     * @param string $status
140
     * @return array
141
     * @throws Exception|GuzzleException
142
     */
143 2
    private function getHistory(string $whatHistory, ?string $currencySymbol, ?string $status): array
144
    {
145 2
        $options = [];
146
147 2
        if (!is_null($currencySymbol)) $options['query'] = ['currencySymbol' => $currencySymbol];
148 2
        if (!is_null($status)) $options['query'] = ['status' => $status];
149
150 2
        return $this->rest('GET', '/' . $whatHistory . '/closed', $options);
151
    }
152
153
    /**
154
     * @param string|null $currencySymbol
155
     * @param string|null $status
156
     * @return array
157
     * @throws Exception|GuzzleException
158
     */
159 1
    public function getDepositHistory(?string $currencySymbol = null, ?string $status = null): array
160
    {
161 1
        return $this->getHistory('deposits', $currencySymbol, $status);
162
    }
163
}