|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kerox\Messenger\Event; |
|
3
|
|
|
|
|
4
|
|
|
use Kerox\Messenger\Model\Callback\AccountLinking; |
|
5
|
|
|
use Kerox\Messenger\Model\Callback\Delivery; |
|
6
|
|
|
use Kerox\Messenger\Model\Callback\Message; |
|
7
|
|
|
use Kerox\Messenger\Model\Callback\MessageEcho; |
|
8
|
|
|
use Kerox\Messenger\Model\Callback\Optin; |
|
9
|
|
|
use Kerox\Messenger\Model\Callback\Payment; |
|
10
|
|
|
use Kerox\Messenger\Model\Callback\Postback; |
|
11
|
|
|
use Kerox\Messenger\Model\Callback\Read; |
|
12
|
|
|
|
|
13
|
|
|
class EventFactory |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param array $payload |
|
18
|
|
|
* @return \Kerox\Messenger\Event\AbstractEvent |
|
19
|
|
|
*/ |
|
20
|
12 |
|
public static function create(array $payload): AbstractEvent |
|
21
|
|
|
{ |
|
22
|
12 |
|
if (isset($payload['message'])) { |
|
23
|
5 |
|
if (isset($payload['message']['is_echo'])) { |
|
24
|
1 |
|
return self::createMessageEchoEvent($payload); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
4 |
|
return self::createMessageEvent($payload); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
7 |
|
if (isset($payload['postback'])) { |
|
31
|
1 |
|
return self::createPostbackEvent($payload); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
6 |
|
if (isset($payload['optin'])) { |
|
35
|
1 |
|
return self::createOptinEvent($payload); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
5 |
|
if (isset($payload['account_linking'])) { |
|
39
|
1 |
|
return self::createAcountLinkingEvent($payload); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
if (isset($payload['delivery'])) { |
|
43
|
1 |
|
return self::createDeliveryEvent($payload); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
if (isset($payload['read'])) { |
|
47
|
1 |
|
return self::createReadEvent($payload); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
if (isset($payload['payment'])) { |
|
51
|
1 |
|
return self::createPaymentEvent($payload); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return self::createRawEvent($payload); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param array $payload |
|
59
|
|
|
* @return \Kerox\Messenger\Event\RawEvent |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public static function createRawEvent(array $payload): RawEvent |
|
62
|
|
|
{ |
|
63
|
1 |
|
$senderId = $payload['sender']['id']; |
|
64
|
1 |
|
$recipientId = $payload['recipient']['id']; |
|
65
|
1 |
|
unset($payload['sender'], $payload['recipient']); |
|
66
|
|
|
|
|
67
|
1 |
|
return new RawEvent($senderId, $recipientId, $payload); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $payload |
|
72
|
|
|
* @return \Kerox\Messenger\Event\MessageEchoEvent |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public static function createMessageEchoEvent(array $payload): MessageEchoEvent |
|
75
|
|
|
{ |
|
76
|
1 |
|
$senderId = $payload['sender']['id']; |
|
77
|
1 |
|
$recipientId = $payload['recipient']['id']; |
|
78
|
1 |
|
$timestamp = $payload['timestamp']; |
|
79
|
1 |
|
$messageEcho = MessageEcho::create($payload['message']); |
|
80
|
|
|
|
|
81
|
1 |
|
return new MessageEchoEvent($senderId, $recipientId, $timestamp, $messageEcho); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $payload |
|
86
|
|
|
* @return \Kerox\Messenger\Event\MessageEvent |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public static function createMessageEvent(array $payload): MessageEvent |
|
89
|
|
|
{ |
|
90
|
4 |
|
$senderId = $payload['sender']['id']; |
|
91
|
4 |
|
$recipientId = $payload['recipient']['id']; |
|
92
|
4 |
|
$timestamp = $payload['timestamp']; |
|
93
|
4 |
|
$message = Message::create($payload['message']); |
|
94
|
|
|
|
|
95
|
4 |
|
return new MessageEvent($senderId, $recipientId, $timestamp, $message); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array $payload |
|
100
|
|
|
* @return \Kerox\Messenger\Event\PostbackEvent |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public static function createPostbackEvent(array $payload): PostbackEvent |
|
103
|
|
|
{ |
|
104
|
1 |
|
$senderId = $payload['sender']['id']; |
|
105
|
1 |
|
$recipientId = $payload['recipient']['id']; |
|
106
|
1 |
|
$timestamp = $payload['timestamp']; |
|
107
|
1 |
|
$postback = Postback::create($payload['postback']); |
|
108
|
|
|
|
|
109
|
1 |
|
return new PostbackEvent($senderId, $recipientId, $timestamp, $postback); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $payload |
|
114
|
|
|
* @return \Kerox\Messenger\Event\OptinEvent |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public static function createOptinEvent(array $payload): OptinEvent |
|
117
|
|
|
{ |
|
118
|
1 |
|
$senderId = $payload['sender']['id']; |
|
119
|
1 |
|
$recipientId = $payload['recipient']['id']; |
|
120
|
1 |
|
$timestamp = $payload['timestamp']; |
|
121
|
1 |
|
$optin = Optin::create($payload['optin']); |
|
122
|
|
|
|
|
123
|
1 |
|
return new OptinEvent($senderId, $recipientId, $timestamp, $optin); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $payload |
|
128
|
|
|
* @return \Kerox\Messenger\Event\AccountLinkingEvent |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public static function createAcountLinkingEvent(array $payload): AccountLinkingEvent |
|
131
|
|
|
{ |
|
132
|
1 |
|
$senderId = $payload['sender']['id']; |
|
133
|
1 |
|
$recipientId = $payload['recipient']['id']; |
|
134
|
1 |
|
$timestamp = $payload['timestamp']; |
|
135
|
1 |
|
$accountLinking = AccountLinking::create($payload['account_linking']); |
|
136
|
|
|
|
|
137
|
1 |
|
return new AccountLinkingEvent($senderId, $recipientId, $timestamp, $accountLinking); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param $payload |
|
142
|
|
|
* @return \Kerox\Messenger\Event\DeliveryEvent |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public static function createDeliveryEvent(array $payload): DeliveryEvent |
|
145
|
|
|
{ |
|
146
|
1 |
|
$senderId = $payload['sender']['id']; |
|
147
|
1 |
|
$recipientId = $payload['recipient']['id']; |
|
148
|
1 |
|
$delivery = Delivery::create($payload['delivery']); |
|
149
|
|
|
|
|
150
|
1 |
|
return new DeliveryEvent($senderId, $recipientId, $delivery); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param $payload |
|
155
|
|
|
* @return \Kerox\Messenger\Event\ReadEvent |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public static function createReadEvent($payload): ReadEvent |
|
158
|
|
|
{ |
|
159
|
1 |
|
$senderId = $payload['sender']['id']; |
|
160
|
1 |
|
$recipientId = $payload['recipient']['id']; |
|
161
|
1 |
|
$timestamp = $payload['timestamp']; |
|
162
|
1 |
|
$read = Read::create($payload['read']); |
|
163
|
|
|
|
|
164
|
1 |
|
return new ReadEvent($senderId, $recipientId, $timestamp, $read); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
1 |
|
public static function createPaymentEvent(array $payload) |
|
168
|
|
|
{ |
|
169
|
1 |
|
$senderId = $payload['sender']['id']; |
|
170
|
1 |
|
$recipientId = $payload['recipient']['id']; |
|
171
|
1 |
|
$timestamp = $payload['timestamp']; |
|
172
|
1 |
|
$payment = Payment::create($payload['payment']); |
|
173
|
|
|
|
|
174
|
1 |
|
return new PaymentEvent($senderId, $recipientId, $timestamp, $payment); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|