1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Chabok; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use NotificationChannels\Chabok\Exceptions\CouldNotSendNotification; |
8
|
|
|
use Illuminate\Notifications\Notification; |
9
|
|
|
use NotificationChannels\Chabok\Exceptions\InvalidConfiguration; |
10
|
|
|
|
11
|
|
|
class ChabokChannel |
12
|
|
|
{ |
13
|
|
|
private static $API_ENDPOINT; |
14
|
|
|
|
15
|
|
|
/** @var Client */ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** @param Client $client */ |
19
|
3 |
|
public function __construct(Client $client) |
20
|
|
|
{ |
21
|
3 |
|
$this->client = $client; |
22
|
3 |
|
self::$API_ENDPOINT = 'https://' . config('services.chabok.app_id') . '.push.adpdigital.com/api/push/toUsers'; |
23
|
3 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Send the given notification. |
27
|
|
|
* |
28
|
|
|
* @param mixed $notifiable |
29
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
30
|
|
|
* |
31
|
|
|
* @throws \NotificationChannels\Chabok\Exceptions\InvalidConfiguration |
32
|
|
|
* @throws \NotificationChannels\Chabok\Exceptions\CouldNotSendNotification |
33
|
|
|
*/ |
34
|
3 |
|
public function send($notifiable, Notification $notification) |
35
|
|
|
{ |
36
|
3 |
|
$routing = collect($notifiable->routeNotificationFor('Chabok')); |
37
|
|
|
|
38
|
3 |
|
if ($routing->isEmpty()) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
$key = config('services.chabok.key'); |
43
|
|
|
$additionalConfiguration = config('services.chabok.additional') ?? []; |
44
|
3 |
|
|
45
|
1 |
|
if (is_null($key)) { |
46
|
|
|
throw InvalidConfiguration::configurationNotSet(); |
47
|
|
|
} |
48
|
2 |
|
|
49
|
|
|
$chabokParameters = $notification->toChabok($notifiable)->toArray(); |
50
|
2 |
|
$params = array_merge( |
51
|
2 |
|
['form_params' => Arr::set($chabokParameters, 'user', $routing->get('uuid'))], |
52
|
|
|
$additionalConfiguration, |
53
|
|
|
); |
|
|
|
|
54
|
2 |
|
$response = $this->client->post(self::$API_ENDPOINT.'?access_token='.$key, $params); |
55
|
1 |
|
|
56
|
|
|
if ($response->getStatusCode() !== 200) { |
57
|
1 |
|
throw CouldNotSendNotification::serviceRespondedWithAnError($response); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|