1: <?php
2:
3: namespace NNV\OneSignal;
4:
5: use GuzzleHttp\Client;
6: use GuzzleHttp\Exception\ClientException;
7: use GuzzleHttp\Psr7\Response;
8: use Exception;
9:
10: 11: 12:
13: class OneSignal
14: {
15: 16: 17: 18: 19:
20: private $apiBaseURI;
21:
22: 23: 24: 25: 26:
27: private $userAuthKey;
28:
29: 30: 31: 32: 33:
34: private $appIDKey;
35:
36: 37: 38: 39: 40:
41: private $restAPIKey;
42:
43: 44: 45:
46: private $guzzleClient;
47:
48: 49: 50: 51: 52:
53: private $guzzleOptions;
54:
55: 56: 57: 58: 59: 60:
61: public function __construct($userAuthKey, $appIDKey = null, $restAPIKey = null, $options = [])
62: {
63: $this->apiBaseURI = 'https://onesignal.com/api/v1/';
64: $this->userAuthKey = $userAuthKey;
65: $this->appIDKey = $appIDKey;
66: $this->restAPIKey = $restAPIKey;
67:
68: $this->guzzleOptions = $this->getDefaultOptions($options);
69: $this->guzzleClient = new Client(array_merge(
70: [
71: 'base_uri' => $this->apiBaseURI
72: ],
73: $this->guzzleOptions
74: ));
75: }
76:
77: 78: 79: 80: 81:
82: public function getAPIBaseURI()
83: {
84: return $this->apiBaseURI;
85: }
86:
87: 88: 89: 90: 91:
92: public function getUserAuth()
93: {
94: return $this->userAuthKey;
95: }
96:
97: 98: 99: 100: 101: 102:
103: public function setAppIDKey($appIDKey)
104: {
105: $this->appIDKey = $appIDKey;
106:
107: return $this;
108: }
109:
110: 111: 112: 113: 114:
115: public function getAppIDKey()
116: {
117: return $this->appIDKey;
118: }
119:
120: 121: 122: 123: 124: 125:
126: public function setRESTAPIKey($restAPIKey)
127: {
128: $this->restAPIKey = $restAPIKey;
129:
130: return $this;
131: }
132:
133: 134: 135: 136: 137:
138: public function getRESTAPIKey()
139: {
140: return $this->restAPIKey;
141: }
142:
143: 144: 145: 146: 147:
148: public function getGuzzleOptions()
149: {
150: return $this->guzzleOptions;
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160:
161: public function execute($url, $method, array $options = [])
162: {
163: $defaultOptions = [
164: 'headers' => [
165: 'Authorization' => sprintf('Basic %s', $this->userAuthKey),
166: ],
167: 'form_params' => [],
168: ];
169: $endpointURL = sprintf('%s%s', $this->apiBaseURI, $url);
170: $options = array_replace_recursive($defaultOptions, $options);
171: $response = [];
172:
173: try {
174: $response = $this->guzzleClient->request(strtoupper($method), $endpointURL, $options);
175: } catch (ClientException $clientEx) {
176: $response = $clientEx->getResponse();
177: } catch (Exception $ex) {
178: throw new Exception($ex->getMessage());
179: }
180:
181: return $this->getResponseContent($response);
182: }
183:
184: 185: 186: 187: 188: 189:
190: public function getResponseContent(Response $response)
191: {
192: $statusCode = $response->getStatusCode();
193: return (object) [
194: 'status' => ($statusCode === 200),
195: 'code' => $statusCode,
196: 'response' => json_decode($response->getBody()->getContents()),
197: ];
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207: 208:
209: public function copy($userAuthKey = null, $appIDKey = null, $restAPIKey = null, array $options = [])
210: {
211: $userAuthKey = ($userAuthKey ? $userAuthKey : $this->getUserAuth());
212: $appIDKey = ($appIDKey ? $appIDKey : $this->getAppIDKey());
213: $restAPIKey = ($restAPIKey ? $restAPIKey : $this->getRESTAPIKey());
214: $options = ($options ? $options : $this->getGuzzleOptions());
215:
216: return new OneSignal($userAuthKey, $appIDKey, $restAPIKey, $options);
217: }
218:
219: 220: 221: 222: 223: 224:
225: private function getDefaultOptions(array $options)
226: {
227: return array_merge([
228: 'timeout' => 30,
229: ], $options);
230: }
231: }
232: