Test Failed
Pull Request — master (#20)
by Vladislav
08:15
created

OrderBookResponse::getAsk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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