Passed
Push — master ( 7a134a...be08d2 )
by Vladislav
02:35 queued 13s
created

OrderBookResponse   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 26
dl 0
loc 100
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setTime() 0 4 1
A setBids() 0 4 1
A getAsks() 0 3 1
A getTime() 0 3 1
A setAsks() 0 4 1
A getBids() 0 3 1
A __construct() 0 21 3
1
<?php
2
namespace Carpenstar\ByBitAPI\Spot\MarketData\OrderBook\Response;
3
4
use Carpenstar\ByBitAPI\Core\Builders\ResponseBuilder;
5
use Carpenstar\ByBitAPI\Core\Helpers\DateTimeHelper;
6
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection;
7
use Carpenstar\ByBitAPI\Core\Objects\ResponseEntity;
8
use Carpenstar\ByBitAPI\Spot\MarketData\OrderBook\Interfaces\IOrderBookResponse;
9
10
class OrderBookResponse extends ResponseEntity implements IOrderBookResponse
11
{
12
    /**
13
     * Current time, unit in millisecond
14
     * @var \DateTime $time
15
     */
16
    protected \DateTime $time;
17
18
    /**
19
     * Bid price and quantity, with best bid prices ranked from top to bottom
20
     * @var EntityCollection $bids
21
     */
22
    protected EntityCollection $bids;
23
24
    /**
25
     * Ask price and quantity, with best ask prices ranked from top to bottom
26
     * @var EntityCollection $asks
27
     */
28
    protected EntityCollection $asks;
29
30
    /**
31
     * @param array $data
32
     * @throws \Exception
33
     */
34
    public function __construct(array $data)
35
    {
36
        $bids = new EntityCollection();
37
        $asks = new EntityCollection();
38
39
        $this->setTime($data['time']);
40
41
        if (!empty($data['bids'])) {
42
            array_map(function ($bid) use ($bids) {
43
                $bids->push(ResponseBuilder::make(OrderBookPriceItemResponse::class, $bid));
44
            }, $data['bids']);
45
        }
46
47
        if (!empty($data['asks'])) {
48
            array_map(function ($ask) use ($asks) {
49
                $asks->push(ResponseBuilder::make(OrderBookPriceItemResponse::class, $ask));
50
            }, $data['asks']);
51
        }
52
53
        $this->setBids($bids);
54
        $this->setAsks($asks);
55
    }
56
57
    /**
58
     * @param string $time
59
     * @return OrderBookResponse
60
     * @throws \Exception
61
     */
62
    private function setTime(string $time): self
63
    {
64
        $this->time = DateTimeHelper::makeFromTimestamp($time);
0 ignored issues
show
Bug introduced by
$time of type string is incompatible with the type integer expected by parameter $timestamp of Carpenstar\ByBitAPI\Core...er::makeFromTimestamp(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $this->time = DateTimeHelper::makeFromTimestamp(/** @scrutinizer ignore-type */ $time);
Loading history...
65
        return $this;
66
    }
67
68
    /**
69
     * @return \DateTime
70
     */
71
    public function getTime(): \DateTime
72
    {
73
        return $this->time;
74
    }
75
76
    /**
77
     * @param EntityCollection $asks
78
     * @return OrderBookResponse
79
     */
80
    private function setAsks(EntityCollection $asks): self
81
    {
82
        $this->asks = $asks;
83
        return $this;
84
    }
85
86
    /**
87
     * @return EntityCollection
88
     */
89
    public function getAsks(): EntityCollection
90
    {
91
        return $this->asks;
92
    }
93
94
    /**
95
     * @param EntityCollection $bids
96
     * @return OrderBookResponse
97
     */
98
    private function setBids(EntityCollection $bids): self
99
    {
100
        $this->bids = $bids;
101
        return $this;
102
    }
103
104
    /**
105
     * @return EntityCollection
106
     */
107
    public function getBids(): EntityCollection
108
    {
109
        return $this->bids;
110
    }
111
}