1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coreproc\GlobeLabsSms; |
4
|
|
|
|
5
|
|
|
use Coreproc\GlobeLabsSms\Events\GlobeLabsSmsSent; |
6
|
|
|
use Coreproc\GlobeLabsSms\Exceptions\CouldNotSendNotification; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use GuzzleHttp\Exception\ConnectException; |
10
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
11
|
|
|
use GuzzleHttp\Exception\ServerException; |
12
|
|
|
use Illuminate\Notifications\Notification; |
13
|
|
|
use Lang; |
14
|
|
|
|
15
|
|
|
class GlobeLabsSmsChannel |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Client |
19
|
|
|
*/ |
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
public function __construct(Client $client) |
23
|
|
|
{ |
24
|
|
|
$this->client = $client; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Send the given notification. |
29
|
|
|
* |
30
|
|
|
* @param mixed $notifiable |
31
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
32
|
|
|
* |
33
|
|
|
* @throws CouldNotSendNotification |
34
|
|
|
*/ |
35
|
|
|
public function send($notifiable, Notification $notification) |
36
|
|
|
{ |
37
|
|
|
if (empty($notifiable->routeNotificationFor('globeLabsSms'))) { |
38
|
|
|
throw new CouldNotSendNotification('Missing method in your notifiable: routeNotificationForGlobeLabs().'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$contactInfo = $notifiable->routeNotificationFor('globeLabsSms'); |
42
|
|
|
|
43
|
|
|
// The contact info should include an address (mobile number). We are making access_token optional. The API |
44
|
|
|
// response will catch that anyway. |
45
|
|
|
if (empty($contactInfo['address'])) { |
46
|
|
|
throw new CouldNotSendNotification('Missing address variable from your routeNotificationForGlobeLabs().'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$message = $notification->toGlobeLabsSms($notifiable); |
|
|
|
|
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$response = $this->client->request('POST', $message->getApiSendUrl(), [ |
53
|
|
|
'body' => $message->toJson(), |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
event(new GlobeLabsSmsSent($response)); |
57
|
|
|
} catch (ConnectException $exception) { |
58
|
|
|
throw $this->generateExceptionMessage($exception, 'connect_exception'); |
59
|
|
|
} catch (ClientException $exception) { |
60
|
|
|
throw $this->generateExceptionMessage($exception, 'client_exception'); |
61
|
|
|
} catch (ServerException $exception) { |
62
|
|
|
throw $this->generateExceptionMessage($exception, 'server_exception'); |
63
|
|
|
} catch (GuzzleException $exception) { |
64
|
|
|
throw $this->generateExceptionMessage($exception, 'guzzle_exception'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function generateExceptionMessage($exception, $langString) |
69
|
|
|
{ |
70
|
|
|
$message = $exception->getMessage(); |
71
|
|
|
|
72
|
|
|
if (! empty($exception->getResponse())) { |
73
|
|
|
$response = json_decode($exception->getResponse()->getBody()->getContents()); |
74
|
|
|
$message = $response->error ?? $exception->getMessage(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (Lang::has('globe_labs_sms::errors.' . $langString)) { |
78
|
|
|
$message = __('globe_labs_sms::errors.' . $langString); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new CouldNotSendNotification($message); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.