1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSignal; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\HttpMethodsClient as Client; |
6
|
|
|
use OneSignal\Exception\OneSignalException; |
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property-read Apps $apps Applications API service. |
11
|
|
|
* @property-read Devices $devices Devices API service. |
12
|
|
|
* @property-read Notifications $notifications Notifications API service. |
13
|
|
|
*/ |
14
|
|
|
class OneSignal |
15
|
|
|
{ |
16
|
|
|
const API_URL = 'https://onesignal.com/api/v1'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Config |
20
|
|
|
*/ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Client |
25
|
|
|
*/ |
26
|
|
|
protected $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $services = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param Config $config |
37
|
|
|
* @param Client $client |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Config $config = null, Client $client = null) |
40
|
|
|
{ |
41
|
|
|
$this->config = ($config ?: new Config()); |
42
|
|
|
|
43
|
|
|
if (null !== $client) { |
44
|
|
|
$this->client = $client; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set config. |
50
|
|
|
* |
51
|
|
|
* @param Config $config |
52
|
|
|
*/ |
53
|
|
|
public function setConfig(Config $config) |
54
|
|
|
{ |
55
|
|
|
$this->config = $config; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get config. |
60
|
|
|
* |
61
|
|
|
* @return Config |
62
|
|
|
*/ |
63
|
|
|
public function getConfig() |
64
|
|
|
{ |
65
|
|
|
return $this->config; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set client |
70
|
|
|
* |
71
|
|
|
* @param Client $client |
72
|
|
|
*/ |
73
|
|
|
public function setClient(Client $client) |
74
|
|
|
{ |
75
|
|
|
$this->client = $client; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get client |
80
|
|
|
* |
81
|
|
|
* @return Client|null |
82
|
|
|
*/ |
83
|
|
|
public function getClient() |
84
|
|
|
{ |
85
|
|
|
return $this->client; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Make a custom api request. |
90
|
|
|
* |
91
|
|
|
* @param string $method HTTP Method |
92
|
|
|
* @param string $uri URI template |
93
|
|
|
* @param array $headers |
94
|
|
|
* @param string|StreamInterface|null $body |
95
|
|
|
* |
96
|
|
|
* @throws OneSignalException |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function request($method, $uri, array $headers = [], $body = null) |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
|
|
$response = $this->client->send($method, self::API_URL . $uri, $headers, $body); |
104
|
|
|
|
105
|
|
|
return json_decode($response->getBody(), true); |
106
|
|
|
} catch (\Exception $e) { |
107
|
|
|
throw new OneSignalException($e->getMessage()); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Create required services on the fly. |
113
|
|
|
* |
114
|
|
|
* @param string $name |
115
|
|
|
* |
116
|
|
|
* @return object |
117
|
|
|
*/ |
118
|
|
|
public function __get($name) |
119
|
|
|
{ |
120
|
|
|
if (in_array($name, ['apps', 'devices', 'notifications'], true)) { |
121
|
|
|
if (isset($this->services[$name])) { |
122
|
|
|
return $this->services[$name]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$serviceName = __NAMESPACE__ . '\\' . ucfirst($name); |
126
|
|
|
|
127
|
|
|
$this->services[$name] = new $serviceName($this); |
128
|
|
|
|
129
|
|
|
return $this->services[$name]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$trace = debug_backtrace(); |
133
|
|
|
|
134
|
|
|
$error = 'Undefined property via __get(): %s in %s on line %u'; |
135
|
|
|
|
136
|
|
|
trigger_error(sprintf($error, $name, $trace[0]['file'], $trace[0]['line']), E_USER_NOTICE); |
137
|
|
|
|
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|