Completed
Push — master ( c15062...be395d )
by Simon
03:01
created

SipgateChannel::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Simonkub\Laravel\Notifications\Sipgate;
4
5
use Illuminate\Notifications\Notification;
6
use Illuminate\Support\Facades\Config;
7
use Simonkub\Laravel\Notifications\Sipgate\Exceptions\CouldNotSendNotification;
8
9
class SipgateChannel
10
{
11
    /**
12
     * @var SipgateClient
13
     */
14
    protected $client;
15
16
    public function __construct(SipgateClient $client)
17
    {
18
        $this->client = $client;
19
    }
20
21
    /**
22
     * Send the given notification.
23
     *
24
     * @param  mixed  $notifiable
25
     * @param  Notification  $notification
26
     *
27
     * @throws CouldNotSendNotification
28
     */
29
    public function send($notifiable, Notification $notification)
30
    {
31
        /** @var SipgateMessage $message */
32
        $message = $notification->toSipgate($notifiable);
1 ignored issue
show
Bug introduced by
The method toSipgate() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
34
        $this->addRecipient($message, $notifiable);
35
36
        $this->addSmsId($message);
37
38
        $this->client->send($message);
39
    }
40
41
    /**
42
     * @param  SipgateMessage  $message
43
     * @param $notifiable
44
     * @throws CouldNotSendNotification
45
     */
46
    protected function addRecipient(SipgateMessage $message, $notifiable)
47
    {
48
        if ($message->getRecipient()) {
49
            return;
50
        }
51
52
        if ($recipient = $notifiable->routeNotificationFor('sipgate', $notifiable)) {
53
            $message->recipient($recipient);
54
55
            return;
56
        }
57
58
        throw CouldNotSendNotification::noRecipient();
59
    }
60
61
    /**
62
     * @param  SipgateMessage  $message
63
     * @throws CouldNotSendNotification
64
     */
65
    protected function addSmsId(SipgateMessage $message)
66
    {
67
        if ($message->getSmsId()) {
68
            return;
69
        }
70
71
        if ($smsId = Config::get('services.sipgate.smsId')) {
72
            $message->smsId($smsId);
73
74
            return;
75
        }
76
77
        throw CouldNotSendNotification::noSmsId();
78
    }
79
}
80