Completed
Pull Request — master (#7)
by Hamoud
05:51
created

MobilyWsChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace NotificationChannels\MobilyWs;
4
5
use Illuminate\Events\Dispatcher;
6
use Illuminate\Notifications\Events\NotificationFailed;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification;
9
10
class MobilyWsChannel
11
{
12
    /** @var MobilyWsApi */
13
    private $api;
14
15
    /** @var Dispatcher */
16
    private $events;
17
18
    /**
19
     * MobilyWsChannel constructor.
20
     *
21
     * @param MobilyWsApi                   $mobilyWs
22
     * @param \Illuminate\Events\Dispatcher $events
23
     */
24
    public function __construct(MobilyWsApi $mobilyWs, Dispatcher $events)
25
    {
26
        $this->api = $mobilyWs;
27
        $this->events = $events;
28
    }
29
30
    /**
31
     * Send the given notification.
32
     *
33
     * @param mixed        $notifiable
34
     * @param Notification $notification
35
     *
36
     * @return string
37
     *
38
     * @throws \NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification
39
     */
40
    public function send($notifiable, Notification $notification)
41
    {
42
        if (!method_exists($notification, 'toMobilyWs')) {
43
            throw CouldNotSendNotification::withErrorMessage('MobilyWs notifications must have toMobilyWs method');
44
        }
45
        $message = $notification->toMobilyWs($notifiable, new MobilyWsMessage());
0 ignored issues
show
Bug introduced by
The method toMobilyWs() 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...
46
        $number = $notifiable->routeNotificationFor('MobilyWs') ?: $notifiable->phone_number;
47
        // TODO Validate Number
48
49
        $response = $this->dispatchRequest($message, $number);
50
51
        if ($response['code'] == 1) {
52
            return $response['message'];
53
        }
54
        $this->events->fire(
55
            new NotificationFailed($notifiable, $notification, 'mobily-ws', $response)
56
        );
57
58
        throw CouldNotSendNotification::mobilyWsRespondedWithAnError($response['code'], $response['message']);
59
    }
60
61
    /**
62
     * @param $message
63
     * @param $number
64
     *
65
     * @return array
66
     *
67
     * @throws CouldNotSendNotification
68
     */
69
    private function dispatchRequest($message, $number)
70
    {
71
        if (is_string($message)) {
72
            $response = $this->api->sendString([
73
                'msg' => $message,
74
                'numbers' => $number,
75
            ]);
76
        } elseif ($message instanceof MobilyWsMessage) {
77
            $response = $this->api->sendMessage($message, $number);
78
        } else {
79
            $errorMessage = sprintf('toMobilyWs must return a string or instance of %s. Instance of %s returned',
80
                MobilyWsMessage::class,
81
                gettype($message)
82
            );
83
            throw CouldNotSendNotification::withErrorMessage($errorMessage);
84
        }
85
86
        return $response;
87
    }
88
}
89