Completed
Branch develop (a0a623)
by Romain
02:33
created

AirlineItinerary::jsonSerialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
namespace Kerox\Messenger\Message\Attachment\Template;
3
4
use Kerox\Messenger\Message\Attachment\Template;
5
6
class AirlineItinerary extends AbstractAirline
7
{
8
9
    /**
10
     * @var string
11
     */
12
    protected $introMessage;
13
14
    /**
15
     * @var string
16
     */
17
    protected $pnrNumber;
18
19
    /**
20
     * @var \Kerox\Messenger\Message\Attachment\Template\Airline\PassengerInfo[]
21
     */
22
    protected $passengerInfo;
23
24
    /**
25
     * @var \Kerox\Messenger\Message\Attachment\Template\Airline\ExtendedFlightInfo[]
26
     */
27
    protected $flightInfo;
28
29
    /**
30
     * @var \Kerox\Messenger\Message\Attachment\Template\Airline\PassengerSegmentInfo[]
31
     */
32
    protected $passengerSegmentInfo;
33
34
    /**
35
     * @var array
36
     */
37
    protected $priceInfo = [];
38
39
    /**
40
     * @var null|float
41
     */
42
    protected $basePrice;
43
44
    /**
45
     * @var null|float
46
     */
47
    protected $tax;
48
49
    /**
50
     * @var float
51
     */
52
    protected $totalPrice;
53
54
    /**
55
     * @var string
56
     */
57
    protected $currency;
58
59
    /**
60
     * AirlineItinerary constructor.
61
     *
62
     * @param string $introMessage
63
     * @param string $locale
64
     * @param string $pnrNumber
65
     * @param \Kerox\Messenger\Message\Attachment\Template\Airline\PassengerInfo[] $passengerInfo
66
     * @param \Kerox\Messenger\Message\Attachment\Template\Airline\ExtendedFlightInfo[] $flightInfo
67
     * @param \Kerox\Messenger\Message\Attachment\Template\Airline\PassengerSegmentInfo[] $passengerSegmentInfo
68
     * @param float $totalPrice
69
     * @param string $currency
70
     */
71
    public function __construct(string $introMessage,
72
                                string $locale,
73
                                string $pnrNumber,
74
                                array $passengerInfo,
75
                                array $flightInfo,
76
                                array $passengerSegmentInfo,
77
                                float $totalPrice,
78
                                string $currency
79
    ) {
80
        parent::__construct($locale);
81
82
        $this->introMessage = $introMessage;
83
        $this->pnrNumber = $pnrNumber;
84
        $this->passengerInfo = $passengerInfo;
85
        $this->flightInfo = $flightInfo;
86
        $this->passengerSegmentInfo = $passengerSegmentInfo;
87
        $this->totalPrice = (string)$totalPrice;
0 ignored issues
show
Documentation Bug introduced by
The property $totalPrice was declared of type double, but (string) $totalPrice is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
88
        $this->currency = $currency;
89
    }
90
91
    /**
92
     * @param string $title
93
     * @param float $amount
94
     * @param string $currency
95
     * @return \Kerox\Messenger\Message\Attachment\Template\AirlineItinerary
96
     * @internal param array|null $priceInfo
97
     */
98
    public function addPriceInfo(string $title, float $amount, string $currency = null): AirlineItinerary
99
    {
100
        if ($currency !== null) {
101
            $this->isValidCurrency($currency);
102
        }
103
104
        $priceInfo = [
105
            'title' => $title,
106
            'amount' => $amount,
107
            'currency' => $currency,
108
        ];
109
110
        $this->isValidArray($this->priceInfo, 4);
111
        $this->priceInfo[] = array_filter($priceInfo);
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param float $basePrice
118
     * @return AirlineItinerary
119
     */
120
    public function setBasePrice(float $basePrice): AirlineItinerary
121
    {
122
        $this->basePrice = (string)$basePrice;
0 ignored issues
show
Documentation Bug introduced by
It seems like (string) $basePrice of type string is incompatible with the declared type null|double of property $basePrice.

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...
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param float $tax
129
     * @return AirlineItinerary
130
     */
131
    public function setTax(float $tax): AirlineItinerary
132
    {
133
        $this->tax = (string)$tax;
0 ignored issues
show
Documentation Bug introduced by
It seems like (string) $tax of type string is incompatible with the declared type null|double of property $tax.

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...
134
135
        return $this;
136
    }
137
138
    /**
139
     * @return array
140
     */
141
    public function jsonSerialize(): array
142
    {
143
        $payload = [
144
            'template_type' => Template::TYPE_AIRLINE_ITINERARY,
145
            'intro_message' => $this->introMessage,
146
            'locale' => $this->locale,
147
            'theme_color' => $this->themeColor,
148
            'pnr_number' => $this->pnrNumber,
149
            'passenger_info' => $this->passengerInfo,
150
            'flight_info' => $this->flightInfo,
151
            'passenger_segment_info' => $this->passengerSegmentInfo,
152
            'price_info' => $this->priceInfo,
153
            'base_price' => $this->basePrice,
154
            'tax' => $this->tax,
155
            'total_price' => $this->totalPrice,
156
            'currency' => $this->currency,
157
        ];
158
159
        $json = parent::jsonSerialize();
160
        $json += [
161
            'payload' => array_filter($payload),
162
        ];
163
164
        return $json;
165
    }
166
}