OrderBookResponse::setAsk()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Carpenstar\ByBitAPI\Derivatives\MarketData\OrderBook\Response;
4
5
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder;
6
use Carpenstar\ByBitAPI\Core\Helpers\DateTimeHelper;
7
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection;
8
use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse;
9
use Carpenstar\ByBitAPI\Derivatives\MarketData\OrderBook\Interfaces\IOrderBookResponsePriceItemInterface;
10
11
class OrderBookResponse extends AbstractResponse
12
{
13
    private string $symbol;
14
15
    private \DateTime $timestamp;
16
17
    private int $updateId;
18
19
    private EntityCollection $bid;
20
21
    private EntityCollection $ask;
22
23
    public function __construct(array $data)
24
    {
25
        $this->bid = new EntityCollection();
26
        $this->ask = new EntityCollection();
27
28
        $this->symbol = $data['s'];
29
        $this->timestamp = DateTimeHelper::makeFromTimestamp($data['ts']);
30
        $this->updateId = $data['u'];
31
32
        $this
33
            ->setBid($data['b'])
34
            ->setAsk($data['a']);
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getSymbol(): string
41
    {
42
        return $this->symbol;
43
    }
44
45
    /**
46
     * @return \DateTime
47
     */
48
    public function getTimestamp(): \DateTime
49
    {
50
        return $this->timestamp;
51
    }
52
53
    /**
54
     * @return int
55
     */
56
    public function getUpdateId(): int
57
    {
58
        return $this->updateId;
59
    }
60
61
    private function setBid(?array $bids = []): self
62
    {
63
        $bidList = $this->bid;
64
65
        if (!empty($bids)) {
66
            array_map(function ($bid) use ($bidList) {
67
                $bidList->push(ResponseDtoBuilder::make(OrderBookPriceItemResponse::class, $bid));
68
            }, $bids);
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return IOrderBookResponsePriceItemInterface[]
76
     */
77
    public function getBid(): ?array
78
    {
79
        return $this->bid->all();
80
    }
81
82
    private function setAsk(?array $asks = []): self
83
    {
84
        $askList = $this->ask;
85
86
        if (!empty($asks)) {
87
            array_map(function ($ask) use ($askList) {
88
                $askList->push(ResponseDtoBuilder::make(OrderBookPriceItemResponse::class, $ask));
89
            }, $asks);
90
        }
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return IOrderBookResponsePriceItemInterface[]
97
     */
98
    public function getAsk(): ?array
99
    {
100
        return $this->ask->all();
101
    }
102
}
103