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