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.

Discord   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
c 3
b 0
f 0
dl 0
loc 97
ccs 25
cts 25
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrivateChannel() 0 3 1
A __construct() 0 4 1
A send() 0 3 1
A request() 0 28 5
1
<?php
2
3
namespace NotificationChannels\Discord;
4
5
use Exception;
6
use GuzzleHttp\Client as HttpClient;
7
use GuzzleHttp\Exception\RequestException;
8
use Illuminate\Support\Arr;
9
use NotificationChannels\Discord\Exceptions\CouldNotSendNotification;
10
11
class Discord
12
{
13
    /**
14
     * Discord API base URL.
15
     *
16
     * @var string
17
     */
18
    protected $baseUrl = 'https://discord.com/api';
19
20
    /**
21
     * API HTTP client.
22
     *
23
     * @var \GuzzleHttp\Client
24
     */
25
    protected $httpClient;
26
27
    /**
28
     * Discord API token.
29
     *
30
     * @var string
31
     */
32
    protected $token;
33
34
    /**
35
     * @param \GuzzleHttp\Client $http
36
     * @param string $token
37
     */
38 7
    public function __construct(HttpClient $http, $token)
39
    {
40 7
        $this->httpClient = $http;
41 7
        $this->token = $token;
42
    }
43
44
    /**
45
     * Send a message to a Discord channel.
46
     *
47
     * @param string $channel
48
     * @param array $data
49
     *
50
     * @return array
51
     */
52 5
    public function send($channel, array $data)
53
    {
54 5
        return $this->request('POST', 'channels/'.$channel.'/messages', $data);
55
    }
56
57
    /**
58
     * Get a private channel with another Discord user from their snowflake ID.
59
     *
60
     * @param string $userId
61
     *
62
     * @return string
63
     */
64 1
    public function getPrivateChannel($userId)
65
    {
66 1
        return $this->request('POST', 'users/@me/channels', ['recipient_id' => $userId])['id'];
67
    }
68
69
    /**
70
     * Perform an HTTP request with the Discord API.
71
     *
72
     * @param string $verb
73
     * @param string $endpoint
74
     * @param array $data
75
     *
76
     * @return array
77
     *
78
     * @throws \NotificationChannels\Discord\Exceptions\CouldNotSendNotification
79
     */
80 6
    protected function request($verb, $endpoint, array $data)
81
    {
82 6
        $url = rtrim($this->baseUrl, '/').'/'.ltrim($endpoint, '/');
83
84
        try {
85 6
            $response = $this->httpClient->request($verb, $url, [
86 6
                'headers' => [
87 6
                    'Authorization' => 'Bot '.$this->token,
88 6
                ],
89 6
                'json' => $data,
90 6
            ]);
91 3
        } catch (RequestException $exception) {
92 2
            if ($response = $exception->getResponse()) {
93 1
                throw CouldNotSendNotification::serviceRespondedWithAnHttpError($response, $response->getStatusCode(), $exception);
94
            }
95
96 1
            throw CouldNotSendNotification::serviceCommunicationError($exception);
97 1
        } catch (Exception $exception) {
98 1
            throw CouldNotSendNotification::serviceCommunicationError($exception);
99
        }
100
101 3
        $body = json_decode($response->getBody(), true);
102
103 3
        if (Arr::get($body, 'code', 0) > 0) {
104 1
            throw CouldNotSendNotification::serviceRespondedWithAnApiError($body, $body['code']);
105
        }
106
107 2
        return $body;
108
    }
109
}
110