|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BRKFun\NotificationOptions; |
|
4
|
|
|
|
|
5
|
|
|
use BRKFun\NotificationOptions\Exceptions\NotificationNotFoundException; |
|
6
|
|
|
use BRKFun\NotificationOptions\Models\NotificationOption; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
|
|
10
|
|
|
class NotificationOptions |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Model |
|
14
|
|
|
*/ |
|
15
|
|
|
private $model; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $name; |
|
21
|
|
|
|
|
22
|
|
|
public function setModel(Model $model) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->model = $model; |
|
25
|
|
|
return $this; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setName(string $notificationName) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->name = $notificationName; |
|
31
|
|
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param bool $value |
|
36
|
|
|
* @return Builder|Model|object|null |
|
37
|
|
|
* @throws NotificationNotFoundException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setNotification(bool $value) |
|
40
|
|
|
{ |
|
41
|
|
|
$notification = $this->notificationQuery(); |
|
42
|
|
|
if(!$notification){ |
|
43
|
|
|
throw new NotificationNotFoundException(); |
|
44
|
|
|
} |
|
45
|
|
|
$notification->value = $value; |
|
46
|
|
|
$notification->token = tokenSetter(); |
|
47
|
|
|
$notification->save(); |
|
48
|
|
|
return $notification; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function makeLink(Model $model, NotificationOption $notificationOption) |
|
52
|
|
|
{ |
|
53
|
|
|
return route('notification-options::unsubscribe', |
|
54
|
|
|
[ |
|
55
|
|
|
'email' => $model->email, |
|
56
|
|
|
'remember_token' => $notificationOption->token, |
|
57
|
|
|
'notification_name' => $notificationOption->key, |
|
58
|
|
|
] |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function notificationQuery() |
|
63
|
|
|
{ |
|
64
|
|
|
$notificationOption = NotificationOption::query() |
|
65
|
|
|
->where('key', $this->name) |
|
66
|
|
|
->where('notifiable_type', $this->model->getMorphClass()) |
|
67
|
|
|
->where('notifiable_id', $this->model->id) |
|
68
|
|
|
->first(); |
|
69
|
|
|
return $notificationOption; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|