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