Completed
Pull Request — master (#13)
by
unknown
01:38
created

SmsChannel::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
namespace Tzsk\Sms\Channels;
3
4
use Illuminate\Notifications\Notification;
5
use Tzsk\Sms\SmsManager;
6
7
class SmsChannel
8
{
9
    /**
10
     * Send the given notification.
11
     *
12
     * @param $notifiable
13
     * @param Notification $notification
14
     * @return mixed
15
     * @throws \Exception
16
     */
17
    public function send($notifiable, Notification $notification)
18
    {
19
        $message = $notification->toSms($notifiable);
20
21
        // Validate the message.
22
        $this->validate($message);
23
24
        $manager = new SmsManager;
25
26
        // Use custom driver if exists.
27
        if (!empty($message['driver'])) {
28
            $manager->withDriver($message['driver']);
29
        }
30
31
        // Send notification.
32
        return $manager->send($message['body'], function ($sms) use ($message) {
33
            $sms->to($message['recipients']);
34
        });
35
    }
36
37
    /**
38
     * Validate message.
39
     *
40
     * @param $message
41
     * @throws \Exception
42
     */
43
    private function validate($message)
44
    {
45
        if (empty($message['body'])) {
46
            throw new \Exception("Message body could not be empty.");
47
        }
48
49
        if (empty($message['recipients'])) {
50
            throw new \Exception("Message recipient could not be empty.");
51
        }
52
    }
53
}
54