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 ( a4fe70...97e8d1 )
by Cody
03:11
created

Discord   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 99
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 4 1
A getPrivateChannel() 0 4 1
B request() 0 29 5
1
<?php
2
3
namespace NotificationChannels\Discord;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use GuzzleHttp\Client as HttpClient;
8
use GuzzleHttp\Exception\RequestException;
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://discordapp.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 7
    }
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
     * Create and/or get a private channel with a Discord user.
59
     *
60
     * @param mixed $user
61
     *
62
     * @return string
63
     */
64 1
    public function getPrivateChannel($user)
65
    {
66 1
        return $this->request('POST', 'users/@me/channels', ['recipient_id' => $user])['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
                'headers' => [
87 6
                    'Authorization' => 'Bot '.$this->token,
88 6
                ],
89 6
                'json' => $data,
90 6
            ]);
91 6
        } catch (RequestException $exception) {
92 2
            if ($response = $exception->getResponse()) {
93 1
                throw CouldNotSendNotification::serviceRespondedWithAnHttpError($response);
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);
105
        }
106
107 2
        return $body;
108
    }
109
}
110