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 ( 039ee8...c2c652 )
by Lester
01:49
created

EngageSparkChannel::setClientRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace LBHurtado\EngageSpark;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Notifications\Notification;
7
8
class EngageSparkChannel
9
{
10
    /** @var string */
11
    private $mode;
12
13
    /** @var string */
14
    private $clientRef;
15
16
    /** @var EngageSpark */
17
    protected $smsc;
18
19
    public function __construct(EngageSpark $smsc)
20
    {
21
        $this->smsc = $smsc;
22
    }
23
24
    /**
25
     * Send the given notification.
26
     *
27
     * @param  mixed  $notifiable
28
     * @param  Notification  $notification
29
     *
30
     * @return void
31
     */
32
    public function send($notifiable, Notification $notification)
33
    {
34
        if (! ($to = $this->getRecipients($notifiable, $notification))) {
35
            return;
36
        }
37
38
        $message = $notification->{'toEngageSpark'}($notifiable);
39
40
        if (\is_string($message)) {
41
            $message = new EngageSparkMessage($message);
42
        }
43
44
        $this->setClientRef($notification)->sendMessage($to, $message);
45
    }
46
47
    /**
48
     * Gets a list of phones from the given notifiable.
49
     *
50
     * @param  mixed  $notifiable
51
     * @param  Notification  $notification
52
     *
53
     * @return string[]
54
     */
55
    protected function getRecipients($notifiable, Notification $notification)
56
    {
57
        $to = $notifiable->routeNotificationFor('engage_spark', $notification);
58
59
        if ($to === null || $to === false || $to === '') {
60
            return [];
61
        }
62
63
        return \is_array($to) ? $to : [$to];
64
    }
65
66
    protected function sendMessage($recipients, EngageSparkMessage $message)
67
    {
68
        $this->setMode($message);
69
70
        switch ($mode = $this->getMode()) {
71
            case 'sms':
72
                $params = [
73
                    'organization_id' => $this->getOrgId(),
74
                    'mobile_numbers'  => $recipients,
75
                    'message'         => $message->content,
76
                    'recipient_type'  => $message->recipient_type,
77
                    'sender_id'       => $this->getSenderId($message),
78
                ];
79
                break;
80
81
            case 'topup':
82
                $params = [
83
                    'organizationId'  => $this->getOrgId(),
84
                    'phoneNumber'     => Arr::first($recipients),
85
                    'maxAmount'       => $message->air_time,
86
                    'clientRef'       => $this->getClientRef(),
87
                ];
88
                break;
89
90
            default:
91
                # code...
92
                break;
93
        }
94
95
        // if ($message->sendAt instanceof \DateTimeInterface) {
96
        //     $params['time'] = '0'.$message->sendAt->getTimestamp();
97
        // }
98
99
        $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...
100
    }
101
102
    protected function getWebHook(EngageSparkMessage $message)
103
    {
104
        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...
105
    }
106
107
    protected function getOrgId()
108
    {
109
        return $this->smsc->getOrgId();
110
    }
111
112
    public function getClientRef()
113
    {
114
        return $this->clientRef;
115
    }
116
117
    protected function getSenderId(EngageSparkMessage $message)
118
    {
119
        return $message->sender_id ? $message->sender_id : $this->smsc->getSenderId();
120
    }
121
122
    protected function setClientRef(Notification $notification)
123
    {
124
        $this->clientRef = $notification->id;
125
126
        return $this;
127
    }
128
129
    protected function getMode()
130
    {
131
        return $this->mode;
132
    }
133
134
    protected function setMode(EngageSparkMessage $message)
135
    {
136
        $this->mode = $message->mode;
137
138
        return $this;
139
    }
140
}
141