|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Exponent; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
7
|
|
|
use Illuminate\Notifications\Notification; |
|
8
|
|
|
use NotificationChannels\Exponent\Exceptions\CouldNotSendNotification; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ExponentChannel. |
|
12
|
|
|
*/ |
|
13
|
|
|
class ExponentChannel |
|
14
|
|
|
{ |
|
15
|
|
|
const DEFAULT_API_URL = 'https://exp.host'; |
|
16
|
|
|
const MAX_TOKEN_LENGTH = 100; |
|
17
|
|
|
|
|
18
|
|
|
protected $client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ExponentChannel constructor. |
|
22
|
|
|
* @param Client $client |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Client $client) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->client = $client; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Send the given notification. |
|
31
|
|
|
* |
|
32
|
|
|
* @param mixed $notifiable |
|
33
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \NotificationChannels\Exponent\Exceptions\CouldNotSendNotification |
|
36
|
|
|
*/ |
|
37
|
|
|
public function send($notifiable, Notification $notification) |
|
38
|
|
|
{ |
|
39
|
|
|
if (! $notifiable->routeNotificationFor('exponent')) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
$tokens = (array) $notifiable->routeNotificationFor('exponent'); |
|
43
|
|
|
|
|
44
|
|
|
if (empty($tokens)) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @var ExponentMessage|null $exponentMessage */ |
|
49
|
|
|
$exponentMessage = $notification->toExponent($notifiable); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if (empty($exponentMessage)) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$partialTokens = array_chunk($tokens, self::MAX_TOKEN_LENGTH, false); |
|
56
|
|
|
foreach ($partialTokens as $partialToken) { |
|
57
|
|
|
$exponentMessage->setTokens($partialToken); |
|
58
|
|
|
$this->sendExponent($exponentMessage); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param ExponentMessage $exponentMessage |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function sendExponent(ExponentMessage $exponentMessage) |
|
66
|
|
|
{ |
|
67
|
|
|
$request = []; |
|
68
|
|
|
foreach ($exponentMessage->getTokens() as $token) { |
|
69
|
|
|
$request[] = $exponentMessage->toArray() + ['to' => $token]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$this->client->post('/--/api/v2/push/send', [ |
|
74
|
|
|
'body' => json_encode($request), |
|
75
|
|
|
'headers' => [ |
|
76
|
|
|
'Content-Type' => 'application/json', |
|
77
|
|
|
], |
|
78
|
|
|
]); |
|
79
|
|
|
} catch (RequestException $e) { |
|
80
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($e); |
|
81
|
|
|
} |
|
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.