|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Notification\Pushover; |
|
4
|
|
|
|
|
5
|
|
|
use App\Notification\Notification; |
|
6
|
|
|
use App\Notification\NotificationHandlerInterface; |
|
7
|
|
|
use App\Settings\NotificationSettings; |
|
8
|
|
|
use App\Utils\ArrayUtils; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Serhiy\Pushover\Api\Message\Message; |
|
11
|
|
|
use Serhiy\Pushover\Api\Message\Notification as PushoverNotification; |
|
12
|
|
|
use Serhiy\Pushover\Api\Message\Priority; |
|
13
|
|
|
use Serhiy\Pushover\Api\Message\Sound; |
|
14
|
|
|
use Serhiy\Pushover\Application; |
|
15
|
|
|
use Serhiy\Pushover\Recipient; |
|
16
|
|
|
|
|
17
|
|
|
class PushoverNotificationHandler implements NotificationHandlerInterface { |
|
18
|
|
|
|
|
19
|
|
|
private ?Application $application = null; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(private readonly NotificationSettings $notificationSettings, private readonly LoggerInterface $logger) { } |
|
22
|
|
|
|
|
23
|
|
|
public function canHandle(Notification $notification): bool { |
|
24
|
|
|
return !empty($this->notificationSettings->getPushoverApiToken()) |
|
25
|
|
|
&& !empty($notification->getRecipient()->getPushoverToken()) |
|
26
|
|
|
&& ArrayUtils::inArray($notification->getRecipient()->getUserType(), $this->notificationSettings->getPushoverEnabledUserTypes()) !== false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function initialize(): void { |
|
30
|
|
|
if($this->application !== null) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->application = new Application($this->notificationSettings->getPushoverApiToken()); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function handle(Notification $notification): void { |
|
38
|
|
|
$this->initialize();; |
|
39
|
|
|
|
|
40
|
|
|
$recipient = new Recipient($notification->getRecipient()->getPushoverToken()); |
|
|
|
|
|
|
41
|
|
|
$message = new Message($notification->getContent(), $notification->getSubject()); |
|
42
|
|
|
if(!empty($notification->getLink()) && !empty($notification->getLinkText())) { |
|
43
|
|
|
$message->setUrl($notification->getLink()); |
|
44
|
|
|
$message->setUrlTitle($notification->getLinkText()); |
|
45
|
|
|
} |
|
46
|
|
|
$message->setPriority(new Priority(Priority::NORMAL)); |
|
47
|
|
|
|
|
48
|
|
|
$pushoverNotification = new PushoverNotification($this->application, $recipient, $message); |
|
|
|
|
|
|
49
|
|
|
$pushoverNotification->setSound(new Sound(Sound::PUSHOVER)); |
|
50
|
|
|
|
|
51
|
|
|
$response = $pushoverNotification->push(); |
|
52
|
|
|
|
|
53
|
|
|
if(!$response->isSuccessful()) { |
|
54
|
|
|
$this->logger->alert('Pushover-Benachrichtung wurde nicht erfolgreich abgeschickt.', [ |
|
55
|
|
|
'errors' => $response->getErrors(), |
|
56
|
|
|
'receipt' => $response->getReceipt() |
|
57
|
|
|
]); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getName(): string { |
|
62
|
|
|
return 'pushover'; |
|
63
|
|
|
} |
|
64
|
|
|
} |