Completed
Push — master ( 3347ae...8ec7fe )
by Romain
9s
created

BoardingPass::addAuxiliaryFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
namespace Kerox\Messenger\Model\Message\Attachment\Template\Airline;
3
4
use Kerox\Messenger\ValidatorTrait;
5
6
class BoardingPass implements \JsonSerializable, TravelClassInterface
7
{
8
9
    use ValidatorTrait;
10
11
    /**
12
     * @var string
13
     */
14
    protected $passengerName;
15
16
    /**
17
     * @var string
18
     */
19
    protected $pnrNumber;
20
21
    /**
22
     * @var null|string
23
     */
24
    protected $travelClass;
25
26
    /**
27
     * @var null|string
28
     */
29
    protected $seat;
30
31
    /**
32
     * @var array
33
     */
34
    protected $auxiliaryFields = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $secondaryFields = [];
40
41
    /**
42
     * @var string
43
     */
44
    protected $logoImageUrl;
45
46
    /**
47
     * @var null|string
48
     */
49
    protected $headerImageUrl;
50
51
    /**
52
     * @var null|string
53
     */
54
    protected $headerTextField;
55
56
    /**
57
     * @var null|string
58
     */
59
    protected $qrCode;
60
61
    /**
62
     * @var null|string
63
     */
64
    protected $barcodeImageUrl;
65
66
    /**
67
     * @var string
68
     */
69
    protected $aboveBarcodeImageUrl;
70
71
    /**
72
     * @var FlightInfo;
73
     */
74
    protected $flightInfo;
75
76
77
    public function __construct(
78
        string $passengerName,
79
        string $pnrNumber,
80
        string $logoImageUrl,
81
        string $code,
82
        string $aboveBarcodeImageUrl,
83
        FlightInfo $flightInfo
84
    ) {
85
        $this->passengerName = $passengerName;
86
        $this->pnrNumber = $pnrNumber;
87
        $this->logoImageUrl = $logoImageUrl;
88
        $this->aboveBarcodeImageUrl = $aboveBarcodeImageUrl;
89
        $this->flightInfo = $flightInfo;
90
91
        $this->setCode($code);
92
    }
93
94
    /**
95
     * @param string $travelClass
96
     * @return BoardingPass
97
     */
98
    public function setTravelClass(string $travelClass): BoardingPass
99
    {
100
        $this->isValidTravelClass($travelClass);
101
        $this->travelClass = $travelClass;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param string $seat
108
     * @return BoardingPass
109
     */
110
    public function setSeat(string $seat): BoardingPass
111
    {
112
        $this->seat = $seat;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param string $label
119
     * @param string $value
120
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
121
     * @internal param array|null $auxiliaryFields
122
     */
123
    public function addAuxiliaryFields(string $label, string $value): BoardingPass
124
    {
125
        $this->auxiliaryFields[] = $this->setLabelValue($label, $value);
126
        $this->isValidArray($this->auxiliaryFields, 5);
127
128
        return $this;
129
    }
130
131
    /**
132
     * @param string $label
133
     * @param string $value
134
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
135
     * @internal param array|null $secondaryFields
136
     */
137
    public function addSecondaryFields(string $label, string $value): BoardingPass
138
    {
139
        $this->secondaryFields[] = $this->setLabelValue($label, $value);
140
        $this->isValidArray($this->secondaryFields, 5);
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param string $headerImageUrl
147
     * @return BoardingPass
148
     */
149
    public function setHeaderImageUrl(string $headerImageUrl): BoardingPass
150
    {
151
        $this->isValidUrl($headerImageUrl);
152
        $this->headerImageUrl = $headerImageUrl;
153
154
        return $this;
155
    }
156
157
    /**
158
     * @param string $headerTextField
159
     * @return BoardingPass
160
     */
161
    public function setHeaderTextField(string $headerTextField): BoardingPass
162
    {
163
        $this->headerTextField = $headerTextField;
164
165
        return $this;
166
    }
167
168
    /**
169
     * @param string $label
170
     * @param string $value
171
     * @return array
172
     */
173
    private function setLabelValue(string $label, string $value): array
174
    {
175
        return [
176
            'label' => $label,
177
            'value' => $value,
178
        ];
179
    }
180
181
    /**
182
     * @param string $code
183
     * @return void
184
     */
185
    private function setCode(string $code)
186
    {
187
        if (filter_var($code, FILTER_VALIDATE_URL)) {
188
            $this->barcodeImageUrl = $code;
189
        } else {
190
            $this->qrCode = $code;
191
        }
192
    }
193
194
    /**
195
     * @param string $travelClass
196
     * @return void
197
     * @throws \InvalidArgumentException
198
     */
199
    public function isValidTravelClass(string $travelClass)
200
    {
201
        $allowedTravelClass = $this->getAllowedTravelClass();
202
        if (!in_array($travelClass, $allowedTravelClass)) {
203
            throw new \InvalidArgumentException('$travelClass must be either ' . implode(', ', $allowedTravelClass));
204
        }
205
    }
206
207
    /**
208
     * @return array
209
     */
210
    public function getAllowedTravelClass(): array
211
    {
212
        return [
213
            self::ECONOMY,
214
            self::BUSINESS,
215
            self::FIRST_CLASS,
216
        ];
217
    }
218
219
    /**
220
     * @return array
221
     */
222
    public function jsonSerialize(): array
223
    {
224
        $json = [
225
            'passenger_name' => $this->passengerName,
226
            'pnr_number' => $this->pnrNumber,
227
            'travel_class' => $this->travelClass,
228
            'seat' => $this->seat,
229
            'auxiliary_fields' => $this->auxiliaryFields,
230
            'secondary_fields' => $this->secondaryFields,
231
            'logo_image_url' => $this->logoImageUrl,
232
            'header_image_url' => $this->headerImageUrl,
233
            'header_text_field' => $this->headerTextField,
234
            'qr_code' => $this->qrCode,
235
            'barcode_image_url' => $this->barcodeImageUrl,
236
            'above_bar_code_image_url' => $this->aboveBarcodeImageUrl,
237
            'flight_info' => $this->flightInfo,
238
        ];
239
240
        return array_filter($json);
241
    }
242
}
243