-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatiniumNotifier.php
More file actions
163 lines (157 loc) · 5.32 KB
/
PlatiniumNotifier.php
File metadata and controls
163 lines (157 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Openium\PlatiniumBundle;
use Openium\PlatiniumBundle\Exception\PushException;
use Openium\PlatiniumBundle\Entity\PlatiniumPushInformation;
use Openium\PlatiniumBundle\Entity\PlatiniumPushNotification;
use Openium\PlatiniumBundle\Entity\PlatiniumPushResponse;
use Openium\PlatiniumBundle\Service\PlatiniumParameterBagService;
/**
* Class PlatiniumNotifier
*
* @package Openium\PlatiniumBundle
*/
class PlatiniumNotifier
{
/**
* PlatiniumNotifier constructor.
*/
public function __construct(
private readonly PlatiniumClient $client,
private readonly PlatiniumParameterBagService $platiniumParameterBagService,
private readonly string $notifyPath,
private readonly string $subscribedPath
) {
}
/**
* notify
* send push
* $message is required
* if you want geolocated push you have to defined all of $latitude, $longitude, $tolerance, $radius
*
* @param string $message the message to push
* @param string[] $groups notification groups
* @param string[] $languages notification langs
* @param bool $langNotIn
* @param float|null $latitude for geolocated push
* @param float|null $longitude for geolocated push
* @param int|null $tolerance for geolocated push
* @param int|null $radius for geolocated push
* @param array<string, string> $paramsBag
* @param int $badgeValue
* @param bool $newsStand
* @param string|null $sound
*
* @throws PushException if push is not sent
* @return bool true => push is sent to platinium
*/
public function notify(
string $message,
array $groups = [],
array $languages = [],
bool $langNotIn = false,
?float $latitude = null,
?float $longitude = null,
?int $tolerance = null,
?int $radius = null,
array $paramsBag = [],
int $badgeValue = 0,
bool $newsStand = false,
?string $sound = null
): bool {
$notificationInformation = new PlatiniumPushInformation($groups, $languages, $langNotIn);
if ($latitude && $longitude && $radius && $tolerance) {
$notificationInformation->setGeolocation($latitude, $longitude, $tolerance, $radius);
}
$notification = new PlatiniumPushNotification(
$message,
$paramsBag,
$badgeValue,
$newsStand,
$sound
);
$parameterBag = $this->platiniumParameterBagService->createPushParam(
$notificationInformation,
$notification
);
$response = $this->client->send($this->notifyPath, $parameterBag);
$this->verifyResponse($response);
// TODO use result for ?
return true;
}
/**
* subscribed
* get number of subscriber
*
* @param string[] $groups notification groups
* @param string[] $languages notification langs
* @param float|null $latitude for geolocated push
* @param float|null $longitude for geolocated push
* @param int|null $tolerance for geolocated push
* @param int|null $radius for geolocated push
*/
public function subscribed(
array $groups = [],
array $languages = [],
bool $langNotIn = false,
?float $latitude = null,
?float $longitude = null,
?int $tolerance = null,
?int $radius = null
): int {
$notificationInformation = new PlatiniumPushInformation($groups, $languages, $langNotIn);
if (
$latitude !== null
&& $longitude !== null
&& $radius !== null
&& $tolerance !== null
) {
$notificationInformation->setGeolocation($latitude, $longitude, $tolerance, $radius);
}
$notification = new PlatiniumPushNotification();
$parameterBag = $this->platiniumParameterBagService->createPushParam(
$notificationInformation,
$notification
);
$response = $this->client->send($this->subscribedPath, $parameterBag);
$content = json_decode($response->getResult(), true);
return array_key_exists('result', $content) ? $content['result'] : 0;
}
/**
* verifyResponse
*
* @throws PushException
*/
public function verifyResponse(PlatiniumPushResponse $response): bool
{
if ($response->getStatus() !== PlatiniumPushResponse::STATUS_SUCCESS) {
$errorMessage = sprintf(
'Status: %s\nResult: %s',
$response->getStatus(),
$response->getResult()
);
throw new PushException($errorMessage);
}
$data = json_decode($response->getResult(), true);
if ($data === null) {
throw new PushException('Push Send Failed : JSON Parse Failed.');
}
$responseKeys = [
'id',
'is_dev',
'ids_groups',
'langs',
'notification_per_minute',
'creation_date',
'params',
'state',
'origin',
];
foreach ($responseKeys as $key) {
if (!array_key_exists($key, $data)) {
$errorMessage = 'Push Send Failed : invalid result. Missing ' . $key;
throw new PushException($errorMessage);
}
}
return true;
}
}