WhatsAppChannel::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace NotificationChannels\WhatsApp;
4
5
use Illuminate\Notifications\Notification;
6
use Netflie\WhatsAppCloudApi\Response;
7
use Netflie\WhatsAppCloudApi\Response\ResponseException;
8
use Netflie\WhatsAppCloudApi\WhatsAppCloudApi;
9
use NotificationChannels\WhatsApp\Exceptions\CouldNotSendNotification;
10
11
class WhatsAppChannel
12
{
13
    /*
14
     * HTTP WhatsApp Cloud API wrapper
15
     */
16
    private WhatsAppCloudApi $whatsapp;
17
18 4
    public function __construct(WhatsAppCloudApi $whatsapp)
19
    {
20 4
        $this->whatsapp = $whatsapp;
21
    }
22
23
    /**
24
     * Send the given notification.
25
     */
26 4
    public function send($notifiable, Notification $notification): ?Response
27
    {
28
        // @phpstan-ignore-next-line
29 4
        $message = $notification->toWhatsApp($notifiable);
0 ignored issues
show
introduced by
The method toWhatsApp() does not exist on Illuminate\Notifications\Notification. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

29
        /** @scrutinizer ignore-call */ 
30
        $message = $notification->toWhatsApp($notifiable);
Loading history...
30
31 4
        if (! $message->hasRecipient()) {
32 2
            $to = $notifiable->routeNotificationFor('whatsapp', $notification)
33 2
                ?? $notifiable->routeNotificationFor(self::class, $notification);
34
35 2
            if (! $to) {
36 1
                return null;
37
            }
38
39 1
            $message->to($to);
40
        }
41
42
        try {
43 3
            if ($message->type() === 'text') {
44
                return $this->whatsapp->sendTextMessage(
45
                    $message->recipient(),
46
                    $message->getMessage()
47
                );
48
            }
49
50 3
            return $this->whatsapp->sendTemplate(
51 3
                $message->recipient(),
52 3
                $message->configuredName(),
53 3
                $message->configuredLanguage(),
54 3
                $message->components()
55 3
            );
56 1
        } catch (ResponseException $e) {
57 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($e->response()->body());
58
        }
59
    }
60
}
61