MergedOrderBookResponse   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

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

7 Methods

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

65
        $this->time = DateTimeHelper::makeFromTimestamp(/** @scrutinizer ignore-type */ $time);
Loading history...
66
        return $this;
67
    }
68
69
    /**
70
     * @return \DateTime
71
     */
72
    public function getTime(): \DateTime
73
    {
74
        return $this->time;
75
    }
76
77
    /**
78
     * @param EntityCollection $asks
79
     * @return MergedOrderBookResponse
80
     */
81
    private function setAsks(EntityCollection $asks): self
82
    {
83
        $this->asks = $asks;
0 ignored issues
show
Documentation Bug introduced by
It seems like $asks of type Carpenstar\ByBitAPI\Core...ection\EntityCollection is incompatible with the declared type Carpenstar\ByBitAPI\Spot...BookResponseInterface[] of property $asks.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
        return $this;
85
    }
86
87
    /**
88
     * @return EntityCollection
89
     */
90
    public function getAsks(): EntityCollection
91
    {
92
        return $this->asks;
93
    }
94
95
    /**
96
     * @param EntityCollection $bids
97
     * @return MergedOrderBookResponse
98
     */
99
    private function setBids(EntityCollection $bids): self
100
    {
101
        $this->bids = $bids;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bids of type Carpenstar\ByBitAPI\Core...ection\EntityCollection is incompatible with the declared type Carpenstar\ByBitAPI\Spot...BookResponseInterface[] of property $bids.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
102
        return $this;
103
    }
104
105
    /**
106
     * @return EntityCollection
107
     */
108
    public function getBids(): EntityCollection
109
    {
110
        return $this->bids;
111
    }
112
}
113