Skip to content

Commit 2429689

Browse files
committed
Add method to set default Graph API version, Micro optimizations and typehint.
1 parent 3c569ea commit 2429689

File tree

1 file changed

+54
-25
lines changed

1 file changed

+54
-25
lines changed

src/Facebook.php

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,89 +4,118 @@
44

55
use Exception;
66
use GuzzleHttp\Client as HttpClient;
7+
use Psr\Http\Message\ResponseInterface;
78
use GuzzleHttp\Exception\ClientException;
9+
use GuzzleHttp\Exception\GuzzleException;
810
use NotificationChannels\Facebook\Exceptions\CouldNotSendNotification;
911

12+
/**
13+
* Class Facebook
14+
*/
1015
class Facebook
1116
{
1217
/** @var HttpClient HTTP Client */
1318
protected $http;
1419

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';
1725

1826
/**
19-
* @param null $token
20-
* @param HttpClient|null $httpClient
27+
* @param string|null $token
28+
* @param HttpClient|null $httpClient
2129
*/
22-
public function __construct($token = null, HttpClient $httpClient = null)
30+
public function __construct(string $token = null, HttpClient $httpClient = null)
2331
{
2432
$this->token = $token;
2533

2634
$this->http = $httpClient;
2735
}
2836

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+
2951
/**
3052
* Get HttpClient.
3153
*
3254
* @return HttpClient
3355
*/
34-
protected function httpClient()
56+
protected function httpClient(): HttpClient
3557
{
36-
return $this->http ?: $this->http = new HttpClient();
58+
return $this->http ?? new HttpClient();
3759
}
3860

3961
/**
4062
* Send text message.
4163
*
42-
* @param $params
64+
* @param array $params
4365
*
44-
* @return \Psr\Http\Message\ResponseInterface
66+
* @throws GuzzleException
67+
* @throws CouldNotSendNotification
68+
* @return ResponseInterface
4569
*/
46-
public function send($params)
70+
public function send(array $params): ResponseInterface
4771
{
4872
return $this->post('me/messages', $params);
4973
}
5074

5175
/**
52-
* @param $endpoint
53-
* @param array $params
76+
* @param string $endpoint
77+
* @param array $params
5478
*
55-
* @return \Psr\Http\Message\ResponseInterface
79+
* @throws GuzzleException
80+
* @throws CouldNotSendNotification
81+
* @return ResponseInterface
5682
*/
57-
public function get($endpoint, array $params = [])
83+
public function get(string $endpoint, array $params = []): ResponseInterface
5884
{
59-
return $this->api($endpoint, ['query' => $params], 'GET');
85+
return $this->api($endpoint, ['query' => $params]);
6086
}
6187

6288
/**
63-
* @param $endpoint
64-
* @param array $params
89+
* @param string $endpoint
90+
* @param array $params
6591
*
66-
* @return \Psr\Http\Message\ResponseInterface
92+
* @throws GuzzleException
93+
* @throws CouldNotSendNotification
94+
* @return ResponseInterface
6795
*/
68-
public function post($endpoint, array $params = [])
96+
public function post(string $endpoint, array $params = []): ResponseInterface
6997
{
7098
return $this->api($endpoint, ['json' => $params], 'POST');
7199
}
72100

73101
/**
74102
* Send an API request and return response.
75103
*
76-
* @param $endpoint
77-
* @param $options
78-
* @param string $method
104+
* @param string $endpoint
105+
* @param array $options
106+
* @param string $method
79107
*
80-
* @return mixed|\Psr\Http\Message\ResponseInterface
108+
* @throws GuzzleException
81109
* @throws CouldNotSendNotification
110+
* @return mixed|ResponseInterface
82111
*/
83-
protected function api($endpoint, $options, $method = 'GET')
112+
protected function api(string $endpoint, array $options, $method = 'GET')
84113
{
85114
if (empty($this->token)) {
86115
throw CouldNotSendNotification::facebookPageTokenNotProvided('You must provide your Facebook Page token to make any API requests.');
87116
}
88117

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}";
90119

91120
try {
92121
return $this->httpClient()->request($method, $url, $options);

0 commit comments

Comments
 (0)