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\RawEvent; |
14
|
|
|
use Tgallice\FBMessenger\Model\Callback\AccountLinking; |
15
|
|
|
use Tgallice\FBMessenger\Model\Callback\Delivery; |
16
|
|
|
use Tgallice\FBMessenger\Model\Callback\Message; |
17
|
|
|
use Tgallice\FBMessenger\Model\Callback\MessageEcho; |
18
|
|
|
use Tgallice\FBMessenger\Model\Callback\Optin; |
19
|
|
|
use Tgallice\FBMessenger\Model\Callback\Postback; |
20
|
|
|
use Tgallice\FBMessenger\Model\Callback\Read; |
21
|
|
|
|
22
|
|
|
class CallbackEventFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param array $payload |
26
|
|
|
* |
27
|
|
|
* @return CallbackEvent |
28
|
|
|
*/ |
29
|
10 |
|
public static function create(array $payload) |
30
|
|
|
{ |
31
|
|
|
// MessageEvent & MessageEchoEvent |
32
|
10 |
|
if (isset($payload['message'])) { |
33
|
4 |
|
if (isset($payload['message']['is_echo'])) { |
34
|
1 |
|
return self::createMessageEchoEvent($payload); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
return self::createMessageEvent($payload); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// PostbackEvent |
41
|
6 |
|
if (isset($payload['postback'])) { |
42
|
1 |
|
return self::createPostbackEvent($payload); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// AuthenticationEvent |
46
|
5 |
|
if (isset($payload['optin'])) { |
47
|
1 |
|
return self::createAuthenticationEvent($payload); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// AccountLinkingEvent |
51
|
4 |
|
if (isset($payload['account_linking'])) { |
52
|
1 |
|
return self::createAccountLinkingEvent($payload); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// MessageDeliveryEvent |
56
|
3 |
|
if (isset($payload['delivery'])) { |
57
|
1 |
|
return self::createMessageDeliveryEvent($payload); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// MessageReadEvent |
61
|
2 |
|
if (isset($payload['read'])) { |
62
|
1 |
|
return self::createMessageReadEvent($payload); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return new RawEvent($payload['sender']['id'], $payload['recipient']['id'], $payload); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $payload |
70
|
|
|
* |
71
|
|
|
* @return MessageEvent |
72
|
|
|
*/ |
73
|
3 |
View Code Duplication |
public static function createMessageEvent(array $payload) |
|
|
|
|
74
|
|
|
{ |
75
|
3 |
|
$message = Message::create($payload['message']); |
76
|
3 |
|
$senderId = $payload['sender']['id']; |
77
|
3 |
|
$recipientId = $payload['recipient']['id']; |
78
|
3 |
|
$timestamp = $payload['timestamp']; |
79
|
|
|
|
80
|
3 |
|
return new MessageEvent($senderId, $recipientId, $timestamp, $message); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $payload |
85
|
|
|
* |
86
|
|
|
* @return MessageEchoEvent |
87
|
|
|
*/ |
88
|
1 |
View Code Duplication |
public static function createMessageEchoEvent(array $payload) |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
$message = MessageEcho::create($payload['message']); |
91
|
1 |
|
$senderId = $payload['sender']['id']; |
92
|
1 |
|
$recipientId = $payload['recipient']['id']; |
93
|
1 |
|
$timestamp = $payload['timestamp']; |
94
|
|
|
|
95
|
1 |
|
return new MessageEchoEvent($senderId, $recipientId, $timestamp, $message); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $payload |
100
|
|
|
* |
101
|
|
|
* @return PostbackEvent |
102
|
|
|
*/ |
103
|
1 |
View Code Duplication |
public static function createPostbackEvent(array $payload) |
|
|
|
|
104
|
|
|
{ |
105
|
1 |
|
$postback = Postback::create($payload['postback']); |
106
|
1 |
|
$senderId = $payload['sender']['id']; |
107
|
1 |
|
$recipientId = $payload['recipient']['id']; |
108
|
1 |
|
$timestamp = $payload['timestamp']; |
109
|
|
|
|
110
|
1 |
|
return new PostbackEvent($senderId, $recipientId, $timestamp, $postback); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $payload |
115
|
|
|
* |
116
|
|
|
* @return AuthenticationEvent |
117
|
|
|
*/ |
118
|
1 |
View Code Duplication |
public static function createAuthenticationEvent(array $payload) |
|
|
|
|
119
|
|
|
{ |
120
|
1 |
|
$optin = Optin::create($payload['optin']); |
121
|
1 |
|
$senderId = $payload['sender']['id']; |
122
|
1 |
|
$recipientId = $payload['recipient']['id']; |
123
|
1 |
|
$timestamp = $payload['timestamp']; |
124
|
|
|
|
125
|
1 |
|
return new AuthenticationEvent($senderId, $recipientId, $timestamp, $optin); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $payload |
130
|
|
|
* |
131
|
|
|
* @return AccountLinkingEvent |
132
|
|
|
*/ |
133
|
1 |
View Code Duplication |
public static function createAccountLinkingEvent(array $payload) |
|
|
|
|
134
|
|
|
{ |
135
|
1 |
|
$accountLinking = AccountLinking::create($payload['account_linking']); |
136
|
1 |
|
$senderId = $payload['sender']['id']; |
137
|
1 |
|
$recipientId = $payload['recipient']['id']; |
138
|
1 |
|
$timestamp = $payload['timestamp']; |
139
|
|
|
|
140
|
1 |
|
return new AccountLinkingEvent($senderId, $recipientId, $timestamp, $accountLinking); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $payload |
145
|
|
|
* |
146
|
|
|
* @return MessageDeliveryEvent |
147
|
|
|
*/ |
148
|
1 |
|
public static function createMessageDeliveryEvent(array $payload) |
149
|
|
|
{ |
150
|
1 |
|
$delivery = Delivery::create($payload['delivery']); |
151
|
1 |
|
$senderId = $payload['sender']['id']; |
152
|
1 |
|
$recipientId = $payload['recipient']['id']; |
153
|
|
|
|
154
|
1 |
|
return new MessageDeliveryEvent($senderId, $recipientId, $delivery); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $payload |
159
|
|
|
* |
160
|
|
|
* @return MessageReadEvent |
161
|
|
|
*/ |
162
|
1 |
View Code Duplication |
public static function createMessageReadEvent(array $payload) |
|
|
|
|
163
|
|
|
{ |
164
|
1 |
|
$read = Read::create($payload['read']); |
165
|
1 |
|
$senderId = $payload['sender']['id']; |
166
|
1 |
|
$recipientId = $payload['recipient']['id']; |
167
|
1 |
|
$timestamp = $payload['timestamp']; |
168
|
|
|
|
169
|
1 |
|
return new MessageReadEvent($senderId, $recipientId, $timestamp, $read); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.