1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSignal; |
4
|
|
|
|
5
|
|
|
use OneSignal\Resolver\ResolverFactory; |
6
|
|
|
|
7
|
|
|
class Notifications |
8
|
|
|
{ |
9
|
|
|
const NOTIFICATIONS_LIMIT = 50; |
10
|
|
|
|
11
|
|
|
protected $api; |
12
|
|
|
|
13
|
|
|
private $resolverFactory; |
14
|
|
|
|
15
|
|
|
public function __construct(OneSignal $api, ResolverFactory $resolverFactory) |
16
|
|
|
{ |
17
|
|
|
$this->api = $api; |
18
|
|
|
$this->resolverFactory = $resolverFactory; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get information about notification with provided ID. |
23
|
|
|
* |
24
|
|
|
* Application authentication key and ID must be set. |
25
|
|
|
* |
26
|
|
|
* @param string $id Notification ID |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
public function getOne($id) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$url = '/notifications/'.$id.'?app_id='.$this->api->getConfig()->getApplicationId(); |
33
|
|
|
|
34
|
|
|
return $this->api->request('GET', $url, [ |
35
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get information about all notifications. |
41
|
|
|
* |
42
|
|
|
* Application authentication key and ID must be set. |
43
|
|
|
* |
44
|
|
|
* @param int $limit How many notifications to return (max 50) |
45
|
|
|
* @param int $offset Results offset (results are sorted by ID) |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function getAll($limit = self::NOTIFICATIONS_LIMIT, $offset = 0) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$query = [ |
52
|
|
|
'limit' => max(1, min(self::NOTIFICATIONS_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))), |
53
|
|
|
'offset' => max(0, filter_var($offset, FILTER_VALIDATE_INT)), |
54
|
|
|
'app_id' => $this->api->getConfig()->getApplicationId(), |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
return $this->api->request('GET', '/notifications?'.http_build_query($query), [ |
58
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Send new notification with provided data. |
64
|
|
|
* |
65
|
|
|
* Application authentication key and ID must be set. |
66
|
|
|
* |
67
|
|
|
* @param array $data |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function add(array $data) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$data = $this->resolverFactory->createNotificationResolver()->resolve($data); |
74
|
|
|
|
75
|
|
|
return $this->api->request('POST', '/notifications', [ |
76
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
77
|
|
|
], json_encode($data)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Open notification. |
82
|
|
|
* |
83
|
|
|
* Application authentication key and ID must be set. |
84
|
|
|
* |
85
|
|
|
* @param string $id Notification ID |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function open($id) |
90
|
|
|
{ |
91
|
|
|
return $this->api->request('PUT', '/notifications/'.$id, [ |
92
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
93
|
|
|
], json_encode([ |
94
|
|
|
'app_id' => $this->api->getConfig()->getApplicationId(), |
95
|
|
|
'opened' => true, |
96
|
|
|
])); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Cancel notification. |
101
|
|
|
* |
102
|
|
|
* Application authentication key and ID must be set. |
103
|
|
|
* |
104
|
|
|
* @param string $id Notification ID |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function cancel($id) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$url = '/notifications/'.$id.'?app_id='.$this->api->getConfig()->getApplicationId(); |
111
|
|
|
|
112
|
|
|
return $this->api->request('DELETE', $url, [ |
113
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* View the devices sent a notification. |
119
|
|
|
* |
120
|
|
|
* Application authentication key and ID must be set. |
121
|
|
|
* |
122
|
|
|
* @param string $id Notification ID |
123
|
|
|
* @param array $data |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function history($id, array $data) |
128
|
|
|
{ |
129
|
|
|
$url = '/notifications/'.$id.'/history?app_id='.$this->api->getConfig()->getApplicationId(); |
130
|
|
|
|
131
|
|
|
$data = $this->resolverFactory->createNotificationResolver()->resolve($data); |
132
|
|
|
|
133
|
|
|
return $this->api->request('POST', $url, [ |
134
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
135
|
|
|
'Cache-Control' => 'no-cache', |
136
|
|
|
], json_encode($data)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.