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