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
Pull Request — master (#43)
by
unknown
05:33 queued 04:30
created

SmscRuChannel::send()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
rs 9.6666
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 4.0218
1
<?php
2
3
namespace NotificationChannels\SmscRu;
4
5
use Illuminate\Notifications\Notification;
6
use NotificationChannels\SmscRu\Exceptions\CouldNotSendNotification;
7
8
class SmscRuChannel
9
{
10
    /** @var SmscRuApi */
11
    protected $smsc;
12
13 5
    public function __construct(SmscRuApi $smsc)
14
    {
15 5
        $this->smsc = $smsc;
16 5
    }
17
18
    /**
19
     * Send the given notification.
20
     *
21
     * @param  mixed  $notifiable
22
     * @param  Notification  $notification
23
     *
24
     * @throws CouldNotSendNotification
25
     *
26
     * @return array|null
27
     */
28 5
    public function send($notifiable, Notification $notification): ?array
29
    {
30 5
        $message = $notification->{'toSmscRu'}($notifiable);
31
32 5
        if (\is_string($message)) {
33
            $message = new SmscRuMessage($message);
34
        }
35
36 5
        $to = $message->to;
37
38 5
        if (empty($to)) {
39 4
            if (! ($to = $this->getRecipients($notifiable, $notification))) {
40 1
                return null;
41
            }
42
        }
43
44 4
        return $this->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 4
    protected function getRecipients($notifiable, Notification $notification): array
56
    {
57 4
        $to = $notifiable->routeNotificationFor('smscru', $notification);
58
59 4
        if (empty($to)) {
60 1
            return [];
61
        }
62
63 3
        return \is_array($to) ? $to : [$to];
64
    }
65
66 4
    protected function sendMessage($recipients, SmscRuMessage $message)
67
    {
68 4
        if (\mb_strlen($message->content) > 800) {
69
            throw CouldNotSendNotification::contentLengthLimitExceeded();
70
        }
71
72
        $params = [
73 4
            'phones'  => \implode(',', $recipients),
74 4
            'mes'     => $message->content,
75 4
            'sender'  => $message->from,
76
        ];
77
78 4
        if ($message->sendAt instanceof \DateTimeInterface) {
79 1
            $params['time'] = '0'.$message->sendAt->getTimestamp();
80
        }
81
82 4
        return $this->smsc->send($params);
83
    }
84
}
85