Completed
Push — master ( 40bf07...dbb18c )
by Mikhail
01:23
created

Account::getOrderHistory()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 64
nop 6
crap 7
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 int|null $pageSize
110
     * @param string|null $startDate
111
     * @param string|null $endDate
112
     * @param string|null $nextPageToken
113
     * @param string|null $previousPageToken
114
     * @return array
115
     * @throws GuzzleException
116
     * @throws TransformResponseException
117
     */
118 1
    public function getOrderHistory(?string $marketSymbol = null, ?int $pageSize = null, ?string $startDate = null, ?string $endDate = null, ?string $nextPageToken = null, ?string $previousPageToken = null): array
119
    {
120 1
        $options = [];
121 1
        if (!is_null($marketSymbol)) $options['query'] = ['marketSymbol' => $marketSymbol];
122 1
        if (!is_null($pageSize)) $options['query'] = ['pageSize' => $pageSize];
123 1
        if (!is_null($startDate)) $options['query'] = ['startDate' => $startDate];
124 1
        if (!is_null($endDate)) $options['query'] = ['endDate' => $endDate];
125 1
        if (!is_null($nextPageToken)) $options['query'] = ['nextPageToken' => $nextPageToken];
126 1
        if (!is_null($previousPageToken)) $options['query'] = ['previousPageToken' => $previousPageToken];
127
128 1
        return $this->rest('GET', '/orders/closed', $options);
129
    }
130
131
    /**
132
     * @param string|null $currencySymbol
133
     * @param string|null $status
134
     * @return array
135
     * @throws Exception|GuzzleException
136
     */
137 1
    public function getWithdrawalHistory(?string $currencySymbol = null, ?string $status = null): array
138
    {
139 1
        return $this->getHistory('withdrawals', $currencySymbol, $status);
140
    }
141
142
    /**
143
     * @param string $whatHistory
144
     * @param string $currencySymbol
145
     * @param string $status
146
     * @return array
147
     * @throws Exception|GuzzleException
148
     */
149 2
    private function getHistory(string $whatHistory, ?string $currencySymbol, ?string $status): array
150
    {
151 2
        $options = [];
152
153 2
        if (!is_null($currencySymbol)) $options['query'] = ['currencySymbol' => $currencySymbol];
154 2
        if (!is_null($status)) $options['query'] = ['status' => $status];
155
156 2
        return $this->rest('GET', '/' . $whatHistory . '/closed', $options);
157
    }
158
159
    /**
160
     * @param string|null $currencySymbol
161
     * @param string|null $status
162
     * @return array
163
     * @throws Exception|GuzzleException
164
     */
165 1
    public function getDepositHistory(?string $currencySymbol = null, ?string $status = null): array
166
    {
167 1
        return $this->getHistory('deposits', $currencySymbol, $status);
168
    }
169
}