SmsChannel::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Tzsk\Sms\Channels;
4
5
use Exception;
6
use Tzsk\Sms\SmsBuilder;
7
use Illuminate\Notifications\Notification;
8
9
class SmsChannel
10
{
11
    /**
12
     * Send the given notification.
13
     *
14
     * @param $notifiable
15
     * @param Notification $notification
16
     * @return mixed
17
     * @throws \Exception
18
     */
19
    public function send($notifiable, Notification $notification)
20
    {
21
        $message = $notification->toSms($notifiable);
0 ignored issues
show
Bug introduced by
The method toSms() 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...
22
23
        $this->validate($message);
24
        $manager = app()->make('tzsk-sms');
25
26
        if (! empty($message->getDriver())) {
27
            $manager->via($message->getDriver());
28
        }
29
30
        return $manager->send($message->getBody(), function ($sms) use ($message) {
31
            $sms->to($message->getRecipients());
32
        });
33
    }
34
35
    /**
36
     * Validate message.
37
     *
38
     * @param $message
39
     * @throws \Exception
40
     */
41
    private function validate($message)
42
    {
43
        $conditions = [
44
            'Invalid data for sms notification.' => ! is_a($message, SmsBuilder::class),
45
            'Message body could not be empty.' => empty($message->getBody()),
46
            'Message recipient could not be empty.' => empty($message->getRecipients()),
47
        ];
48
49
        foreach ($conditions as $ex => $condition) {
50
            throw_if($condition, new Exception($ex));
51
        }
52
    }
53
}
54