1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\NotifiableException; |
4
|
|
|
|
5
|
|
|
use Cerbero\NotifiableException\Notifications\ErrorOccurred; |
6
|
|
|
use Illuminate\Notifications\AnonymousNotifiable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait to let exceptions notify their errors. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
trait Notifies |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Define how the current exception should be reported by the exception handler |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
3 |
|
public function report() |
20
|
|
|
{ |
21
|
3 |
|
$this->notify(); |
22
|
3 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Notify the current exception. |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
24 |
|
public function notify() |
30
|
|
|
{ |
31
|
|
|
/** @var \Cerbero\NotifiableException\Notifiable $this */ |
32
|
24 |
|
$notification = new ErrorOccurred($this); |
33
|
24 |
|
$routes = $this->getRoutesToNotify(); |
|
|
|
|
34
|
|
|
|
35
|
24 |
|
foreach ($routes as $channel => $channelRoutes) { |
36
|
21 |
|
$channelRoutes = array_unique((array) $channelRoutes); |
37
|
|
|
|
38
|
21 |
|
foreach ($channelRoutes as $route) { |
39
|
21 |
|
(new AnonymousNotifiable)->route($channel, $route)->notify($notification); |
40
|
|
|
} |
41
|
|
|
} |
42
|
24 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieve all the routes to send notification to. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
24 |
|
protected function getRoutesToNotify(): array |
50
|
|
|
{ |
51
|
24 |
|
if ($this->overridesRoutes()) { |
52
|
3 |
|
return $this->getCustomRoutes(); |
53
|
|
|
} |
54
|
|
|
|
55
|
21 |
|
$defaultRoutes = config('notifiable_exception.default_routes'); |
56
|
|
|
|
57
|
21 |
|
return array_merge_recursive($defaultRoutes, $this->getCustomRoutes()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Determine whether the current exception routes should override the default ones. |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
21 |
|
protected function overridesRoutes(): bool |
66
|
|
|
{ |
67
|
21 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retrieve the custom notification routes keyed by the channel alias |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
3 |
|
protected function getCustomRoutes(): array |
76
|
|
|
{ |
77
|
3 |
|
return []; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Retrieve the message for each channel keyed by the channel alias |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
3 |
|
public function getMessages(): array |
86
|
|
|
{ |
87
|
3 |
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieve the custom channel class names keyed by the channel alias |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
3 |
|
public function getCustomChannels(): array |
96
|
|
|
{ |
97
|
3 |
|
return []; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|