WhatsAppChannel   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 47
ccs 19
cts 23
cp 0.8261
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A send() 0 32 5
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