BoardingPass::addSecondaryFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template\Airline;
6
7
use Kerox\Messenger\Exception\InvalidKeyException;
8
use Kerox\Messenger\Helper\ValidatorTrait;
9
10
class BoardingPass implements \JsonSerializable, TravelClassInterface
11
{
12
    use ValidatorTrait;
13
14
    /**
15
     * @var string
16
     */
17
    protected $passengerName;
18
19
    /**
20
     * @var string
21
     */
22
    protected $pnrNumber;
23
24
    /**
25
     * @var string|null
26
     */
27
    protected $travelClass;
28
29
    /**
30
     * @var string|null
31
     */
32
    protected $seat;
33
34
    /**
35
     * @var array
36
     */
37
    protected $auxiliaryFields = [];
38
39
    /**
40
     * @var array
41
     */
42
    protected $secondaryFields = [];
43
44
    /**
45
     * @var string
46
     */
47
    protected $logoImageUrl;
48
49
    /**
50
     * @var string|null
51
     */
52
    protected $headerImageUrl;
53
54
    /**
55
     * @var string|null
56
     */
57
    protected $headerTextField;
58
59
    /**
60
     * @var string|null
61
     */
62
    protected $qrCode;
63
64
    /**
65
     * @var string|null
66
     */
67
    protected $barcodeImageUrl;
68
69
    /**
70
     * @var string
71
     */
72
    protected $aboveBarcodeImageUrl;
73
74
    /**
75
     * @var FlightInfo;
76
     */
77
    protected $flightInfo;
78
79
    /**
80
     * BoardingPass constructor.
81
     *
82
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo $flightInfo
83
     */
84 5
    public function __construct(
85
        string $passengerName,
86
        string $pnrNumber,
87
        string $logoImageUrl,
88
        string $code,
89
        string $aboveBarcodeImageUrl,
90
        FlightInfo $flightInfo
91
    ) {
92 5
        $this->passengerName = $passengerName;
93 5
        $this->pnrNumber = $pnrNumber;
94 5
        $this->logoImageUrl = $logoImageUrl;
95 5
        $this->aboveBarcodeImageUrl = $aboveBarcodeImageUrl;
96 5
        $this->flightInfo = $flightInfo;
97
98 5
        $this->setCode($code);
99 5
    }
100
101
    /**
102
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo $flightInfo
103
     *
104
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
105
     */
106 5
    public static function create(
107
        string $passengerName,
108
        string $pnrNumber,
109
        string $logoImageUrl,
110
        string $code,
111
        string $aboveBarcodeImageUrl,
112
        FlightInfo $flightInfo
113
    ): self {
114 5
        return new self($passengerName, $pnrNumber, $logoImageUrl, $code, $aboveBarcodeImageUrl, $flightInfo);
115
    }
116
117
    /**
118
     * @throws \Kerox\Messenger\Exception\MessengerException
119
     *
120
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
121
     */
122 2
    public function setTravelClass(string $travelClass): self
123
    {
124 2
        $this->isValidTravelClass($travelClass);
125
126 1
        $this->travelClass = $travelClass;
127
128 1
        return $this;
129
    }
130
131
    /**
132
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
133
     */
134 1
    public function setSeat(string $seat): self
135
    {
136 1
        $this->seat = $seat;
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * @throws \Kerox\Messenger\Exception\MessengerException
143
     *
144
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
145
     */
146 2
    public function addAuxiliaryFields(string $label, string $value): self
147
    {
148 2
        $this->auxiliaryFields[] = $this->setLabelValue($label, $value);
149
150 2
        $this->isValidArray($this->auxiliaryFields, 5);
151
152 2
        return $this;
153
    }
154
155
    /**
156
     * @throws \Kerox\Messenger\Exception\MessengerException
157
     *
158
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
159
     */
160 2
    public function addSecondaryFields(string $label, string $value): self
161
    {
162 2
        $this->secondaryFields[] = $this->setLabelValue($label, $value);
163
164 2
        $this->isValidArray($this->secondaryFields, 5);
165
166 2
        return $this;
167
    }
168
169
    /**
170
     * @throws \Kerox\Messenger\Exception\MessengerException
171
     *
172
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
173
     */
174 1
    public function setHeaderImageUrl(string $headerImageUrl): self
175
    {
176 1
        $this->isValidUrl($headerImageUrl);
177
178 1
        $this->headerImageUrl = $headerImageUrl;
179
180 1
        return $this;
181
    }
182
183
    /**
184
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\BoardingPass
185
     */
186 1
    public function setHeaderTextField(string $headerTextField): self
187
    {
188 1
        $this->headerTextField = $headerTextField;
189
190 1
        return $this;
191
    }
192
193 3
    private function setLabelValue(string $label, string $value): array
194
    {
195
        return [
196 3
            'label' => $label,
197 3
            'value' => $value,
198
        ];
199
    }
200
201 5
    private function setCode(string $code): void
202
    {
203 5
        if (filter_var($code, FILTER_VALIDATE_URL)) {
204 1
            $this->barcodeImageUrl = $code;
205
206 1
            return;
207
        }
208 5
        $this->qrCode = $code;
209 5
    }
210
211
    /**
212
     * @throws \Kerox\Messenger\Exception\MessengerException
213
     */
214 2 View Code Duplication
    public function isValidTravelClass(string $travelClass): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216 2
        $allowedTravelClass = $this->getAllowedTravelClass();
217 2
        if (!\in_array($travelClass, $allowedTravelClass, true)) {
218 1
            throw new InvalidKeyException(sprintf('travelClass must be either "%s".', implode(', ', $allowedTravelClass)));
219
        }
220 1
    }
221
222 2
    public function getAllowedTravelClass(): array
223
    {
224
        return [
225 2
            self::ECONOMY,
226 2
            self::BUSINESS,
227 2
            self::FIRST_CLASS,
228
        ];
229
    }
230
231 2
    public function toArray(): array
232
    {
233
        $array = [
234 2
            'passenger_name' => $this->passengerName,
235 2
            'pnr_number' => $this->pnrNumber,
236 2
            'travel_class' => $this->travelClass,
237 2
            'seat' => $this->seat,
238 2
            'auxiliary_fields' => $this->auxiliaryFields,
239 2
            'secondary_fields' => $this->secondaryFields,
240 2
            'logo_image_url' => $this->logoImageUrl,
241 2
            'header_image_url' => $this->headerImageUrl,
242 2
            'header_text_field' => $this->headerTextField,
243 2
            'qr_code' => $this->qrCode,
244 2
            'barcode_image_url' => $this->barcodeImageUrl,
245 2
            'above_bar_code_image_url' => $this->aboveBarcodeImageUrl,
246 2
            'flight_info' => $this->flightInfo,
247
        ];
248
249 2
        return array_filter($array);
250
    }
251
252 2
    public function jsonSerialize(): array
253
    {
254 2
        return $this->toArray();
255
    }
256
}
257