Test Failed
Push — master ( 96e40b...4a9569 )
by
unknown
17:48 queued 07:57
created

AfricasTalkingChannel::__construct()   A

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\AfricasTalking;
4
5
use Exception;
6
use Illuminate\Notifications\Notification;
7
use AfricasTalking\SDK\AfricasTalking as AfricasTalkingSDK;
8
use NotificationChannels\AfricasTalking\Exceptions\InvalidPhonenumber;
9
use NotificationChannels\AfricasTalking\Exceptions\CouldNotSendNotification;
10
11
class AfricasTalkingChannel
12
{
13
    /** @var AfricasTalkingSDK */
14
    protected $africasTalking;
15
16 2
    /** @param AfricasTalkingSDK $africasTalking */
17
    public function __construct(AfricasTalkingSDK $africasTalking)
18 2
    {
19 2
        $this->africasTalking = $africasTalking;
20
    }
21
22
    /**
23
     * Send the given notification.
24
     *
25
     * @param mixed $notifiable
26
     * @param \Illuminate\Notifications\Notification $notification
27
     * @throws CouldNotSendNotification
28 1
     */
29
    public function send($notifiable, Notification $notification)
30 1
    {
31
        $message = $notification->toAfricasTalking($notifiable);
0 ignored issues
show
Bug introduced by
The method toAfricasTalking() does not exist on Illuminate\Notifications\Notification. It seems like you code against a sub-type of Illuminate\Notifications\Notification such as NotificationChannels\Afr...g\Test\TestNotification. ( Ignorable by Annotation )

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

31
        /** @scrutinizer ignore-call */ 
32
        $message = $notification->toAfricasTalking($notifiable);
Loading history...
32 1
33
        if (!$phoneNumber = $notifiable->routeNotificationFor('africasTalking')) {
34
            $phoneNumber = $notifiable->phone_number;
35
        }
36
37 1
        if(!empty($message->getTo())) {
38 1
            $phoneNumber = $message->getTo();
39 1
        }
40 1
41
        if(empty($phoneNumber)) {
42
            throw InvalidPhonenumber::configurationNotSet();
43
        }
44
45 1
        if (empty(($message->getSender())) || is_null($message->getSender())) {
46
            $params = [
47
                'to'        => $phoneNumber,
48
                'message'   => $message->getContent(),
49
            ];
50
        } else {
51
            $params = [
52
                    'to'        => $phoneNumber,
53
                    'message'   => $message->getContent(),
54
                    'from'      => $message->getSender(),
55
                ];
56
        }
57
58
        try {
59
            return $this->africasTalking->sms()->send($params);
60
        } catch (Exception $e) {
61
            throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
62
        }
63
    }
64
}
65