Completed
Pull Request — master (#3)
by
unknown
04:36
created

SnsChannel::guessDestination()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace NotificationChannels\AwsSns;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Illuminate\Notifications\Events\NotificationFailed;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\AwsSns\Exceptions\CouldNotSendNotification;
9
10
class SnsChannel
11
{
12
    /**
13
     * @var Sns
14
     */
15
    protected $sns;
16
17
    /**
18
     * @var Dispatcher
19
     */
20
    protected $events;
21
22 5
    public function __construct(Sns $sns, Dispatcher $events)
23
    {
24 5
        $this->sns = $sns;
25 5
        $this->events = $events;
26 5
    }
27
28
    /**
29
     * Send the given notification.
30
     *
31
     * @param  mixed                                  $notifiable
32
     * @param  \Illuminate\Notifications\Notification $notification
33
     * @return \Aws\Result
34
     */
35 5
    public function send($notifiable, Notification $notification)
36
    {
37
        try {
38 5
            $destination = $this->getDestination($notifiable);
39 4
            $message = $this->getMessage($notifiable, $notification);
40
41 3
            return $this->sns->send($message, $destination);
42 2
        } catch (\Exception $e) {
43 2
            $event = new NotificationFailed(
44 2
                $notifiable,
45 2
                $notification,
46 2
                'sns',
47 2
                ['message' => $e->getMessage(), 'exception' => $e]
48
            );
49 2
            $this->events->dispatch($event);
50
        }
51 2
    }
52
53
    /**
54
     * Get the phone number to send a notification to.
55
     *
56
     * @param $notifiable
57
     * @return mixed
58
     * @throws CouldNotSendNotification
59
     */
60 5
    protected function getDestination($notifiable)
61
    {
62 5
        if ($to = $notifiable->routeNotificationFor('sns')) {
63 1
            return $to;
64
        }
65
66 4
        return $this->guessDestination($notifiable);
67
    }
68
69
    /**
70
     * Try to get the phone number from some commonly used attributes for that.
71
     *
72
     * @param $notifiable
73
     * @return mixed
74
     * @throws CouldNotSendNotification
75
     */
76 4
    protected function guessDestination($notifiable)
77
    {
78 4
        $commonAttributes = ['phone', 'phone_number', 'full_phone'];
79 4
        foreach ($commonAttributes as $attribute) {
80 4
            if (isset($notifiable->$attribute)) {
81 3
                return $notifiable->$attribute;
82
            }
83
        }
84
85 1
        throw CouldNotSendNotification::invalidReceiver();
86
    }
87
88
    /**
89
     * Get the SNS Message object.
90
     *
91
     * @param $notifiable
92
     * @param  Notification             $notification
93
     * @return SnsMessage
94
     * @throws CouldNotSendNotification
95
     */
96 4
    protected function getMessage($notifiable, Notification $notification): SnsMessage
97
    {
98 4
        $message = $notification->toSns($notifiable);
0 ignored issues
show
Bug introduced by
The method toSns() does not exist on Illuminate\Notifications\Notification. ( Ignorable by Annotation )

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

98
        /** @scrutinizer ignore-call */ 
99
        $message = $notification->toSns($notifiable);

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...
99 4
        if (is_string($message)) {
100 1
            return new SnsMessage($message);
101
        }
102
103 3
        if ($message instanceof SnsMessage) {
104 2
            return $message;
105
        }
106
107 1
        throw CouldNotSendNotification::invalidMessageObject($message);
108
    }
109
}
110