Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
16 | class Batch extends Api |
||
17 | { |
||
18 | /** |
||
19 | * @var Queue |
||
20 | */ |
||
21 | protected Queue $batchQueue; |
||
|
|||
22 | |||
23 | /** |
||
24 | * Api constructor. |
||
25 | */ |
||
26 | public function __construct(Client $client) |
||
27 | { |
||
28 | parent::__construct($client); |
||
29 | $this->batchQueue = new Queue(); |
||
30 | } |
||
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 | public function addBuyLimit(string $market, float $quantity, float $rate, bool $useAwards = false) |
||
40 | { |
||
41 | $addOrder = [ |
||
42 | "resource" => "order", |
||
43 | "operation" => "post", |
||
44 | 'payload' => [ |
||
45 | 'marketSymbol' => $market, |
||
46 | 'direction' => 'BUY', |
||
47 | 'type' => 'LIMIT', |
||
48 | 'quantity' => $quantity, |
||
49 | 'limit' => $rate, |
||
50 | 'timeInForce' => 'GOOD_TIL_CANCELLED', |
||
51 | 'useAwards' => $useAwards |
||
52 | |||
53 | ]]; |
||
54 | $this->batchQueue->push($addOrder); |
||
55 | } |
||
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 | public function addSellLimit(string $market, float $quantity, float $rate, bool $useAwards = false) |
||
65 | { |
||
66 | $addOrder = [ |
||
67 | "resource" => "order", |
||
68 | "operation" => "post", |
||
69 | 'payload' => [ |
||
70 | 'marketSymbol' => $market, |
||
71 | 'direction' => 'SELL', |
||
72 | 'type' => 'LIMIT', |
||
73 | 'quantity' => $quantity, |
||
74 | 'limit' => $rate, |
||
75 | 'timeInForce' => 'GOOD_TIL_CANCELLED', |
||
76 | 'useAwards' => $useAwards |
||
77 | |||
78 | ]]; |
||
79 | $this->batchQueue->push($addOrder); |
||
80 | } |
||
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 | public function addCancel(string $uuid) |
||
87 | { |
||
88 | $addOrder = [ |
||
89 | "resource" => "order", |
||
90 | "operation" => "delete", |
||
91 | 'payload' => [ |
||
92 | 'id' => $uuid |
||
93 | ]]; |
||
94 | $this->batchQueue->push($addOrder); |
||
95 | } |
||
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 | public function executeBatch(): array |
||
104 | { |
||
105 | $queue = []; |
||
106 | while (!$this->batchQueue->isEmpty()) |
||
107 | $queue[] = $this->batchQueue->pop(); |
||
108 | $options = ['body' => json_encode($queue)]; |
||
109 | return $this->rest('POST', '/batch', $options); |
||
110 | } |
||
111 | } |