Completed
Branch develop (a0a623)
by Romain
02:33
created

Receipt::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
namespace Kerox\Messenger\Message\Attachment\Template;
3
4
use Kerox\Messenger\Message\Attachment\Template;
5
use Kerox\Messenger\Message\Attachment\Template\Receipt\Address;
6
use Kerox\Messenger\Message\Attachment\Template\Receipt\Summary;
7
8
class Receipt extends Template
9
{
10
11
    /**
12
     * @var string
13
     */
14
    protected $recipientName;
15
16
    /**
17
     * @var string
18
     */
19
    protected $orderNumber;
20
21
    /**
22
     * @var string
23
     */
24
    protected $currency;
25
26
    /**
27
     * @var string
28
     */
29
    protected $paymentMethod;
30
31
    /**
32
     * @var string
33
     */
34
    protected $timestamp;
35
36
    /**
37
     * @var string
38
     */
39
    protected $orderUrl;
40
41
    /**
42
     * @var \Kerox\Messenger\Message\Attachment\Template\Element\ReceiptElement[]
43
     */
44
    protected $elements;
45
46
    /**
47
     * @var null|\Kerox\Messenger\Message\Attachment\Template\Receipt\Address
48
     */
49
    protected $address;
50
51
    /**
52
     * @var \Kerox\Messenger\Message\Attachment\Template\Receipt\Summary
53
     */
54
    protected $summary;
55
56
    /**
57
     * @var null|\Kerox\Messenger\Message\Attachment\Template\Receipt\Adjustment[]
58
     */
59
    protected $adjustments;
60
61
    /**
62
     * Receipt constructor.
63
     *
64
     * @param string $recipientName
65
     * @param string $orderNumber
66
     * @param string $currency
67
     * @param string $paymentMethod
68
     * @param \Kerox\Messenger\Message\Attachment\Template\Element\ReceiptElement[] $elements
69
     * @param \Kerox\Messenger\Message\Attachment\Template\Receipt\Summary $summary
70
     */
71
    public function __construct(string $recipientName,
72
                                string $orderNumber,
73
                                string $currency,
74
                                string $paymentMethod,
75
                                array $elements,
76
                                Summary $summary
77
    ) {
78
        parent::__construct();
79
80
        $this->isValidArray($elements, 100);
81
82
        $this->recipientName = $recipientName;
83
        $this->orderNumber = $orderNumber;
84
        $this->currency = $currency;
85
        $this->paymentMethod = $paymentMethod;
86
        $this->elements = $elements;
87
        $this->summary = $summary;
88
    }
89
90
    /**
91
     * @param string $timestamp
92
     * @return Receipt
93
     */
94
    public function setTimestamp(string $timestamp): Receipt
95
    {
96
        $this->timestamp = $timestamp;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param string $orderUrl
103
     * @return Receipt
104
     */
105
    public function setOrderUrl(string $orderUrl): Receipt
106
    {
107
        $this->isValidUrl($orderUrl);
108
        $this->orderUrl = $orderUrl;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param \Kerox\Messenger\Message\Attachment\Template\Receipt\Address $address
115
     * @return Receipt
116
     */
117
    public function setAddress(Address $address): Receipt
118
    {
119
        $this->address = $address;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param \Kerox\Messenger\Message\Attachment\Template\Receipt\Adjustment[] $adjustments
126
     * @return Receipt
127
     */
128
    public function setAdjustments(array $adjustments): Receipt
129
    {
130
        $this->adjustments = $adjustments;
131
132
        return $this;
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    public function jsonSerialize(): array
139
    {
140
        $payload = [
141
            'template_type' => Template::TYPE_RECEIPT,
142
            'recipient_name' => $this->recipientName,
143
            'order_number' => $this->orderNumber,
144
            'currency' => $this->currency,
145
            'payment_method' => $this->paymentMethod,
146
            'order_url' => $this->orderUrl,
147
            'timestamp' => $this->timestamp,
148
            'elements' => $this->elements,
149
            'address' => $this->address,
150
            'summary' => $this->summary,
151
            'adjustments' => $this->adjustments,
152
        ];
153
154
        $json = parent::jsonSerialize();
155
        $json += [
156
            'payload' => array_filter($payload),
157
        ];
158
159
        return $json;
160
    }
161
}
162