AirlineUpdateTemplate   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 103
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A create() 0 8 1
A setIntroMessage() 0 6 1
A isValidUpdateType() 0 7 2
A getAllowedUpdateType() 0 8 1
A toArray() 0 16 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template;
6
7
use Kerox\Messenger\Exception\InvalidTypeException;
8
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;
9
use Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo;
10
11
class AirlineUpdateTemplate extends AbstractAirlineTemplate
12
{
13
    public const UPDATE_TYPE_DELAY = 'delay';
14
    public const UPDATE_TYPE_GATE_CHANGE = 'gate_change';
15
    public const UPDATE_TYPE_CANCELLATION = 'cancellation';
16
17
    /**
18
     * @var string|null
19
     */
20
    protected $introMessage;
21
22
    /**
23
     * @var string
24
     */
25
    protected $updateType;
26
27
    /**
28
     * @var string
29
     */
30
    protected $pnrNumber;
31
32
    /**
33
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightInfo
34
     */
35
    protected $updateFlightInfo;
36
37
    /**
38
     * AirlineUpdate constructor.
39
     *
40
     * @throws \Kerox\Messenger\Exception\MessengerException
41
     */
42 2
    public function __construct(string $updateType, string $locale, string $pnrNumber, FlightInfo $updateFlightInfo)
43
    {
44 2
        parent::__construct($locale);
45
46 2
        $this->isValidUpdateType($updateType);
47
48 1
        $this->updateType = $updateType;
49 1
        $this->pnrNumber = $pnrNumber;
50 1
        $this->updateFlightInfo = $updateFlightInfo;
51 1
    }
52
53
    /**
54
     *@throws \Kerox\Messenger\Exception\MessengerException
55
     *
56
     *@return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineUpdateTemplate
57
     */
58 2
    public static function create(
59
        string $updateType,
60
        string $locale,
61
        string $pnrNumber,
62
        FlightInfo $updateFlightInfo
63
    ): self {
64 2
        return new self($updateType, $locale, $pnrNumber, $updateFlightInfo);
65
    }
66
67
    /**
68
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\AirlineUpdateTemplate
69
     */
70 1
    public function setIntroMessage(string $introMessage): self
71
    {
72 1
        $this->introMessage = $introMessage;
73
74 1
        return $this;
75
    }
76
77
    /**
78
     * @throws \Kerox\Messenger\Exception\MessengerException
79
     */
80 2
    private function isValidUpdateType(string $updateType): void
81
    {
82 2
        $allowedUpdateType = $this->getAllowedUpdateType();
83 2
        if (!\in_array($updateType, $allowedUpdateType, true)) {
84 1
            throw new InvalidTypeException(sprintf('updateType must be either "%s".', implode(', ', $allowedUpdateType)));
85
        }
86 1
    }
87
88 2
    private function getAllowedUpdateType(): array
89
    {
90
        return [
91 2
            self::UPDATE_TYPE_DELAY,
92 2
            self::UPDATE_TYPE_GATE_CHANGE,
93 2
            self::UPDATE_TYPE_CANCELLATION,
94
        ];
95
    }
96
97 1
    public function toArray(): array
98
    {
99 1
        $array = parent::toArray();
100
        $array += [
101
            'payload' => [
102 1
                'template_type' => AbstractTemplate::TYPE_AIRLINE_UPDATE,
103 1
                'intro_message' => $this->introMessage,
104 1
                'update_type' => $this->updateType,
105 1
                'locale' => $this->locale,
106 1
                'pnr_number' => $this->pnrNumber,
107 1
                'update_flight_info' => $this->updateFlightInfo,
108
            ],
109
        ];
110
111 1
        return $this->arrayFilter($array);
112
    }
113
}
114