AirlineItineraryTemplate::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template;
6
7
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;
8
9
class AirlineItineraryTemplate extends AbstractAirlineTemplate
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $introMessage;
15
16
    /**
17
     * @var string
18
     */
19
    protected $pnrNumber;
20
21
    /**
22
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Airline\PassengerInfo[]
23
     */
24
    protected $passengerInfo;
25
26
    /**
27
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Airline\ExtendedFlightInfo[]
28
     */
29
    protected $flightInfo;
30
31
    /**
32
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Airline\PassengerSegmentInfo[]
33
     */
34
    protected $passengerSegmentInfo;
35
36
    /**
37
     * @var array
38
     */
39
    protected $priceInfo = [];
40
41
    /**
42
     * @var string|null
43
     */
44
    protected $basePrice;
45
46
    /**
47
     * @var string|null
48
     */
49
    protected $tax;
50
51
    /**
52
     * @var string
53
     */
54
    protected $totalPrice;
55
56
    /**
57
     * @var string
58
     */
59
    protected $currency;
60
61
    /**
62
     * AirlineItinerary constructor.
63
     *
64
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\PassengerInfo[]        $passengerInfo
65
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\ExtendedFlightInfo[]   $flightInfo
66
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\PassengerSegmentInfo[] $passengerSegmentInfo
67
     */
68 1
    public function __construct(
69
        string $introMessage,
70
        string $locale,
71
        string $pnrNumber,
72
        array $passengerInfo,
73
        array $flightInfo,
74
        array $passengerSegmentInfo,
75
        string $totalPrice,
76
        string $currency
77
    ) {
78 1
        parent::__construct($locale);
79
80 1
        $this->introMessage = $introMessage;
81 1
        $this->pnrNumber = $pnrNumber;
82 1
        $this->passengerInfo = $passengerInfo;
83 1
        $this->flightInfo = $flightInfo;
84 1
        $this->passengerSegmentInfo = $passengerSegmentInfo;
85 1
        $this->totalPrice = $totalPrice;
86 1
        $this->currency = $currency;
87 1
    }
88
89
    /**
90
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineItineraryTemplate
91
     */
92 1
    public static function create(
93
        string $introMessage,
94
        string $locale,
95
        string $pnrNumber,
96
        array $passengerInfo,
97
        array $flightInfo,
98
        array $passengerSegmentInfo,
99
        string $totalPrice,
100
        string $currency
101
    ): self {
102 1
        return new self(
103 1
            $introMessage,
104
            $locale,
105
            $pnrNumber,
106
            $passengerInfo,
107
            $flightInfo,
108
            $passengerSegmentInfo,
109
            $totalPrice,
110
            $currency
111
        );
112
    }
113
114
    /**
115
     * @param string $currency
116
     *
117
     *@throws \Kerox\Messenger\Exception\MessengerException
118
     *
119
     *@return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineItineraryTemplate
120
     */
121 1
    public function addPriceInfo(string $title, string $amount, ?string $currency = null): self
122
    {
123 1
        if ($currency !== null) {
124 1
            $this->isValidCurrency($currency);
125
        }
126
127
        $priceInfo = [
128 1
            'title' => $title,
129 1
            'amount' => $amount,
130 1
            'currency' => $currency,
131
        ];
132
133 1
        $this->isValidArray($this->priceInfo, 4);
134
135 1
        $this->priceInfo[] = array_filter($priceInfo);
136
137 1
        return $this;
138
    }
139
140
    /**
141
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineItineraryTemplate
142
     */
143 1
    public function setBasePrice(string $basePrice): self
144
    {
145 1
        $this->basePrice = $basePrice;
146
147 1
        return $this;
148
    }
149
150
    /**
151
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineItineraryTemplate
152
     */
153 1
    public function setTax(string $tax): self
154
    {
155 1
        $this->tax = $tax;
156
157 1
        return $this;
158
    }
159
160 1
    public function toArray(): array
161
    {
162 1
        $array = parent::toArray();
163
        $array += [
164
            'payload' => [
165 1
                'template_type' => AbstractTemplate::TYPE_AIRLINE_ITINERARY,
166 1
                'intro_message' => $this->introMessage,
167 1
                'locale' => $this->locale,
168 1
                'theme_color' => $this->themeColor,
169 1
                'pnr_number' => $this->pnrNumber,
170 1
                'passenger_info' => $this->passengerInfo,
171 1
                'flight_info' => $this->flightInfo,
172 1
                'passenger_segment_info' => $this->passengerSegmentInfo,
173 1
                'price_info' => $this->priceInfo,
174 1
                'base_price' => $this->basePrice,
175 1
                'tax' => $this->tax,
176 1
                'total_price' => $this->totalPrice,
177 1
                'currency' => $this->currency,
178
            ],
179
        ];
180
181 1
        return $this->arrayFilter($array);
182
    }
183
}
184