1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace OneSignal; |
6
|
|
|
|
7
|
|
|
use OneSignal\Exception\BadMethodCallException; |
8
|
|
|
use OneSignal\Exception\InvalidArgumentException; |
9
|
|
|
use OneSignal\Exception\JsonException; |
10
|
|
|
use OneSignal\Resolver\ResolverFactory; |
11
|
|
|
use Psr\Http\Client\ClientInterface; |
12
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
13
|
|
|
use Psr\Http\Message\RequestInterface; |
14
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @method Apps apps() |
18
|
|
|
* @method Devices devices() |
19
|
|
|
* @method Notifications notifications() |
20
|
|
|
*/ |
21
|
|
|
class OneSignal |
22
|
|
|
{ |
23
|
|
|
public const API_URL = 'https://onesignal.com/api/v1'; |
24
|
|
|
|
25
|
|
|
private $config; |
26
|
|
|
private $httpClient; |
27
|
|
|
private $requestFactory; |
28
|
|
|
private $streamFactory; |
29
|
|
|
private $resolverFactory; |
30
|
|
|
|
31
|
|
|
public function __construct(Config $config, ClientInterface $httpClient, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory) |
32
|
|
|
{ |
33
|
|
|
$this->config = $config; |
34
|
|
|
$this->httpClient = $httpClient; |
35
|
|
|
$this->requestFactory = $requestFactory; |
36
|
|
|
$this->streamFactory = $streamFactory; |
37
|
|
|
$this->resolverFactory = new ResolverFactory($this->config); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getConfig(): Config |
41
|
|
|
{ |
42
|
|
|
return $this->config; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getRequestFactory(): RequestFactoryInterface |
46
|
|
|
{ |
47
|
|
|
return $this->requestFactory; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getStreamFactory(): StreamFactoryInterface |
51
|
|
|
{ |
52
|
|
|
return $this->streamFactory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function sendRequest(RequestInterface $request): array |
56
|
|
|
{ |
57
|
|
|
$response = $this->httpClient->sendRequest($request); |
58
|
|
|
|
59
|
|
|
$contentType = $response->getHeader('Content-Type')[0] ?? 'application/json'; |
60
|
|
|
|
61
|
|
|
if (!preg_match('/\bjson\b/i', $contentType)) { |
62
|
|
|
throw new JsonException("Response content-type is '$contentType' while a JSON-compatible one was expected."); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$content = $response->getBody()->__toString(); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$content = json_decode($content, true, 512, JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); |
69
|
|
|
} catch (\JsonException $e) { |
70
|
|
|
throw new JsonException($e->getMessage(), $e->getCode(), $e); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!\is_array($content)) { |
74
|
|
|
throw new JsonException(sprintf('JSON content was expected to decode to an array, %s returned.', \gettype($content))); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $content; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return object |
82
|
|
|
* |
83
|
|
|
* @throws InvalidArgumentException |
84
|
|
|
*/ |
85
|
|
|
public function api(string $name) |
86
|
|
|
{ |
87
|
|
|
switch ($name) { |
88
|
|
|
case 'apps': |
89
|
|
|
$api = new Apps($this, $this->resolverFactory); |
90
|
|
|
|
91
|
|
|
break; |
92
|
|
|
case 'devices': |
93
|
|
|
$api = new Devices($this, $this->resolverFactory); |
94
|
|
|
|
95
|
|
|
break; |
96
|
|
|
case 'notifications': |
97
|
|
|
$api = new Notifications($this, $this->resolverFactory); |
98
|
|
|
|
99
|
|
|
break; |
100
|
|
|
default: |
101
|
|
|
throw new InvalidArgumentException("Undefined api instance called: '$name'."); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $api; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function __call($name, $args) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
return $this->api($name); |
111
|
|
|
} catch (InvalidArgumentException $e) { |
112
|
|
|
throw new BadMethodCallException("Undefined method called: '$name'."); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|