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

OrderBookResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 104
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 27 3
A getAsk() 0 3 1
A getSymbol() 0 3 1
A getResponseTime() 0 3 1
A getRequestTime() 0 3 1
A getBid() 0 3 1
A getTopic() 0 3 1
A getType() 0 3 1
1
<?php
2
3
namespace Carpenstar\ByBitAPI\WebSockets\Spot\PublicChannels\OrderBook\Entities;
4
5
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder;
6
use Carpenstar\ByBitAPI\Core\Helpers\DateTimeHelper;
7
use Carpenstar\ByBitAPI\Core\Interfaces\ICollectionInterface;
8
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection;
9
use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse;
10
11
class OrderBookResponse extends AbstractResponse
12
{
13
    /**
14
     * Topic name
15
     * @var string $topic
16
     */
17
    private string $topic;
18
19
    /**
20
     * Data type. snapshot
21
     * @var string $type
22
     */
23
    private string $type;
24
25
    /**
26
     * Trading pair
27
     * @var string $symbol
28
     */
29
    private string $symbol;
30
31
    /**
32
     * The timestamp that message is sent out
33
     * @var \DateTime $requestTime
34
     */
35
    private \DateTime $requestTime;
36
37
    /**
38
     * The timestamp that system generates the data.
39
     * @var \DateTime $responseTime
40
     */
41
    private \DateTime $responseTime;
42
43
    /**
44
     * @var ICollectionInterface $ask
45
     */
46
    private ICollectionInterface $ask;
47
48
    /**
49
     * @var ICollectionInterface $bid
50
     */
51
    private ICollectionInterface $bid;
52
53
    public function __construct(array $data)
54
    {
55
        $this->topic = $data['topic'];
56
        $this->type = $data['type'];
57
        $this->symbol = $data['data']['s'];
58
        $this->requestTime = DateTimeHelper::makeFromTimestamp($data['ts']);
59
        $this->responseTime = DateTimeHelper::makeFromTimestamp($data['data']['t']);
60
61
        $bidCollection = new EntityCollection();
62
63
        if (!empty($data['data']['b'])) {
64
            array_map(function ($item) use ($bidCollection) {
65
                $bidCollection->push(ResponseDtoBuilder::make(OrderBookResponseItem::class, $item));
66
            }, $data['data']['b']);
67
        }
68
69
        $this->bid = $bidCollection;
70
71
        $askCollection = new EntityCollection();
72
73
        if (!empty($data['data']['a'])) {
74
            array_map(function ($item) use ($askCollection) {
75
                $askCollection->push(ResponseDtoBuilder::make(OrderBookResponseItem::class, $item));
76
            }, $data['data']['a']);
77
        }
78
79
        $this->ask = $askCollection;
80
    }
81
82
    public function getTopic(): string
83
    {
84
        return $this->topic;
85
    }
86
87
    public function getType(): string
88
    {
89
        return $this->type;
90
    }
91
92
    public function getSymbol(): string
93
    {
94
        return $this->symbol;
95
    }
96
97
    public function getRequestTime(): \DateTime
98
    {
99
        return $this->requestTime;
100
    }
101
102
    public function getResponseTime(): \DateTime
103
    {
104
        return $this->responseTime;
105
    }
106
107
    public function getAsk(): array
108
    {
109
        return $this->ask->all();
110
    }
111
112
    public function getBid(): array
113
    {
114
        return $this->bid->all();
115
    }
116
}
117