1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace OneSignal; |
6
|
|
|
|
7
|
|
|
use OneSignal\Resolver\ResolverFactory; |
8
|
|
|
|
9
|
|
|
class Notifications extends AbstractApi |
10
|
|
|
{ |
11
|
|
|
private $resolverFactory; |
12
|
|
|
|
13
|
|
|
public function __construct(OneSignal $client, ResolverFactory $resolverFactory) |
14
|
|
|
{ |
15
|
|
|
parent::__construct($client); |
16
|
|
|
|
17
|
|
|
$this->resolverFactory = $resolverFactory; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get information about notification with provided ID. |
22
|
|
|
* |
23
|
|
|
* Application authentication key and ID must be set. |
24
|
|
|
* |
25
|
|
|
* @param string $id Notification ID |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function getOne(string $id): array |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$request = $this->createRequest('GET', "/notifications/$id?app_id={$this->client->getConfig()->getApplicationId()}"); |
30
|
|
|
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}"); |
31
|
|
|
|
32
|
|
|
return $this->client->sendRequest($request); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get information about all notifications. |
37
|
|
|
* |
38
|
|
|
* Application authentication key and ID must be set. |
39
|
|
|
* |
40
|
|
|
* @param int $limit How many notifications to return (max 50) |
41
|
|
|
* @param int $offset Results offset (results are sorted by ID) |
42
|
|
|
* @param int $kind Kind of notifications returned. Default (not set) is all notification types |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function getAll(int $limit = null, int $offset = null/*, int $kind = null */): array |
45
|
|
|
{ |
46
|
|
|
if (func_num_args() > 2 && !is_int(func_get_arg(2))) { |
47
|
|
|
trigger_deprecation('norkunas/onesignal-php-api', '2.1.0', 'Method %s() will have a third `int $kind` argument. Not defining it or passing a non integer value is deprecated.', __METHOD__); |
48
|
|
|
} elseif (__CLASS__ !== static::class) { |
49
|
|
|
$r = new \ReflectionMethod($this, __FUNCTION__); |
50
|
|
|
|
51
|
|
|
if (\count($r->getParameters()) > 2) { |
52
|
|
|
trigger_deprecation('norkunas/onesignal-php-api', '2.1.0', 'Method %s() will have a third `int $kind` argument. Not defining it or passing a non integer value is deprecated.', __METHOD__); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$query = ['app_id' => $this->client->getConfig()->getApplicationId()]; |
57
|
|
|
|
58
|
|
|
if ($limit !== null) { |
59
|
|
|
$query['limit'] = $limit; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($offset !== null) { |
63
|
|
|
$query['offset'] = $offset; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (func_num_args() > 2 && is_int(func_get_arg(2))) { |
67
|
|
|
$query['kind'] = func_get_arg(2); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$request = $this->createRequest('GET', '/notifications?'.http_build_query($query)); |
71
|
|
|
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}"); |
72
|
|
|
|
73
|
|
|
return $this->client->sendRequest($request); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Send new notification with provided data. |
78
|
|
|
* |
79
|
|
|
* Application authentication key and ID must be set. |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function add(array $data): array |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$resolvedData = $this->resolverFactory->createNotificationResolver()->resolve($data); |
84
|
|
|
|
85
|
|
|
$request = $this->createRequest('POST', '/notifications'); |
86
|
|
|
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}"); |
87
|
|
|
$request = $request->withHeader('Content-Type', 'application/json'); |
88
|
|
|
$request = $request->withBody($this->createStream($resolvedData)); |
89
|
|
|
|
90
|
|
|
return $this->client->sendRequest($request); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Open notification. |
95
|
|
|
* |
96
|
|
|
* Application authentication key and ID must be set. |
97
|
|
|
* |
98
|
|
|
* @param string $id Notification ID |
99
|
|
|
*/ |
100
|
|
|
public function open(string $id): array |
101
|
|
|
{ |
102
|
|
|
$request = $this->createRequest('PUT', "/notifications/$id"); |
103
|
|
|
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}"); |
104
|
|
|
$request = $request->withHeader('Content-Type', 'application/json'); |
105
|
|
|
$request = $request->withBody($this->createStream([ |
106
|
|
|
'app_id' => $this->client->getConfig()->getApplicationId(), |
107
|
|
|
'opened' => true, |
108
|
|
|
])); |
109
|
|
|
|
110
|
|
|
return $this->client->sendRequest($request); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Cancel notification. |
115
|
|
|
* |
116
|
|
|
* Application authentication key and ID must be set. |
117
|
|
|
* |
118
|
|
|
* @param string $id Notification ID |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function cancel(string $id): array |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$request = $this->createRequest('DELETE', "/notifications/$id?app_id={$this->client->getConfig()->getApplicationId()}"); |
123
|
|
|
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}"); |
124
|
|
|
|
125
|
|
|
return $this->client->sendRequest($request); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* View the devices sent a notification. |
130
|
|
|
* |
131
|
|
|
* Application authentication key and ID must be set. |
132
|
|
|
* |
133
|
|
|
* @param string $id Notification ID |
134
|
|
|
*/ |
135
|
|
|
public function history(string $id, array $data): array |
136
|
|
|
{ |
137
|
|
|
$resolvedData = $this->resolverFactory->createNotificationHistoryResolver()->resolve($data); |
138
|
|
|
|
139
|
|
|
$request = $this->createRequest('POST', "/notifications/$id/history"); |
140
|
|
|
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}"); |
141
|
|
|
$request = $request->withHeader('Cache-Control', 'no-cache'); |
142
|
|
|
$request = $request->withHeader('Content-Type', 'application/json'); |
143
|
|
|
$request = $request->withBody($this->createStream($resolvedData)); |
144
|
|
|
|
145
|
|
|
return $this->client->sendRequest($request); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.