Passed
Push — master ( 8ca73d...2eaf19 )
by Vladislav
02:37 queued 15s
created

OrderBookResponse::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 28
rs 9.6666
1
<?php
2
3
namespace Carpenstar\ByBitAPI\WebSockets\Derivatives\PublicChannels\OrderBook\Entities;
4
5
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder;
6
use Carpenstar\ByBitAPI\Core\Interfaces\ICollectionInterface;
7
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection;
8
use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse;
9
10
class OrderBookResponse extends AbstractResponse
11
{
12
    /**
13
     * Topic name
14
     * @var string $topic
15
     */
16
    private ?string $topic;
17
18
    /**
19
     * Message type. snapshot,delta
20
     * @var string $type
21
     */
22
    private ?string $type;
23
24
    /**
25
     * @var string
26
     */
27
    private string $symbol;
28
29
    /**
30
     * @var \DateTime $timestamp
31
     */
32
    private \DateTime $timestamp;
0 ignored issues
show
introduced by
The private property $timestamp is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var ICollectionInterface $bid
36
     */
37
    private ICollectionInterface $bid;
38
39
    /**
40
     * @var ICollectionInterface $ask
41
     */
42
    private ICollectionInterface $ask;
43
44
    /**
45
     * Update id, is always in sequence. Occasionally, you'll receive "u"=1,
46
     * which is a snapshot data due to the restart of the service.
47
     * So please overwrite the locally saved orderbook
48
     * @var null|int $updateId
49
     */
50
    private ?int $updateId;
51
52
    /**
53
     * @var null|int $crossSequence
54
     */
55
    private ?int $crossSequence;
56
57
    public function __construct(array $data)
58
    {
59
        $askCollection = new EntityCollection();
60
        $bidCollection = new EntityCollection();
61
62
        $this->topic = $data['topic'] ?? null;
63
        $this->type = $data['type'] ?? null;
64
65
        if (!empty($data['data'])) {
66
            if (!empty($data['data']['a'])) {
67
                array_map(function ($askItem) use ($askCollection) {
68
                    $askCollection->push(ResponseDtoBuilder::make(OrderBookResponseItem::class, $askItem));
69
                }, $data['data']['a']);
70
            }
71
    
72
            if (!empty($data['data']['b'])) {
73
                array_map(function ($bidItem) use ($bidCollection) {
74
                    $bidCollection->push(ResponseDtoBuilder::make(OrderBookResponseItem::class, $bidItem));
75
                }, $data['data']['b']);
76
            }
77
78
            $this->symbol = $data['data']['s'];
79
            $this->updateId = $data['data']['u'];
80
            $this->crossSequence = $data['data']['seq'];
81
        }
82
83
        $this->bid = $bidCollection;
84
        $this->ask = $askCollection;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getTopic(): ?string
91
    {
92
        return $this->topic;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getType(): ?string
99
    {
100
        return $this->type;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getAsk(): array
107
    {
108
        return $this->ask->all();
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getSymbol(): string
115
    {
116
        return $this->symbol;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function getBid(): array
123
    {
124
        return $this->bid->all();
125
    }
126
127
    /**
128
     * @return int
129
     */
130
    public function getUpdateId(): int
131
    {
132
        return $this->updateId;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->updateId could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
133
    }
134
135
    /**
136
     * @return int
137
     */
138
    public function getCrossSequence(): int
139
    {
140
        return $this->crossSequence;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->crossSequence could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
141
    }
142
}
143