Batch   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addBuyLimit() 0 15 1
A addCancel() 0 9 1
A addSellLimit() 0 15 1
A executeBatch() 0 7 2
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 2
    public function __construct(Client $client)
27
    {
28 2
        parent::__construct($client);
29 2
        $this->batchQueue = new Queue();
30 2
    }
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 1
        $this->batchQueue->push($addOrder);
54 1
    }
55
56
    /** https://bittrex.github.io/api/v3#operation--batch-post
57
     *  https://bittrex.github.io/api/v3#operation--orders-post
58
     * @param string $market
59
     * @param float $quantity
60
     * @param float $rate
61
     * @param bool $useAwards
62
     */
63 1
    public function addSellLimit(string $market, float $quantity, float $rate, bool $useAwards = false)
64
    {
65
        $addOrder = [
66 1
            "resource" => "order",
67 1
            "operation" => "post",
68
            'payload' => [
69 1
                'marketSymbol' => $market,
70 1
                'direction' => 'SELL',
71 1
                'type' => 'LIMIT',
72 1
                'quantity' => $quantity,
73 1
                'limit' => $rate,
74 1
                'timeInForce' => 'GOOD_TIL_CANCELLED',
75 1
                'useAwards' => $useAwards
76
            ]];
77 1
        $this->batchQueue->push($addOrder);
78 1
    }
79
80
    /** https://bittrex.github.io/api/v3#operation--batch-post
81
     * https://bittrex.github.io/api/v3#operation--orders--orderId--delete
82
     * @param string $uuid
83
     */
84 1
    public function addCancel(string $uuid)
85
    {
86
        $addOrder = [
87 1
            "resource" => "order",
88 1
            "operation" => "delete",
89
            'payload' => [
90 1
                'id' => $uuid
91
            ]];
92 1
        $this->batchQueue->push($addOrder);
93 1
    }
94
95
    /** https://bittrex.github.io/api/v3#operation--batch-post
96
     * Create a new batch request. Currently batch requests are limited to placing and cancelling orders. The request model corresponds to the equivalent individual operations.
97
     * 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.
98
     * The status and response payload are the same as the responses would be if individual API requests were made for each operation
99
     * @throws TransformResponseException | GuzzleException
100
     */
101 1
    public function executeBatch(): array
102
    {
103 1
        $queue = [];
104 1
        while (!$this->batchQueue->isEmpty())
105 1
            $queue[] = $this->batchQueue->pop();
106 1
        $options = ['body' => json_encode($queue)];
107 1
        return $this->rest('POST', '/batch', $options);
108
    }
109
}