1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asad\ZohoCliq; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as Guzzle; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
class ZohoCliq |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Zoho Cliq Auth Token |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $authtoken; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default send to |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $send_to; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Default Channel to send message |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $channel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Zoho Cliq message endpoint |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $endpoint; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Zoho Cliq message |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $message; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Zoho Cliq card |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $card; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The Guzzle HTTP client instance. |
48
|
|
|
* |
49
|
|
|
* @var \GuzzleHttp\Client |
50
|
|
|
*/ |
51
|
|
|
protected $guzzle; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Instantiate a new Client. |
55
|
|
|
* |
56
|
|
|
* @param string $endpoint |
|
|
|
|
57
|
|
|
* @param array $attributes |
58
|
|
|
* @return void |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
public function __construct($authtoken, array $attributes = [], Guzzle $guzzle = null) |
61
|
|
|
{ |
62
|
|
|
$this->authtoken = $authtoken; |
63
|
|
|
|
64
|
|
|
if (isset($attributes['channel'])) { |
65
|
|
|
$this->setDefaultChannel($attributes['channel']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (isset($attributes['send_to'])) { |
69
|
|
|
$this->setDefaultSendTo($attributes['send_to']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->guzzle = $guzzle ?: new Guzzle; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set Default AuthToken |
77
|
|
|
* @param string $authtoken |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function setAuthToken($authtoken) |
81
|
|
|
{ |
82
|
|
|
$this->authtoken = $authtoken; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get Default Authtoken |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getAuthToken(): string |
90
|
|
|
{ |
91
|
|
|
return $this->authtoken; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set Default SendTo |
96
|
|
|
* @param string $send_to |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function setDefaultSendTo($send_to) |
100
|
|
|
{ |
101
|
|
|
$this->send_to = $send_to; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get Default Send To |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getDefaultSendTo(): string |
109
|
|
|
{ |
110
|
|
|
return $this->send_to; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set Default Channel |
115
|
|
|
* @param string $channel |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function setDefaultChannel($channel) |
119
|
|
|
{ |
120
|
|
|
$this->channel = $channel; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get Default Channel |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getDefaultChannel(): string |
128
|
|
|
{ |
129
|
|
|
return $this->channel; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Generate Cliq Endpoint from send_to and channel name |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
|
137
|
|
|
public function generateCliqEndpoint() |
138
|
|
|
{ |
139
|
|
|
$this->endpoint = "https://cliq.zoho.com/api/v2/" . $this->getDefaultSendTo() . "/" . $this->getDefaultChannel() . "/message"; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get Default Endpoint |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getEndpoint(): string |
147
|
|
|
{ |
148
|
|
|
return $this->endpoint; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get Message |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getMessage(): string |
156
|
|
|
{ |
157
|
|
|
return $this->message; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set Send To onfly |
162
|
|
|
* @return self |
163
|
|
|
*/ |
164
|
|
|
|
165
|
|
|
public function toChannel(): self |
166
|
|
|
{ |
167
|
|
|
$this->send_to = 'channelsbyname'; |
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set Send To onfly |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
|
|
|
176
|
|
|
public function toBot(): self |
177
|
|
|
{ |
178
|
|
|
$this->send_to = 'bots'; |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set Send To onfly |
184
|
|
|
* @return self |
185
|
|
|
*/ |
186
|
|
|
|
187
|
|
|
public function toChat(): self |
188
|
|
|
{ |
189
|
|
|
$this->send_to = 'chats'; |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Set Send To onfly |
195
|
|
|
* @return self |
196
|
|
|
*/ |
197
|
|
|
|
198
|
|
|
public function toBuddy(): self |
199
|
|
|
{ |
200
|
|
|
$this->send_to = 'buddies'; |
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Change Channel on fly |
206
|
|
|
*/ |
207
|
|
|
public function to($channel) |
208
|
|
|
{ |
209
|
|
|
$this->channel = $channel; |
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Create Message Card |
215
|
|
|
* @return self |
216
|
|
|
*/ |
217
|
|
|
public function card(array $attributes): self |
218
|
|
|
{ |
219
|
|
|
$title = isset($attributes['title']) ? $attributes['title'] : "BUG"; |
220
|
|
|
$theme = isset($attributes['theme']) ? $attributes['theme'] : "modern-inline"; |
221
|
|
|
$thumbnail = isset($attributes['thumbnail']) ? $attributes['thumbnail'] : "https://en.gravatar.com/userimage/57826719/1bcf7f90b22897258d2b3e4e84875218.jpg"; |
222
|
|
|
$card = [ |
223
|
|
|
'title' => $title, |
224
|
|
|
'theme' => $theme, |
225
|
|
|
'thumbnail' => $thumbnail |
226
|
|
|
]; |
227
|
|
|
$this->card = $card; |
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Send message to selected destination |
233
|
|
|
* */ |
234
|
|
|
public function send($message) |
235
|
|
|
{ |
236
|
|
|
$this->message = $message; |
237
|
|
|
$this->generateCliqEndpoint(); |
238
|
|
|
$payload = $this->createBody($message); |
239
|
|
|
$encoded = json_encode($payload, JSON_UNESCAPED_UNICODE); |
240
|
|
|
|
241
|
|
|
if ($encoded === false) { |
242
|
|
|
throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg())); |
243
|
|
|
} |
244
|
|
|
$this->guzzle->post($this->endpoint, [ |
245
|
|
|
'query' => $this->prepareAuth(), |
246
|
|
|
'body' => $encoded |
247
|
|
|
]); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function createBody($message) |
251
|
|
|
{ |
252
|
|
|
if (gettype($this->card) == 'array') { |
253
|
|
|
$payload['card'] = $this->card; |
|
|
|
|
254
|
|
|
} |
255
|
|
|
$payload['text'] = $message; |
|
|
|
|
256
|
|
|
return $payload; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function prepareAuth() |
260
|
|
|
{ |
261
|
|
|
return [ |
262
|
|
|
'authtoken' => $this->getAuthToken() |
263
|
|
|
]; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.