Overview

Namespaces

  • NNV
    • OneSignal
      • API
      • Constants
      • Utils

Classes

  • NNV\OneSignal\API\App
  • NNV\OneSignal\API\Notification
  • NNV\OneSignal\API\Player
  • NNV\OneSignal\Constants\DeviceTypes
  • NNV\OneSignal\Constants\NotificationTypes
  • NNV\OneSignal\Constants\TestTypes
  • NNV\OneSignal\OneSignal
  • NNV\OneSignal\Utils\Validation
  • Overview
  • Namespace
  • Class
  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:  * OneSignal API
 12:  */
 13: class OneSignal
 14: {
 15:     /**
 16:      * API base URI
 17:      *
 18:      * @var string
 19:      */
 20:     private $apiBaseURI;
 21: 
 22:     /**
 23:      * User Auth Key
 24:      *
 25:      * @var string
 26:      */
 27:     private $userAuthKey;
 28: 
 29:     /**
 30:      * App ID Key
 31:      *
 32:      * @var string
 33:      */
 34:     private $appIDKey;
 35: 
 36:     /**
 37:      * REST API Key
 38:      *
 39:      * @var string
 40:      */
 41:     private $restAPIKey;
 42: 
 43:     /**
 44:      * @var GuzzleHttp\Client
 45:      */
 46:     private $guzzleClient;
 47: 
 48:     /**
 49:      * Guzzle extra options
 50:      *
 51:      * @var array
 52:      */
 53:     private $guzzleOptions;
 54: 
 55:     /**
 56:      * @param string $userAuthKey User auth key
 57:      * @param string $appIDKey    App ID key
 58:      * @param string $restAPIKey  REST API key
 59:      * @param array  $options     Extra options for GuzzleHttp Client
 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:      * Get API base URI
 79:      *
 80:      * @return string API base URI
 81:      */
 82:     public function getAPIBaseURI()
 83:     {
 84:         return $this->apiBaseURI;
 85:     }
 86: 
 87:     /**
 88:      * Get user auth key
 89:      *
 90:      * @return string User auth key
 91:      */
 92:     public function getUserAuth()
 93:     {
 94:         return $this->userAuthKey;
 95:     }
 96: 
 97:     /**
 98:      * Set app ID key
 99:      *
100:      * @param string $appIDKey App ID key
101:      * @return self
102:      */
103:     public function setAppIDKey($appIDKey)
104:     {
105:         $this->appIDKey = $appIDKey;
106: 
107:         return $this;
108:     }
109: 
110:     /**
111:      * Get App ID key
112:      *
113:      * @return string App ID key
114:      */
115:     public function getAppIDKey()
116:     {
117:         return $this->appIDKey;
118:     }
119: 
120:     /**
121:      * Set REST API key
122:      *
123:      * @param string $restAPIKey REST API key
124:      * @return self
125:      */
126:     public function setRESTAPIKey($restAPIKey)
127:     {
128:         $this->restAPIKey = $restAPIKey;
129: 
130:         return $this;
131:     }
132: 
133:     /**
134:      * Get REST API key
135:      *
136:      * @return string API Key for JSON API
137:      */
138:     public function getRESTAPIKey()
139:     {
140:         return $this->restAPIKey;
141:     }
142: 
143:     /**
144:      * Get Guzzle extra options
145:      *
146:      * @return array Guzzle extra options
147:      */
148:     public function getGuzzleOptions()
149:     {
150:         return $this->guzzleOptions;
151:     }
152: 
153:     /**
154:      * Execute call API
155:      *
156:      * @param  string $url     URL to call
157:      * @param  string $method  Method to call
158:      * @param  array  $options Extra options for GuzzleHttp Client
159:      * @return object Object contain response detail
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:      * Get detail response
186:      *
187:      * @param  Response $response Response from GuzzleHttp Client
188:      * @return object Response detail
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:      * Create new OneSignal instace
202:      *
203:      * @param string $userAuthKey User auth key
204:      * @param string $appIDKey    App ID key
205:      * @param string $restAPIKey  REST API key
206:      * @param array  $options     Extra options for GuzzleHttp Client
207:      * @return NNV\OneSignal\OneSignal New OneSignal instance
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:      * Get default options for GuzzleHttp Client
221:      *
222:      * @param  array  $options Extra options for GuzzleHttp Client
223:      * @return array  GuzzleHttp Client options
224:      */
225:     private function getDefaultOptions(array $options)
226:     {
227:         return array_merge([
228:             'timeout' => 30,
229:         ], $options);
230:     }
231: }
232: 
API documentation generated by ApiGen