Test Failed
Pull Request — master (#3)
by Vladislav
20:43 queued 12:42
created

OrderBookResponse::setBids()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Carpenstar\ByBitAPI\Spot\MarketData\OrderBook\Dto;
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
9
class OrderBookResponse extends ResponseEntity
10
{
11
    /**
12
     * Current time, unit in millisecond
13
     * @var \DateTime $time
14
     */
15
    protected \DateTime $time;
16
17
    /**
18
     * Bid price and quantity, with best bid prices ranked from top to bottom
19
     * @var EntityCollection $bids
20
     */
21
    protected EntityCollection $bids;
22
23
    /**
24
     * Ask price and quantity, with best ask prices ranked from top to bottom
25
     * @var EntityCollection $asks
26
     */
27
    protected EntityCollection $asks;
28
29
    /**
30
     * @param array $data
31
     * @throws \Exception
32
     */
33
    public function __construct(array $data)
34
    {
35
        $bids = new EntityCollection();
36
        $asks = new EntityCollection();
37
38
        $this->setTime($data['time']);
39
40
        if (!empty($data['bids'])) {
41
            array_map(function ($bid) use ($bids) {
42
                $bids->push(ResponseBuilder::make(OrderBookPriceItemResponse::class, $bid));
43
            }, $data['bids']);
44
        }
45
46
        if (!empty($data['asks'])) {
47
            array_map(function ($ask) use ($asks) {
48
                $asks->push(ResponseBuilder::make(OrderBookPriceItemResponse::class, $ask));
49
            }, $data['asks']);
50
        }
51
52
        $this->setBids($bids);
53
        $this->setAsks($asks);
54
    }
55
56
    /**
57
     * @param string $time
58
     * @return OrderBookResponse
59
     * @throws \Exception
60
     */
61
    private function setTime(string $time): self
62
    {
63
        $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

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