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 ( c059d9...a4fe70 )
by Cody
11s
created

Discord::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
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
    public function __construct(HttpClient $http, $token)
39
    {
40
        $this->httpClient = $http;
41
        $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
    public function send($channel, array $data)
53
    {
54
        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
    public function getPrivateChannel($user)
65
    {
66
        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
    protected function request($verb, $endpoint, array $data)
81
    {
82
        $url = rtrim($this->baseUrl, '/').'/'.ltrim($endpoint, '/');
83
84
        try {
85
            $response = $this->httpClient->request($verb, $url, [
86
                'headers' => [
87
                    'Authorization' => 'Bot '.$this->token,
88
                ],
89
                'json' => $data,
90
            ]);
91
        } catch (RequestException $exception) {
92
            if ($response = $exception->getResponse()) {
93
                throw CouldNotSendNotification::serviceRespondedWithAnHttpError($response);
94
            }
95
96
            throw CouldNotSendNotification::serviceCommunicationError($exception);
97
        } catch (Exception $exception) {
98
            throw CouldNotSendNotification::serviceCommunicationError($exception);
99
        }
100
101
        $body = json_decode($response->getBody(), true);
102
103
        if (Arr::get($body, 'code', 0) > 0) {
104
            throw CouldNotSendNotification::serviceRespondedWithAnApiError($body);
105
        }
106
107
        return $body;
108
    }
109
}
110