-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPushbullet.php
More file actions
76 lines (66 loc) · 1.79 KB
/
Copy pathPushbullet.php
File metadata and controls
76 lines (66 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace NotificationChannels\Pushbullet;
use Exception;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException;
use NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification;
use Psr\Http\Message\ResponseInterface;
class Pushbullet
{
/** @var string */
private $token;
/** @var \GuzzleHttp\Client */
private $httpClient;
/**
* Create small Pushbullet client.
*
* @param string $token
* @param \GuzzleHttp\Client $httpClient
*/
public function __construct(string $token, HttpClient $httpClient)
{
$this->token = $token;
$this->httpClient = $httpClient;
}
/**
* Get url to send to Pushbullet API?
*
* @return string
*/
private function getPushbulletUrl(): string
{
return 'https://api.pushbullet.com/v2/pushes';
}
/**
* Get headers for API calls.
*
* @return array
*/
private function getHeaders(): array
{
return [
'Access-Token' => $this->token,
];
}
/**
* Send request to Pushbullet API.
*
* @param array $params
* @return \Psr\Http\Message\ResponseInterface
*/
public function send($params): ResponseInterface
{
$url = $this->getPushbulletUrl();
try {
return $this->httpClient->post($url, [
'json' => $params,
'headers' => $this->getHeaders(),
]);
} catch (ClientException $exception) {
throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception->getResponse(), $exception);
} catch (Exception $exception) {
throw CouldNotSendNotification::couldNotCommunicateWithPushbullet($exception);
}
}
}