-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.php
More file actions
50 lines (41 loc) · 1.44 KB
/
Channel.php
File metadata and controls
50 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace TransformStudios\Front\Notifications;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Statamic\Auth\User;
class Channel
{
/**
* @throws \Illuminate\Http\Client\RequestException
*/
public function send($ignore, BaseNotification $notification): bool
{
$data = $this->data($notification);
if ($conversationId = Cache::pull($key = $notification->key)) {
return $this->post('conversations', $conversationId, $data)->successful();
}
$response = $this->post('channels', config('front.notifications.channel'), $data);
Cache::forever($key, $this->getConversationId($response));
return $response->successful();
}
private function data(BaseNotification $notification): array
{
return [
'body' => $notification->renderedView,
'options' => ['archive' => false],
'subject' => $notification->subject,
'to' => $notification->users->map(fn (User $user) => $user->email())->all(),
];
}
private function post(string $segment, string $id, array $data): Response
{
return front()
->post("/$segment/$id/messages", $data)
->throw();
}
private function getConversationId(Response $response): string
{
return last(explode('/', Arr::get($response, '_links.related.conversation')));
}
}