GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f25603...2d0cfb )
by Lester
01:10
created

EngageSparkChannel::getRecipient()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.5555
c 0
b 0
f 0
cc 5
nc 3
nop 2
1
<?php
2
3
namespace LBHurtado\EngageSpark;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Notifications\Notification;
7
use Propaganistas\LaravelPhone\PhoneNumber;
8
9
class EngageSparkChannel
10
{
11
    /** @var string */
12
    private $mode;
13
14
    /** @var string */
15
    private $clientRef;
16
17
    /** @var EngageSpark */
18
    protected $smsc;
19
20
    public function __construct(EngageSpark $smsc)
21
    {
22
        $this->smsc = $smsc;
23
    }
24
25
    /**
26
     * Send the given notification.
27
     *
28
     * @param  mixed  $notifiable
29
     * @param  Notification  $notification
30
     *
31
     * @return void
32
     */
33
    public function send($notifiable, Notification $notification)
34
    {
35
        if (! ($to = $this->getRecipients($notifiable, $notification))) {
36
            return;
37
        }
38
39
        $message = $notification->{'toEngageSpark'}($notifiable);
40
41
        if (\is_string($message)) {
42
            $message = new EngageSparkMessage($message);
43
        }
44
45
        $this->setClientRef($notification)->sendMessage($to, $message);
46
    }
47
48
    /**
49
     * Gets a list of phones from the given notifiable.
50
     *
51
     * @param  mixed  $notifiable
52
     * @param  Notification  $notification
53
     *
54
     * @return string[]
55
     */
56
    protected function getRecipients($notifiable, Notification $notification)
57
    {
58
        return $this->getRecipient($notifiable, $notification);
59
    }
60
61
    /**
62
     * Gets a list of phones from the given notifiable.
63
     *
64
     * @param  mixed  $notifiable
65
     * @param  Notification  $notification
66
     *
67
     * @return string[]
68
     */
69
    protected function getRecipient($notifiable, Notification $notification)
70
    {
71
        $to = $notifiable->routeNotificationFor('engage_spark', $notification);
72
73
        if ($to === null || $to === false || $to === []) {
74
            return '';
75
        }
76
77
        $to = \is_array($to) ? Arr::first($to) : $to;
78
79
        return $this->getFormattedMobile($to);
80
    }
81
82
    protected function sendMessage($recipient, EngageSparkMessage $message)
83
    {
84
        $this->setMode($message);
85
86
        switch ($mode = $this->getMode()) {
87
            case 'sms':
88
                $params = [
89
                    'orgId'   => $this->getOrgId(),
90
                    'to'      => $recipient,
91
                    'message' => $message->content,
92
                    'from'    => $this->getSenderId($message),
93
                ];
94
                break;
95
96
            case 'topup':
97
                $params = [
98
                    'organizationId'  => $this->getOrgId(),
99
                    'phoneNumber'     => $recipient,
100
                    'maxAmount'       => $message->air_time,
101
                    'clientRef'       => $this->getClientRef(),
102
                ];
103
                break;
104
105
            default:
106
                # code...
107
                break;
108
        }
109
110
        // if ($message->sendAt instanceof \DateTimeInterface) {
111
        //     $params['time'] = '0'.$message->sendAt->getTimestamp();
112
        // }
113
114
        $this->smsc->send($params, $mode);
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
115
    }
116
117
    protected function getWebHook(EngageSparkMessage $message)
118
    {
119
        return $this->smsc->getWebHook($message->mode);
0 ignored issues
show
Bug introduced by
The method getWebHook() does not seem to exist on object<LBHurtado\EngageSpark\EngageSpark>.

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...
120
    }
121
122
    protected function getOrgId()
123
    {
124
        return $this->smsc->getOrgId();
125
    }
126
127
    public function getClientRef()
128
    {
129
        return $this->clientRef;
130
    }
131
132
    protected function getSenderId(EngageSparkMessage $message)
133
    {
134
        return $message->sender_id ? $message->sender_id : $this->smsc->getSenderId();
135
    }
136
137
    protected function setClientRef(Notification $notification)
138
    {
139
        $this->clientRef = $notification->id;
140
141
        return $this;
142
    }
143
144
    protected function getMode()
145
    {
146
        return $this->mode;
147
    }
148
149
    protected function setMode(EngageSparkMessage $message)
150
    {
151
        $this->mode = $message->mode;
152
153
        return $this;
154
    }
155
156
    protected function getFormattedMobile($to)
157
    {
158
        return tap(PhoneNumber::make($to, 'PH')->formatE164(), function(&$recipient) {
159
            $recipient = preg_replace('/\D/', '', $recipient);
160
        });
161
    }
162
}
163