Completed
Pull Request — master (#1)
by Romain
02:17
created

AirlineUpdate::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
namespace Kerox\Messenger\Model\Message\Attachment\Template;
3
4
use Kerox\Messenger\Model\Message\Attachment\Template;
5
use Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo;
6
7
class AirlineUpdate extends AbstractAirline
8
{
9
10
    const UPDATE_TYPE_DELAY = 'delay';
11
    const UPDATE_TYPE_GATE_CHANGE = 'gate_change';
12
    const UPDATE_TYPE_CANCELLATION = 'cancellation';
13
14
    /**
15
     * @var null|string
16
     */
17
    protected $introMessage;
18
19
    /**
20
     * @var string
21
     */
22
    protected $updateType;
23
24
    /**
25
     * @var string
26
     */
27
    protected $pnrNumber;
28
29
    /**
30
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo
31
     */
32
    protected $updateFlightInfo;
33
34
    /**
35
     * AirlineUpdate constructor.
36
     *
37
     * @param string $updateType
38
     * @param string $locale
39
     * @param string $pnrNumber
40
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo $updateFlightInfo
41
     */
42
    public function __construct(string $updateType, string $locale, string $pnrNumber, FlightInfo $updateFlightInfo)
43
    {
44
        parent::__construct($locale);
45
46
        $this->isValidUpdateType($updateType);
47
48
        $this->updateType = $updateType;
49
        $this->pnrNumber = $pnrNumber;
50
        $this->updateFlightInfo = $updateFlightInfo;
51
    }
52
53
    /**
54
     * @param string $introMessage
55
     * @return AirlineUpdate
56
     */
57
    public function setIntroMessage($introMessage): AirlineUpdate
58
    {
59
        $this->introMessage = $introMessage;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $updateType
66
     * @return void
67
     * @throws \InvalidArgumentException
68
     */
69
    private function isValidUpdateType(string $updateType)
70
    {
71
        $allowedUpdateType = $this->getAllowedUpdateType();
72
        if (!in_array($updateType, $allowedUpdateType)) {
73
            throw new \InvalidArgumentException('Allowed values for $updateType are: ' . implode(',', $allowedUpdateType));
74
        }
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    private function getAllowedUpdateType(): array
81
    {
82
        return [
83
            self::UPDATE_TYPE_DELAY,
84
            self::UPDATE_TYPE_GATE_CHANGE,
85
            self::UPDATE_TYPE_CANCELLATION,
86
        ];
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function jsonSerialize(): array
93
    {
94
        $payload = [
95
            'template_type' => Template::TYPE_AIRLINE_UPDATE,
96
            'intro_message' => $this->introMessage,
97
            'update_type' => $this->updateType,
98
            'locale' => $this->locale,
99
            'pnr_number' => $this->pnrNumber,
100
            'update_flight_info' => $this->updateFlightInfo,
101
        ];
102
103
        $json = parent::jsonSerialize();
104
        $json += [
105
            'payload' => array_filter($payload),
106
        ];
107
108
        return $json;
109
    }
110
}
111