1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\MobilyWs; |
4
|
|
|
|
5
|
|
|
use Illuminate\Events\Dispatcher; |
6
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use NotificationChannels\MobilyWs\Exceptions\CouldNotSendMobilyWsNotification; |
9
|
|
|
|
10
|
|
|
class MobilyWsChannel |
11
|
|
|
{ |
12
|
|
|
/** @var MobilyWsApi */ |
13
|
|
|
private $api; |
14
|
|
|
|
15
|
|
|
/** @var Dispatcher */ |
16
|
|
|
private $events; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* MobilyWsChannel constructor. |
20
|
|
|
* |
21
|
|
|
* @param MobilyWsApi $mobilyWs |
22
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
23
|
|
|
*/ |
24
|
7 |
|
public function __construct(MobilyWsApi $mobilyWs, Dispatcher $events) |
25
|
|
|
{ |
26
|
7 |
|
$this->api = $mobilyWs; |
27
|
7 |
|
$this->events = $events; |
28
|
7 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Send the given notification. |
32
|
|
|
* |
33
|
|
|
* @param mixed $notifiable |
34
|
|
|
* @param Notification $notification |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
* |
38
|
|
|
* @throws \NotificationChannels\MobilyWs\Exceptions\CouldNotSendMobilyWsNotification |
39
|
|
|
*/ |
40
|
7 |
|
public function send($notifiable, Notification $notification) |
41
|
|
|
{ |
42
|
7 |
|
if (!method_exists($notification, 'toMobilyWs')) { |
43
|
1 |
|
throw CouldNotSendMobilyWsNotification::withErrorMessage('MobilyWs notifications must have toMobilyWs method'); |
44
|
|
|
} |
45
|
6 |
|
$message = $notification->toMobilyWs($notifiable, new MobilyWsMessage()); |
|
|
|
|
46
|
6 |
|
$number = $notifiable->routeNotificationFor('MobilyWs') ?: $notifiable->phone_number; |
47
|
|
|
// TODO Validate Number |
48
|
|
|
|
49
|
6 |
|
$response = $this->dispatchRequest($message, $number); |
50
|
|
|
|
51
|
5 |
|
if ($response['code'] == 1) { |
52
|
3 |
|
return $response['message']; |
53
|
|
|
} |
54
|
2 |
|
$this->events->fire( |
55
|
2 |
|
new NotificationFailed($notifiable, $notification, 'mobily-ws', $response) |
56
|
2 |
|
); |
57
|
|
|
|
58
|
2 |
|
throw CouldNotSendMobilyWsNotification::mobilyWsRespondedWithAnError($response['code'], $response['message']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $message |
63
|
|
|
* @param $number |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
* |
67
|
|
|
* @throws CouldNotSendMobilyWsNotification |
68
|
|
|
*/ |
69
|
6 |
|
private function dispatchRequest($message, $number) |
70
|
|
|
{ |
71
|
6 |
|
if (is_string($message)) { |
72
|
3 |
|
$response = $this->api->sendString([ |
73
|
3 |
|
'msg' => $message, |
74
|
3 |
|
'numbers' => $number, |
75
|
3 |
|
]); |
76
|
6 |
|
} elseif ($message instanceof MobilyWsMessage) { |
77
|
2 |
|
$response = $this->api->sendMessage($message, $number); |
78
|
2 |
|
} else { |
79
|
1 |
|
$errorMessage = sprintf('toMobilyWs must return a string or instance of %s. Instance of %s returned', |
80
|
1 |
|
MobilyWsMessage::class, |
81
|
1 |
|
gettype($message) |
82
|
1 |
|
); |
83
|
1 |
|
throw CouldNotSendMobilyWsNotification::withErrorMessage($errorMessage); |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
return $response; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.