|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\MobilyWs; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
7
|
|
|
use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification; |
|
8
|
|
|
|
|
9
|
|
|
class MobilyWsApi |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var MobilyWsConfig */ |
|
12
|
|
|
private $config; |
|
13
|
|
|
|
|
14
|
|
|
/** @var HttpClient */ |
|
15
|
|
|
private $http; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Create a new MobilyWs channel instance. |
|
19
|
|
|
* |
|
20
|
|
|
* @param MobilyWsConfig $config |
|
21
|
|
|
* @param HttpClient $http |
|
22
|
|
|
*/ |
|
23
|
6 |
|
public function __construct(MobilyWsConfig $config, HttpClient $http) |
|
24
|
|
|
{ |
|
25
|
6 |
|
$this->http = $http; |
|
26
|
6 |
|
$this->config = $config; |
|
27
|
6 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Send request with string message |
|
31
|
|
|
* |
|
32
|
|
|
* @param $params |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function sendString($params) |
|
37
|
|
|
{ |
|
38
|
1 |
|
$payload = $this->preparePayload($params); |
|
39
|
1 |
|
return $this->send($payload); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Send request with MobilyWsMessage instance |
|
44
|
|
|
* |
|
45
|
|
|
* @param MobilyWsMessage $message |
|
46
|
|
|
* |
|
47
|
|
|
* @param $number |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
* @internal param $params |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function sendMessage(MobilyWsMessage $message, $number) |
|
53
|
|
|
{ |
|
54
|
|
|
$params = [ |
|
55
|
2 |
|
'msg' => $message->text, |
|
56
|
2 |
|
'numbers' => $number, |
|
57
|
2 |
|
'dateSend' => $message->dateSend(), |
|
58
|
2 |
|
'timeSend' => $message->timeSend(), |
|
59
|
2 |
|
]; |
|
60
|
|
|
|
|
61
|
2 |
|
$payload = $this->preparePayload($params); |
|
62
|
2 |
|
return $this->send($payload); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Send request to mobily.ws |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $payload |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
* @throws \NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification |
|
72
|
|
|
* @internal param array $params |
|
73
|
|
|
* |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function send(array $payload) |
|
76
|
|
|
{ |
|
77
|
5 |
|
$endpoint = 'msgSend.php'; |
|
78
|
|
|
try { |
|
79
|
5 |
|
$response = $this->http->post($endpoint, $payload); |
|
80
|
|
|
|
|
81
|
4 |
|
if ($response->getStatusCode() == 200) { |
|
82
|
|
|
return [ |
|
83
|
4 |
|
'code' => $code = (string) $response->getBody(), |
|
84
|
4 |
|
'message' => $this->msgSendResponse($code), |
|
85
|
4 |
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
throw CouldNotSendNotification::someErrorWhenSendingSms($response); |
|
|
|
|
|
|
88
|
1 |
|
} catch (RequestException $exception) { |
|
89
|
1 |
|
throw CouldNotSendNotification::couldNotSendRequestToMobilyWs($exception); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Prepare payload for http request. |
|
95
|
|
|
* |
|
96
|
|
|
* @param $params |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
3 |
|
protected function preparePayload($params) |
|
101
|
|
|
{ |
|
102
|
3 |
|
$form = array_merge([ |
|
103
|
3 |
|
'mobile' => $this->config->mobile, |
|
|
|
|
|
|
104
|
3 |
|
'password' => $this->config->password, |
|
|
|
|
|
|
105
|
3 |
|
'applicationType' => $this->config->applicationType, |
|
|
|
|
|
|
106
|
3 |
|
'lang' => $this->config->lang, |
|
|
|
|
|
|
107
|
3 |
|
'sender' => $this->config->sender, |
|
|
|
|
|
|
108
|
3 |
|
], $params); |
|
109
|
|
|
|
|
110
|
3 |
|
return array_merge( |
|
111
|
3 |
|
['form_params' => $form], |
|
112
|
3 |
|
$this->config->request |
|
|
|
|
|
|
113
|
3 |
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Parse the response body from mobily.ws. |
|
118
|
|
|
* |
|
119
|
|
|
* @param $code |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
4 |
|
protected function msgSendResponse($code) |
|
124
|
|
|
{ |
|
125
|
4 |
|
$arraySendMsg = []; |
|
126
|
4 |
|
$arraySendMsg[0] = 'لم يتم الاتصال بالخادم'; |
|
127
|
4 |
|
$arraySendMsg[1] = 'تمت عملية الإرسال بنجاح'; |
|
128
|
4 |
|
$arraySendMsg[2] = 'رصيدك 0 , الرجاء إعادة التعبئة حتى تتمكن من إرسال الرسائل'; |
|
129
|
4 |
|
$arraySendMsg[3] = 'رصيدك غير كافي لإتمام عملية الإرسال'; |
|
130
|
4 |
|
$arraySendMsg[4] = 'رقم الجوال (إسم المستخدم) غير صحيح'; |
|
131
|
4 |
|
$arraySendMsg[5] = 'كلمة المرور الخاصة بالحساب غير صحيحة'; |
|
132
|
4 |
|
$arraySendMsg[6] = 'صفحة الانترنت غير فعالة , حاول الارسال من جديد'; |
|
133
|
4 |
|
$arraySendMsg[7] = 'نظام المدارس غير فعال'; |
|
134
|
4 |
|
$arraySendMsg[8] = 'تكرار رمز المدرسة لنفس المستخدم'; |
|
135
|
4 |
|
$arraySendMsg[9] = 'انتهاء الفترة التجريبية'; |
|
136
|
4 |
|
$arraySendMsg[10] = 'عدد الارقام لا يساوي عدد الرسائل'; |
|
137
|
4 |
|
$arraySendMsg[11] = 'اشتراكك لا يتيح لك ارسال رسائل لهذه المدرسة. يجب عليك تفعيل الاشتراك لهذه المدرسة'; |
|
138
|
4 |
|
$arraySendMsg[12] = 'إصدار البوابة غير صحيح'; |
|
139
|
4 |
|
$arraySendMsg[13] = 'الرقم المرسل به غير مفعل أو لا يوجد الرمز BS في نهاية الرسالة'; |
|
140
|
4 |
|
$arraySendMsg[14] = 'غير مصرح لك بالإرسال بإستخدام هذا المرسل'; |
|
141
|
4 |
|
$arraySendMsg[15] = 'الأرقام المرسل لها غير موجوده أو غير صحيحه'; |
|
142
|
4 |
|
$arraySendMsg[16] = 'إسم المرسل فارغ، أو غير صحيح'; |
|
143
|
4 |
|
$arraySendMsg[17] = 'نص الرسالة غير متوفر أو غير مشفر بشكل صحيح'; |
|
144
|
4 |
|
$arraySendMsg[18] = 'تم ايقاف الارسال من المزود'; |
|
145
|
4 |
|
$arraySendMsg[19] = 'لم يتم العثور على مفتاح نوع التطبيق'; |
|
146
|
|
|
|
|
147
|
4 |
|
if (array_key_exists($code, $arraySendMsg)) { |
|
148
|
4 |
|
return $arraySendMsg[$code]; |
|
149
|
|
|
} |
|
150
|
|
|
$message = "نتيجة العملية غير معرفه، الرجاء المحاول مجددا\n"; |
|
151
|
|
|
$message .= 'الكود المرسل من الموقع: '; |
|
152
|
|
|
$message .= "{$code}"; |
|
153
|
|
|
|
|
154
|
|
|
return $message; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.