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