Passed
Push — master ( a3961f...62f88c )
by Marcel
21:12
created

PushoverNotificationHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 46
rs 10
c 1
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 2
A canHandle() 0 4 3
A getName() 0 2 1
A __construct() 0 1 1
A handle() 0 20 4
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());
0 ignored issues
show
Bug introduced by
It seems like $this->notificationSetti...->getPushoverApiToken() can also be of type null; however, parameter $token of Serhiy\Pushover\Application::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $this->application = new Application(/** @scrutinizer ignore-type */ $this->notificationSettings->getPushoverApiToken());
Loading history...
35
    }
36
37
    public function handle(Notification $notification): void {
38
        $this->initialize();;
39
40
        $recipient = new Recipient($notification->getRecipient()->getPushoverToken());
0 ignored issues
show
Bug introduced by
It seems like $notification->getRecipient()->getPushoverToken() can also be of type null; however, parameter $userKey of Serhiy\Pushover\Recipient::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $recipient = new Recipient(/** @scrutinizer ignore-type */ $notification->getRecipient()->getPushoverToken());
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $this->application can also be of type null; however, parameter $application of Serhiy\Pushover\Api\Mess...fication::__construct() does only seem to accept Serhiy\Pushover\Application, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $pushoverNotification = new PushoverNotification(/** @scrutinizer ignore-type */ $this->application, $recipient, $message);
Loading history...
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
}