Completed
Pull Request — master (#40)
by Romain
02:29
created

AirlineItinerary::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

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.4285
c 0
b 0
f 0
cc 1
eloc 17
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
namespace Kerox\Messenger\Model\Message\Attachment\Template;
3
4
use Kerox\Messenger\Model\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\Model\Message\Attachment\Template\Airline\PassengerInfo[]
21
     */
22
    protected $passengerInfo;
23
24
    /**
25
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Airline\ExtendedFlightInfo[]
26
     */
27
    protected $flightInfo;
28
29
    /**
30
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Airline\PassengerSegmentInfo[]
31
     */
32
    protected $passengerSegmentInfo;
33
34
    /**
35
     * @var array
36
     */
37
    protected $priceInfo = [];
38
39
    /**
40
     * @var null|string
41
     */
42
    protected $basePrice;
43
44
    /**
45
     * @var null|string
46
     */
47
    protected $tax;
48
49
    /**
50
     * @var string
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\Model\Message\Attachment\Template\Airline\PassengerInfo[] $passengerInfo
66
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\ExtendedFlightInfo[] $flightInfo
67
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\PassengerSegmentInfo[] $passengerSegmentInfo
68
     * @param string $totalPrice
69
     * @param string $currency
70
     */
71 1
    public function __construct(
72
        string $introMessage,
73
        string $locale,
74
        string $pnrNumber,
75
        array $passengerInfo,
76
        array $flightInfo,
77
        array $passengerSegmentInfo,
78
        string $totalPrice,
79
        string $currency
80
    ) {
81 1
        parent::__construct($locale);
82
83 1
        $this->introMessage = $introMessage;
84 1
        $this->pnrNumber = $pnrNumber;
85 1
        $this->passengerInfo = $passengerInfo;
86 1
        $this->flightInfo = $flightInfo;
87 1
        $this->passengerSegmentInfo = $passengerSegmentInfo;
88 1
        $this->totalPrice = $totalPrice;
89 1
        $this->currency = $currency;
90 1
    }
91
92
    /**
93
     * @param string $title
94
     * @param string $amount
95
     * @param string $currency
96
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineItinerary
97
     * @internal param array|null $priceInfo
98
     */
99 1
    public function addPriceInfo(string $title, string $amount, string $currency = null): AirlineItinerary
100
    {
101 1
        if ($currency !== null) {
102 1
            $this->isValidCurrency($currency);
103
        }
104
105
        $priceInfo = [
106 1
            'title' => $title,
107 1
            'amount' => $amount,
108 1
            'currency' => $currency,
109
        ];
110
111 1
        $this->isValidArray($this->priceInfo, 4);
112 1
        $this->priceInfo[] = array_filter($priceInfo);
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * @param string $basePrice
119
     * @return AirlineItinerary
120
     */
121 1
    public function setBasePrice(string $basePrice): AirlineItinerary
122
    {
123 1
        $this->basePrice = $basePrice;
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * @param string $tax
130
     * @return AirlineItinerary
131
     */
132 1
    public function setTax(string $tax): AirlineItinerary
133
    {
134 1
        $this->tax = $tax;
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * @return array
141
     */
142 1
    public function jsonSerialize(): array
143
    {
144 1
        $json = parent::jsonSerialize();
145
        $json += [
146
            'payload' => [
147 1
                'template_type' => Template::TYPE_AIRLINE_ITINERARY,
148 1
                'intro_message' => $this->introMessage,
149 1
                'locale' => $this->locale,
150 1
                'theme_color' => $this->themeColor,
151 1
                'pnr_number' => $this->pnrNumber,
152 1
                'passenger_info' => $this->passengerInfo,
153 1
                'flight_info' => $this->flightInfo,
154 1
                'passenger_segment_info' => $this->passengerSegmentInfo,
155 1
                'price_info' => $this->priceInfo,
156 1
                'base_price' => $this->basePrice,
157 1
                'tax' => $this->tax,
158 1
                'total_price' => $this->totalPrice,
159 1
                'currency' => $this->currency,
160
            ],
161
        ];
162
163 1
        return $this->arrayFilter($json);
164
    }
165
}
166