forked from protonemedia/eddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerHandler.php
88 lines (69 loc) · 1.88 KB
/
ServerHandler.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace App\Exceptions;
use App\Models\CouldNotConnectToServerException;
use App\Models\Server;
use App\Notifications\JobOnServerFailed;
use App\Notifications\ServerConnectionLost;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notification;
use Throwable;
class ServerHandler
{
private $notifiable;
private Throwable $exception;
private string $reference = '';
private array $exceptionMailMap = [
CouldNotConnectToServerException::class => ServerConnectionLost::class,
];
public function __construct(private Server $server)
{
}
/**
* Setter for the notifiable.
*
* @param mixed $notifiable
*/
public function notify($notifiable): self
{
if ($notifiable instanceof Model && ! $notifiable->exists) {
$notifiable = null;
}
$this->notifiable = $notifiable;
return $this;
}
/**
* Setter for the Throwable.
*/
public function about(Throwable $exception): self
{
$this->exception = $exception;
return $this;
}
/**
* Setter for the reference.
*/
public function withReference(string $reference): self
{
$this->reference = $reference;
return $this;
}
/**
* Creates a Notification instance based on the exception type.
*/
private function buildNotification(): Notification
{
$exceptionClass = get_class($this->exception);
$notificationClass = $this->exceptionMailMap[$exceptionClass] ?? JobOnServerFailed::class;
return new $notificationClass($this->server, $this->reference);
}
/**
* Sends the notification if the notifiable is set.
*/
public function send(): void
{
if (! $this->notifiable) {
return;
}
$this->notifiable->notify($this->buildNotification());
}
}