-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathClient.php
130 lines (110 loc) · 3.8 KB
/
Client.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Created by PhpStorm.
* User: lkaybob
* Date: 20/03/2018
* Time: 18:58
*/
namespace phpFCMv1;
use \GuzzleHttp;
use phpFCMv1\Config\CommonConfig;
class Client {
const SEND_URL = 'https://fcm.googleapis.com/v1/projects/$0/messages:send';
const CONTENT_TYPE = 'json';
const HTTP_ERRORS_OPTION = 'http_errors';
private $credentials;
private $payload;
private $URL;
public function __construct($keyFile) {
$this -> credentials = new Credentials($keyFile);
$this -> setProjectID();
$this -> payload = array('message' => null);
}
/**
* @param Recipient $recipient : Recipient token or topic for the notificaation
* @param Notification|null $notification : Notification with title & body to send.
* Not required, if only downstream data payload is needed
* @param Data|null $data : (Optional) Downstream data payload to send
* @param CommonConfig|null $config : (Optional) CommonConfig instance to define optional characteristics
* of notification
*/
public function build(Recipient $recipient, Notification $notification = null, Data $data = null, CommonConfig $config = null) {
$result = $recipient();
$isPlayload = false;
if (!is_null($notification)) {
$result = array_replace_recursive($result, $notification());
$isPlayload = true;
}
if (!is_null($data)) {
$result = array_replace_recursive($result, $data());
$isPlayload = true;
}
if (!is_null($config)) {
$result = array_replace_recursive($result, $config());
}
if (!$isPlayload) {
throw new \UnderflowException('Neither notification or data object has not been set');
} else {
$this -> setPayload($result);
}
}
/**
* Fires built message
*/
public function fire($returnResponseArray = false) {
if(!isset($this->header_options)){
$this->header_options = array(
'headers' => array(
'Authorization' => 'Bearer ' . $this -> credentials -> getAccessToken()
)
);
}
$options = $this->header_options;
$body = array(
self::CONTENT_TYPE => $this -> getPayload(),
self::HTTP_ERRORS_OPTION => false
);
if(!isset($this->guzzle_client)){
// Class name conflict occurs, when used as "Client"
$this->guzzle_client = new GuzzleHttp\Client($options);
}
$client = $this->guzzle_client;
$response = $client -> request('POST', $this -> getURL(), $body);
if ($response -> getStatusCode() == 200) {
return true;
} else {
$result = json_decode($response -> getBody(), true);
return $result['error']['message'];
}
}
/**
* @return array
*/
public function getPayload() {
return $this -> payload;
}
/**
* @param array $payload
*/
public function setPayload(array $payload) {
$this -> payload['message'] = $payload;
}
public function setValidateOnly($option) {
if (is_bool($option))
$this -> payload['validate_only'] = $option;
else
throw new \InvalidArgumentException('validate_only option only allows boolean');
}
private function setProjectID() {
$projectId = $this -> credentials -> getProjectID();
$pattern = '/\$0/';
$result = preg_replace($pattern, $projectId, self::SEND_URL);
$this -> setURL($result);
}
private function setURL($result) {
$this -> URL = $result;
}
public function getURL() {
return $this -> URL;
}
}