Api
1 year ago
Errors
1 year ago
Net
1 year ago
Obj
1 year ago
Params
1 year ago
Util
1 year ago
WonderPush.php
1 year ago
WonderPush.php
394 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WonderPush; |
| 4 | |
| 5 | use WonderPush\Net\Request; |
| 6 | |
| 7 | if (count(get_included_files()) === 1) { http_response_code(403); exit(); } // Prevent direct access |
| 8 | |
| 9 | /** |
| 10 | * **WonderPush library entry class.** |
| 11 | */ |
| 12 | class WonderPush { |
| 13 | |
| 14 | /** |
| 15 | * WonderPush Management API base URL. |
| 16 | * |
| 17 | * Must contain scheme, host and optional port. |
| 18 | * Can contain an additional path. |
| 19 | * Must not end with a slash. |
| 20 | * @see getApiBase() |
| 21 | * @see getApiRoot() |
| 22 | */ |
| 23 | const WONDERPUSH_MANAGEMENT_API_BASE = 'https://management-api.wonderpush.com'; // DO NOT END WITH SLASH |
| 24 | |
| 25 | /** |
| 26 | * @deprecated |
| 27 | */ |
| 28 | const API_BASE = self::WONDERPUSH_MANAGEMENT_API_BASE; |
| 29 | |
| 30 | /** |
| 31 | * Brevo Management API base URL. |
| 32 | * |
| 33 | * Must contain scheme, host and optional port. |
| 34 | * Can contain an additional path. |
| 35 | * Must not end with a slash. |
| 36 | * @see getApiBase() |
| 37 | * @see getApiRoot() |
| 38 | */ |
| 39 | const BREVO_API_BASE = 'https://api.brevo.com/v3/wonderpush'; // DO NOT END WITH SLASH |
| 40 | |
| 41 | /** |
| 42 | * API version. |
| 43 | * @see getApiRoot() |
| 44 | */ |
| 45 | const API_VERSION = 'v1'; // "vX", NO SLASH |
| 46 | |
| 47 | /** |
| 48 | * API prefix. |
| 49 | * @see getApiRoot() |
| 50 | */ |
| 51 | const API_PREFIX = ''; // DO NOT END WITH SLASH |
| 52 | |
| 53 | /** |
| 54 | * WonderPush PHP library version. |
| 55 | */ |
| 56 | const VERSION = '2.1.2-dev'; |
| 57 | |
| 58 | /** @var Credentials */ |
| 59 | private $credentials; |
| 60 | /** @var string */ |
| 61 | private $applicationId; |
| 62 | /** @var string */ |
| 63 | private $apiBase; |
| 64 | |
| 65 | /** |
| 66 | * The logger to which the library will produce messages. |
| 67 | * @var Util\Logger |
| 68 | */ |
| 69 | private static $globalLogger; |
| 70 | |
| 71 | /** |
| 72 | * The logger to which the library will produce messages. |
| 73 | * @var Util\Logger |
| 74 | */ |
| 75 | private $logger; |
| 76 | |
| 77 | /** |
| 78 | * The HttpClient implementation to use. |
| 79 | * @var Net\HttpClientInterface |
| 80 | */ |
| 81 | private $httpClient; |
| 82 | |
| 83 | /** |
| 84 | * Lazily initialized Rest API. |
| 85 | * @var Api\Rest |
| 86 | */ |
| 87 | private $rest; |
| 88 | |
| 89 | /** |
| 90 | * Lazily initialized Deliveries endpoints. |
| 91 | * @var Api\Deliveries |
| 92 | */ |
| 93 | private $deliveries; |
| 94 | |
| 95 | /** |
| 96 | * Lazily initialized Applications endpoints. |
| 97 | * @var Api\Applications |
| 98 | */ |
| 99 | private $applications; |
| 100 | |
| 101 | /** |
| 102 | * Lazily initialized Segments endpoints. |
| 103 | * @var Api\Segments |
| 104 | */ |
| 105 | private $segments; |
| 106 | |
| 107 | /** |
| 108 | * Lazily initialized Campaigns endpoints. |
| 109 | * @var Api\Campaigns |
| 110 | */ |
| 111 | private $campaigns; |
| 112 | |
| 113 | /** |
| 114 | * Lazily initialized Installations endpoints. |
| 115 | * @var Api\Installations |
| 116 | */ |
| 117 | private $installations; |
| 118 | |
| 119 | /** |
| 120 | * Lazily initialized Stats endpoints |
| 121 | * @var Api/Stats |
| 122 | */ |
| 123 | private $stats; |
| 124 | |
| 125 | /** |
| 126 | * Lazily initialized Events endpoints. |
| 127 | * @var Api\Events |
| 128 | */ |
| 129 | private $events; |
| 130 | |
| 131 | /** |
| 132 | * Constructs the library instance that you can use to send API calls against WonderPush. |
| 133 | * |
| 134 | * This is the library entry-point. |
| 135 | * |
| 136 | * Relying on an instance instead of a static enables you to easily handle multiple projects, |
| 137 | * and does not prevent you from creating your own static singleton instance out of it. |
| 138 | * |
| 139 | * You can find your credentials in the _Settings_ / _Configuration_ page of {@link https://dashboard.wonderpush.com/ your project dashboard}. |
| 140 | * |
| 141 | * @param string|Credentials A credentials object, or a WonderPush access token string |
| 142 | * The Management API access token used to perform API calls. |
| 143 | * @param string $applicationId |
| 144 | * The application id corresponding to the access token. |
| 145 | */ |
| 146 | public function __construct($credentials, $applicationId = null) { |
| 147 | $this->credentials = is_string($credentials) ? new AccessTokenCredentials($credentials) : $credentials; |
| 148 | $this->applicationId = $applicationId; |
| 149 | } |
| 150 | |
| 151 | public function getCredentials() { |
| 152 | return $this->credentials; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * The Management API access token used to perform API calls. |
| 157 | * @return string |
| 158 | */ |
| 159 | public function getAccessToken() { |
| 160 | return $this->accessToken; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * The application id corresponding to the access token. |
| 165 | * @return string |
| 166 | */ |
| 167 | public function getApplicationId() { |
| 168 | return $this->applicationId; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * The logger to which the library will produce messages, when used outside the scope of a WonderPush instance. |
| 173 | * @return Util\Logger |
| 174 | */ |
| 175 | public static function getGlobalLogger() { |
| 176 | return self::$globalLogger; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Set the logger to which the library will produce messages, when used outside the scope of a WonderPush instance. |
| 181 | * @param Util\Logger $logger |
| 182 | */ |
| 183 | public static function setGlobalLogger(Util\Logger $logger) { |
| 184 | self::$globalLogger = $logger; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * The logger to which the library will produce messages. |
| 189 | * @return Util\Logger |
| 190 | */ |
| 191 | public function getLogger() { |
| 192 | return $this->logger ?: self::getGlobalLogger(); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Set the logger to which the library will produce messages. |
| 197 | * @param Util\Logger $logger |
| 198 | */ |
| 199 | public function setLogger(Util\Logger $logger) { |
| 200 | $this->logger = $logger; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * The HTTP client to use to perform API calls. |
| 205 | * @return Net\HttpClientInterface |
| 206 | */ |
| 207 | public function getHttpClient() { |
| 208 | if ($this->httpClient === null) { |
| 209 | $this->httpClient = new Net\CurlHttpClient($this); |
| 210 | } |
| 211 | return $this->httpClient; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Set the HTTP client to use to perform API calls. |
| 216 | * @param \WonderPush\Net\HttpClientInterface $httpClient |
| 217 | */ |
| 218 | public function setHttpClient(Net\HttpClientInterface $httpClient) { |
| 219 | $this->httpClient = $httpClient; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * The API base against which to place API calls. |
| 224 | * |
| 225 | * This is mostly useful for developing the PHP library itself, you should ignore it. |
| 226 | * |
| 227 | * @return string |
| 228 | * @see WONDERPUSH_MANAGEMENT_API_BASE |
| 229 | */ |
| 230 | public function getApiBase() { |
| 231 | if ($this->apiBase) { |
| 232 | return $this->apiBase; |
| 233 | } |
| 234 | if ($this->credentials instanceof BrevoAPIKeyV3Credentials) { |
| 235 | return self::BREVO_API_BASE; |
| 236 | } |
| 237 | return self::WONDERPUSH_MANAGEMENT_API_BASE; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * The API base against which to place API calls. |
| 242 | * |
| 243 | * This is mostly useful for developing the PHP library itself, you should ignore it. |
| 244 | * |
| 245 | * @param string $apiBase |
| 246 | */ |
| 247 | public function setApiBase($apiBase) { |
| 248 | $this->apiBase = $apiBase; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * The API root against which to place API calls. |
| 253 | * |
| 254 | * Builds on the API base, API version and API prefix. |
| 255 | * |
| 256 | * @return string |
| 257 | * @see getApiBase() |
| 258 | * @see API_VERSION |
| 259 | * @see API_PREFIX |
| 260 | */ |
| 261 | public function getApiRoot() { |
| 262 | return $this->getApiBase() . '/' . self::API_VERSION . self::API_PREFIX; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Rest API instance. |
| 267 | * @return Api\Rest |
| 268 | */ |
| 269 | public function rest() { |
| 270 | if ($this->rest === null) { |
| 271 | $this->rest = new Api\Rest($this); |
| 272 | } |
| 273 | return $this->rest; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Deliveries endpoints. |
| 278 | * @return Api\Deliveries |
| 279 | */ |
| 280 | public function deliveries() { |
| 281 | if ($this->deliveries === null) { |
| 282 | $this->deliveries = new Api\Deliveries($this); |
| 283 | } |
| 284 | return $this->deliveries; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Application endpoints |
| 289 | * @return Api\Applications |
| 290 | */ |
| 291 | public function applications() { |
| 292 | if ($this->applications === null) { |
| 293 | $this->applications = new Api\Applications($this); |
| 294 | } |
| 295 | return $this->applications; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Segments endpoints |
| 300 | * @return Api\Segments |
| 301 | */ |
| 302 | public function segments() { |
| 303 | if ($this->segments === null) { |
| 304 | $this->segments = new Api\Segments($this); |
| 305 | } |
| 306 | return $this->segments; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Installation endpoints |
| 311 | * @return Api\Installations |
| 312 | */ |
| 313 | public function installations() { |
| 314 | if ($this->installations === null) { |
| 315 | $this->installations = new Api\Installations($this); |
| 316 | } |
| 317 | return $this->installations; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Campaigns endpoints |
| 322 | * @return Api\Campaigns |
| 323 | */ |
| 324 | public function campaigns() { |
| 325 | if ($this->campaigns === null) { |
| 326 | $this->campaigns = new Api\Campaigns($this); |
| 327 | } |
| 328 | return $this->campaigns; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Stats endpoints |
| 333 | * @return Api\Stats |
| 334 | */ |
| 335 | public function stats() { |
| 336 | if ($this->stats === null) { |
| 337 | $this->stats = new Api\Stats($this); |
| 338 | } |
| 339 | return $this->stats; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Event endpoints |
| 344 | * @return Api\Events |
| 345 | */ |
| 346 | public function events() { |
| 347 | if ($this->events === null) { |
| 348 | $this->events = new Api\Events($this); |
| 349 | } |
| 350 | return $this->events; |
| 351 | } |
| 352 | |
| 353 | } |
| 354 | |
| 355 | interface Credentials { |
| 356 | /** |
| 357 | * @param Request $request |
| 358 | * @return mixed |
| 359 | */ |
| 360 | public function authenticate($request); |
| 361 | } |
| 362 | |
| 363 | class AccessTokenCredentials implements Credentials { |
| 364 | |
| 365 | /** @var string */ |
| 366 | var $accessToken; |
| 367 | |
| 368 | public function __construct($accessToken = null) { |
| 369 | $this->accessToken = $accessToken; |
| 370 | } |
| 371 | |
| 372 | public function authenticate($request) { |
| 373 | $request->setQsParam('accessToken', $this->accessToken); |
| 374 | } |
| 375 | |
| 376 | } |
| 377 | |
| 378 | class BrevoAPIKeyV3Credentials implements Credentials { |
| 379 | |
| 380 | /** @var string */ |
| 381 | var $apiKey; |
| 382 | |
| 383 | public function __construct($apiKey = null) { |
| 384 | $this->apiKey = $apiKey; |
| 385 | } |
| 386 | |
| 387 | public function authenticate($request) { |
| 388 | $request->setHeader('api-key', $this->apiKey); |
| 389 | } |
| 390 | |
| 391 | } |
| 392 | |
| 393 | WonderPush::setGlobalLogger(new Util\DefaultLogger()); |
| 394 |