Passed
Push — master ( 7e9313...eec0b0 )
by Mikhail
10:09
created

Batch   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 39
dl 0
loc 94
ccs 40
cts 40
cp 1
c 1
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addBuyLimit() 0 16 1
A addCancel() 0 9 1
A addSellLimit() 0 16 1
A executeBatch() 0 7 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace R3bers\BittrexApi\Api;
6
7
use Ds\Queue;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Exception\GuzzleException;
10
use R3bers\BittrexApi\Exception\TransformResponseException;
11
12
/**
13
 * Class Batch
14
 * @package R3bers\BittrexApi\Api
15
 */
16
class Batch extends Api
17
{
18
    /**
19
     * @var Queue
20
     */
21
    protected Queue $batchQueue;
22
23
    /**
24
     * Api constructor.
25
     */
26 1
    public function __construct(Client $client)
27
    {
28 1
        parent::__construct($client);
29 1
        $this->batchQueue = new Queue();
30 1
    }
31
32
    /** https://bittrex.github.io/api/v3#operation--batch-post
33
     *  https://bittrex.github.io/api/v3#operation--orders-post
34
     * @param string $market
35
     * @param float $quantity
36
     * @param float $rate
37
     * @param bool $useAwards
38
     */
39 1
    public function addBuyLimit(string $market, float $quantity, float $rate, bool $useAwards = false)
40
    {
41
        $addOrder = [
42 1
            "resource" => "order",
43 1
            "operation" => "post",
44
            'payload' => [
45 1
                'marketSymbol' => $market,
46 1
                'direction' => 'BUY',
47 1
                'type' => 'LIMIT',
48 1
                'quantity' => $quantity,
49 1
                'limit' => $rate,
50 1
                'timeInForce' => 'GOOD_TIL_CANCELLED',
51 1
                'useAwards' => $useAwards
52
53
            ]];
54 1
        $this->batchQueue->push($addOrder);
55 1
    }
56
57
    /** https://bittrex.github.io/api/v3#operation--batch-post
58
     *  https://bittrex.github.io/api/v3#operation--orders-post
59
     * @param string $market
60
     * @param float $quantity
61
     * @param float $rate
62
     * @param bool $useAwards
63
     */
64 1
    public function addSellLimit(string $market, float $quantity, float $rate, bool $useAwards = false)
65
    {
66
        $addOrder = [
67 1
            "resource" => "order",
68 1
            "operation" => "post",
69
            'payload' => [
70 1
                'marketSymbol' => $market,
71 1
                'direction' => 'SELL',
72 1
                'type' => 'LIMIT',
73 1
                'quantity' => $quantity,
74 1
                'limit' => $rate,
75 1
                'timeInForce' => 'GOOD_TIL_CANCELLED',
76 1
                'useAwards' => $useAwards
77
78
            ]];
79 1
        $this->batchQueue->push($addOrder);
80 1
    }
81
82
    /** https://bittrex.github.io/api/v3#operation--batch-post
83
     * https://bittrex.github.io/api/v3#operation--orders--orderId--delete
84
     * @param string $uuid
85
     */
86 1
    public function addCancel(string $uuid)
87
    {
88
        $addOrder = [
89 1
            "resource" => "order",
90 1
            "operation" => "delete",
91
            'payload' => [
92 1
                'id' => $uuid
93
            ]];
94 1
        $this->batchQueue->push($addOrder);
95 1
    }
96
97
    /** https://bittrex.github.io/api/v3#operation--batch-post
98
     * Create a new batch request. Currently batch requests are limited to placing and cancelling orders. The request model corresponds to the equivalent individual operations.
99
     * Batch operations are executed sequentially in the order they are listed in the request. The response will return one result for each operation in the request in the same order.
100
     * The status and response payload are the same as the responses would be if individual API requests were made for each operation
101
     * @throws TransformResponseException | GuzzleException
102
     */
103 1
    public function executeBatch(): array
104
    {
105 1
        $queue = [];
106 1
        while (!$this->batchQueue->isEmpty())
107 1
            $queue[] = $this->batchQueue->pop();
108 1
        $options = ['body' => json_encode($queue)];
109 1
        return $this->rest('POST', '/batch', $options);
110
    }
111
}