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 ( 1b2b33...d85f67 )
by Irfaq
02:49
created

Facebook::setGraphApiVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace NotificationChannels\Facebook;
4
5
use Exception;
6
use GuzzleHttp\Client as HttpClient;
7
use Psr\Http\Message\ResponseInterface;
8
use GuzzleHttp\Exception\ClientException;
9
use GuzzleHttp\Exception\GuzzleException;
10
use NotificationChannels\Facebook\Exceptions\CouldNotSendNotification;
11
12
/**
13
 * Class Facebook
14
 */
15
class Facebook
16
{
17
    /** @var HttpClient HTTP Client */
18
    protected $http;
19
20
    /** @var string|null Page Token. */
21
    protected $token;
22
23
    /** @var string Default Graph API Version */
24
    protected $graphApiVersion = '4.0';
25
26
    /**
27
     * @param  string|null      $token
28
     * @param  HttpClient|null  $httpClient
29
     */
30
    public function __construct(string $token = null, HttpClient $httpClient = null)
31
    {
32
        $this->token = $token;
33
34
        $this->http = $httpClient;
35
    }
36
37
    /**
38
     * Set Default Graph API Version.
39
     *
40
     * @param $graphApiVersion
41
     *
42
     * @return Facebook
43
     */
44
    public function setGraphApiVersion($graphApiVersion): Facebook
45
    {
46
        $this->graphApiVersion = $graphApiVersion;
47
48
        return $this;
49
    }
50
51
    /**
52
     * Get HttpClient.
53
     *
54
     * @return HttpClient
55
     */
56
    protected function httpClient(): HttpClient
57
    {
58
        return $this->http ?? new HttpClient();
59
    }
60
61
    /**
62
     * Send text message.
63
     *
64
     * @param  array  $params
65
     *
66
     * @throws GuzzleException
67
     * @throws CouldNotSendNotification
68
     * @return ResponseInterface
69
     */
70
    public function send(array $params): ResponseInterface
71
    {
72
        return $this->post('me/messages', $params);
73
    }
74
75
    /**
76
     * @param  string  $endpoint
77
     * @param  array   $params
78
     *
79
     * @throws GuzzleException
80
     * @throws CouldNotSendNotification
81
     * @return ResponseInterface
82
     */
83
    public function get(string $endpoint, array $params = []): ResponseInterface
84
    {
85
        return $this->api($endpoint, ['query' => $params]);
86
    }
87
88
    /**
89
     * @param  string  $endpoint
90
     * @param  array   $params
91
     *
92
     * @throws GuzzleException
93
     * @throws CouldNotSendNotification
94
     * @return ResponseInterface
95
     */
96
    public function post(string $endpoint, array $params = []): ResponseInterface
97
    {
98
        return $this->api($endpoint, ['json' => $params], 'POST');
99
    }
100
101
    /**
102
     * Send an API request and return response.
103
     *
104
     * @param  string  $endpoint
105
     * @param  array   $options
106
     * @param  string  $method
107
     *
108
     * @throws GuzzleException
109
     * @throws CouldNotSendNotification
110
     * @return mixed|ResponseInterface
111
     */
112
    protected function api(string $endpoint, array $options, $method = 'GET')
113
    {
114
        if (empty($this->token)) {
115
            throw CouldNotSendNotification::facebookPageTokenNotProvided('You must provide your Facebook Page token to make any API requests.');
116
        }
117
118
        $url = "https://graph.facebook.com/v{$this->graphApiVersion}/{$endpoint}?access_token={$this->token}";
119
120
        try {
121
            return $this->httpClient()->request($method, $url, $options);
122
        } catch (ClientException $exception) {
123
            throw CouldNotSendNotification::facebookRespondedWithAnError($exception);
124
        } catch (Exception $exception) {
125
            throw CouldNotSendNotification::couldNotCommunicateWithFacebook($exception);
126
        }
127
    }
128
}
129