1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ninjacto\OrtcPhp\Models\Requests; |
4
|
|
|
|
5
|
|
|
use ninjacto\OrtcPhp\Handlers\OrtcResponseHandler; |
6
|
|
|
use ninjacto\OrtcPhp\Handlers\SendMessageResponseHandler; |
7
|
|
|
|
8
|
|
|
class SendMessageRequest extends OrtcRequest |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* authentication token. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $authToken; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* channel name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $channelName; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* message. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $message; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* get url path (not base url). |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
1 |
|
public function getUrlPath() |
37
|
|
|
{ |
38
|
1 |
|
return $this->getOrtcConfig()->getSendPath(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* is post request or get request? |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
1 |
|
public function isPost() |
47
|
|
|
{ |
48
|
1 |
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* get post body. |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
1 |
|
public function getPostData() |
57
|
|
|
{ |
58
|
1 |
|
$chunks = $this->makeChunks(); |
59
|
|
|
|
60
|
1 |
|
$postData = []; |
61
|
|
|
|
62
|
1 |
|
foreach ($chunks as $chunk) { |
63
|
1 |
|
$postData[] = [ |
64
|
1 |
|
'AK' => $this->getOrtcConfig()->getApplicationKey(), |
65
|
1 |
|
'PK' => $this->getOrtcConfig()->getPrivateKey(), |
66
|
1 |
|
'AT' => $this->getAuthToken(), |
67
|
1 |
|
'C' => $this->getChannelName(), |
68
|
1 |
|
'M' => $chunk, |
69
|
|
|
]; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
return $postData; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* split the message into chunks. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
1 |
|
protected function makeChunks() |
81
|
|
|
{ |
82
|
1 |
|
$maxSize = $this->getOrtcConfig()->getMaxChunkSize(); |
83
|
|
|
|
84
|
1 |
|
$chunks = str_split($this->getMessage(), $maxSize); |
85
|
1 |
|
$numberOfParts = count($chunks); |
86
|
|
|
|
87
|
1 |
|
$randomString = substr(sha1(rand(1, 10)), -8); |
88
|
|
|
|
89
|
1 |
|
for ($i = 0; $i < count($chunks); $i++) { |
|
|
|
|
90
|
1 |
|
$preString = strtr( |
91
|
1 |
|
$this->getOrtcConfig()->getPreMessageString(), |
92
|
|
|
[ |
93
|
1 |
|
'{RANDOM}' => $randomString, |
94
|
1 |
|
'{PART}' => $i + 1, |
95
|
1 |
|
'{TOTAL_PARTS}' => $numberOfParts, |
96
|
|
|
] |
97
|
1 |
|
); |
98
|
|
|
|
99
|
1 |
|
$chunks[$i] = $preString . $chunks[$i]; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
return $chunks; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
2 |
|
public function getMessage() |
109
|
|
|
{ |
110
|
2 |
|
return $this->message; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $message |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
2 |
|
public function setMessage($message) |
119
|
|
|
{ |
120
|
2 |
|
$this->message = $message; |
121
|
|
|
|
122
|
2 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
2 |
|
public function getAuthToken() |
129
|
|
|
{ |
130
|
2 |
|
return $this->authToken; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $authToken |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
2 |
|
public function setAuthToken($authToken) |
139
|
|
|
{ |
140
|
2 |
|
$this->authToken = $authToken; |
141
|
|
|
|
142
|
2 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
2 |
|
public function getChannelName() |
149
|
|
|
{ |
150
|
2 |
|
return $this->channelName; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $channelName |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
2 |
|
public function setChannelName($channelName) |
159
|
|
|
{ |
160
|
2 |
|
$this->channelName = $channelName; |
161
|
|
|
|
162
|
2 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* get response handler. |
167
|
|
|
* |
168
|
|
|
* @return OrtcResponseHandler |
169
|
|
|
*/ |
170
|
1 |
|
public function getResponseHandler() |
171
|
|
|
{ |
172
|
1 |
|
return new SendMessageResponseHandler(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: