-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Notifiable.php
63 lines (54 loc) · 1.83 KB
/
Notifiable.php
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
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Orchestra\Notifier;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Orchestra\Support\Facades\Notifier;
use Illuminate\Contracts\Support\Arrayable;
use Orchestra\Contracts\Notification\Recipient;
use Orchestra\Contracts\Notification\Message as MessageContract;
trait Notifiable
{
/**
* Send email notification to user.
*
* @param \Illuminate\Support\Collection $users
* @param \Orchestra\Contracts\Notification\Message|string $subject
* @param \Illuminate\Contracts\Mail\Mailable|string|null $view
* @param array $data
*
* @return bool
*/
protected function sendNotifications(Collection $users, $subject, $view = null, array $data = [])
{
foreach ($users->all() as $user) {
$this->sendNotification($user, $subject, $view, $data);
}
}
/**
* Send email notification to user.
*
* @param \Orchestra\Contracts\Notification\Recipient $user
* @param \Orchestra\Contracts\Notification\Message|string $subject
* @param \Illuminate\Contracts\Mail\Mailable|string|null $view
* @param array $data
*
* @return bool
*/
protected function sendNotification(Recipient $user, $subject, $view = null, array $data = [])
{
if ($subject instanceof MessageContract) {
if ($subject->mailable()) {
return Notifier::send($user, $subject);
}
$view = $subject->getView();
$data = $subject->getData();
$subject = $subject->getSubject();
}
$entity = $user;
if ($user instanceof Arrayable) {
$entity = $user->toArray();
}
$data = Arr::add($data, 'user', $entity);
return Notifier::send($user, Message::create($view, $data, $subject));
}
}