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 ( 969a81...5f333b )
by Talv
03:02
created

MsTeamsChannel::getNotificationType()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6.6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 6
cts 10
cp 0.6
rs 9.3554
c 0
b 0
f 0
cc 5
nc 2
nop 1
crap 6.6
1
<?php
2
3
namespace NotificationChannels\MsTeams;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use Illuminate\Notifications\Notification;
8
use Illuminate\Support\Arr;
9
use NotificationChannels\MsTeams\Exceptions\CouldNotSendNotification;
10
11
class MsTeamsChannel
12
{
13
    /**
14
     * @var Client
15
     */
16
    private $client;
17
18 2
    public function __construct(Client $client)
19
    {
20 2
        $this->client = $client;
21 2
    }
22
23
    /**
24
     * @param $notifiable
25
     * @param Notification $notification
26
     * @return \Psr\Http\Message\ResponseInterface
27
     * @throws CouldNotSendNotification
28
     */
29 2
    public function send($notifiable, Notification $notification)
30
    {
31
        /** @var MsTeamsMessage $message */
32 2
        $message = $notification->toMsTeams($notifiable);
0 ignored issues
show
Bug introduced by
The method toMsTeams() 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...
33
34 2
        if ($message->toUnknown()) {
35 2
            if (! $to = $notifiable->routeNotificationFor('msteams')) {
36 1
                throw CouldNotSendNotification::connectorWebHookUrlMissing();
37
            }
38
39 1
            $message->to($to);
40
        }
41
42 1
        $data = $message->toArray();
43
44 1
        if (! $url = Arr::get($data, 'url')) {
45
            return;
46
        }
47
48 1
        $code = collect(Arr::get($data, 'code', []))
49
            ->map(function ($code){
50
                return [
51
                    "name"  => "Code",
52
                    "value" => "<pre>$code</pre>"
53
                ];
54 1
            });
55
56 1
        $potentialActions = collect(Arr::get($data, 'buttons', []))
57
            ->map(function ($button) {
58
                return (object) [
59
                    '@context' => 'http://schema.org',
60
                    '@type' => 'ViewAction',
61
                    'name' => $button['text'],
62
                    'target' => [
63
                        $button['url'],
64
                    ],
65
                ];
66 1
            });
67
68 1
        $images = collect(Arr::get($data, 'images', []))
69
            ->map(function ($image) {
70
                return (object) [
71
                    'image' => $image,
72
                ];
73 1
            });
74
75
        $payload = [
76 1
            "@type"      => "MessageCard",
77 1
            "@context"   => "http://schema.org/extensions",
78 1
            "summary" =>  Arr::get($data, 'title', 'Incoming notification'),
79 1
            "themeColor" =>  $this->getNotificationType(Arr::get($data, 'type', 'success')),
80 1
            "title"      => Arr::get($data, 'title'),
81
82
            "sections"   => [
83
                [
84 1
                    "activitySubtitle"  => sprintf("%s : (%s)",config('app.url'), config('app.env')),
85 1
                    "text" => Arr::get($data, 'text'),
86 1
                    "facts" => $code,
87 1
                    "images" => $images,
88
                ]
89
            ],
90
91 1
            'potentialAction' => $potentialActions,
92
        ];
93
94
        try {
95 1
            $response = $this->client->post($data['url'], [
96 1
                'json' => $payload,
97
            ]);
98
        } catch (ClientException $exception) {
99
            throw CouldNotSendNotification::msTeamsRespondedWithAnError($exception);
100
        } catch (\Exception $exception) {
101
            throw CouldNotSendNotification::couldNotCommunicateWithMsTeams($exception);
102
        }
103
104 1
        return $response;
105
    }
106
107
    /**
108
     * Generate a colour code use for the card accent colour...
109
     *
110
     * @param string $type
111
     * @return string
112
     */
113 1
    private function getNotificationType($type = 'info') : string
114
    {
115 1
        switch($type){
116 1
            case 'error':
117
                return '#D8000C';
118
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
119
            default:
120 1
            case 'info':
0 ignored issues
show
Unused Code introduced by
case 'info': return '#31708f'; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
121
                return '#31708f';
122
                break;
123 1
            case 'success':
124 1
                return '#4F8A10';
125
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
126
            case 'warning':
127
                return '#FEEFB3';
128
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
129
        }
130
    }
131
}
132