1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pushmix\WebNotification; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Pushmix\WebNotification\Exceptions\InvalidConfiguration; |
7
|
|
|
|
8
|
|
|
class PushmixClient |
9
|
|
|
{ |
10
|
|
|
protected $api_url; |
11
|
|
|
protected $client; |
12
|
|
|
protected $headers; |
13
|
|
|
protected $additionalParams; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
public $requestAsync = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Constructor. |
22
|
|
|
*/ |
23
|
13 |
|
public function __construct() |
24
|
|
|
{ |
25
|
13 |
|
$this->client = new Client(); |
26
|
13 |
|
$this->headers = ['headers' => []]; |
27
|
13 |
|
$this->headers['headers']['Content-Type'] = 'application/json'; |
28
|
13 |
|
} |
29
|
|
|
|
30
|
|
|
/***/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Turn on, turn off async requests. |
34
|
|
|
* |
35
|
|
|
* @param bool $on |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
2 |
|
public function async($on = true) |
39
|
|
|
{ |
40
|
2 |
|
$this->requestAsync = $on; |
41
|
|
|
|
42
|
2 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get requestAsync. |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
1 |
|
public function getRequestAsync() |
50
|
|
|
{ |
51
|
1 |
|
return $this->requestAsync; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/***/ |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initialize API URL Parameter. |
58
|
|
|
* @return string [description] |
59
|
|
|
*/ |
60
|
5 |
|
public function initApiUrl() |
61
|
|
|
{ |
62
|
5 |
|
if (is_null(config('pushmix.api_url', null))) { |
63
|
1 |
|
throw InvalidConfiguration::configurationNotSet(); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
$this->api_url = config('pushmix.api_url'); |
67
|
|
|
|
68
|
4 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/***/ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set APi URL Parameter. |
75
|
|
|
* @param string $api_url [description] |
76
|
|
|
*/ |
77
|
1 |
|
public function setApiUrl($api_url) |
78
|
|
|
{ |
79
|
1 |
|
$this->api_url = $api_url; |
80
|
|
|
|
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/***/ |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get API URL Parameter. |
88
|
|
|
* @return string [description] |
89
|
|
|
*/ |
90
|
1 |
|
public function getApiUrl() |
91
|
|
|
{ |
92
|
1 |
|
return $this->api_url; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/***/ |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Initialize additional parameters. |
99
|
|
|
*/ |
100
|
6 |
|
public function initKey() |
101
|
|
|
{ |
102
|
6 |
|
if (is_null(config('pushmix.subscription_id', null))) { |
103
|
1 |
|
throw InvalidConfiguration::configurationNotSet(); |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
$this->additionalParams = [ |
107
|
|
|
|
108
|
5 |
|
'key_id' => config('pushmix.subscription_id'), |
109
|
|
|
]; |
110
|
|
|
|
111
|
5 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/***/ |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set SUbscription ID. |
118
|
|
|
* @param string $key_id [description] |
119
|
|
|
*/ |
120
|
1 |
|
public function setKey($key_id) |
121
|
|
|
{ |
122
|
1 |
|
$this->additionalParams = [ |
123
|
|
|
|
124
|
1 |
|
'key_id' => $key_id, |
125
|
|
|
]; |
126
|
|
|
|
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/***/ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get Subscription ID from config file. |
134
|
|
|
* |
135
|
|
|
* @throw InvalidConfiguration exception |
136
|
|
|
* @return string subscription id |
137
|
|
|
*/ |
138
|
2 |
|
public function getKey() |
139
|
|
|
{ |
140
|
2 |
|
if (is_null(config('pushmix.subscription_id', null))) { |
141
|
1 |
|
throw InvalidConfiguration::configurationNotSet(); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
return $this->additionalParams['key_id']; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/***/ |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get an array of additional parameters. |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
1 |
|
public function getAdditionalParams() |
154
|
|
|
{ |
155
|
1 |
|
return $this->additionalParams; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/***/ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Merge Notification Parameters and Send Notification to Pushmix API. |
162
|
|
|
* |
163
|
|
|
* @param array $parameters notification parameters |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
4 |
|
public function sendNotification($parameters) |
167
|
|
|
{ |
168
|
4 |
|
$parameters = array_merge($parameters, $this->additionalParams); |
169
|
4 |
|
$this->headers['body'] = json_encode($parameters); |
170
|
4 |
|
$this->headers['verify'] = false; |
171
|
|
|
|
172
|
4 |
|
return $this->post(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* POST Call to Pushmix API. |
177
|
|
|
* @param string $endPoint API endpoint |
178
|
|
|
* @return mixed |
179
|
|
|
*/ |
180
|
4 |
|
public function post() |
181
|
|
|
{ |
182
|
4 |
|
if ($this->requestAsync === true) { |
183
|
1 |
|
return $this->client->postAsync($this->api_url, $this->headers); |
184
|
|
|
} |
185
|
|
|
|
186
|
3 |
|
return $this->client->post($this->api_url, $this->headers); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/***/ |
190
|
|
|
} |
191
|
|
|
|