1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushwoosh; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
|
9
|
|
|
class Pushwoosh |
10
|
|
|
{ |
11
|
|
|
protected $application; |
12
|
|
|
protected $client; |
13
|
|
|
protected $token; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Create a new Pushwoosh API client. |
17
|
|
|
* |
18
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
19
|
|
|
* @param string $application |
20
|
|
|
* @param string $token |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
45 |
|
public function __construct(ClientInterface $client, string $application, string $token) |
24
|
|
|
{ |
25
|
45 |
|
$this->application = $application; |
26
|
45 |
|
$this->client = $client; |
27
|
45 |
|
$this->token = $token; |
28
|
45 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create the given message in the Pushwoosh API. |
32
|
|
|
* |
33
|
|
|
* @param \NotificationChannels\Pushwoosh\PushwooshPendingMessage $message |
34
|
|
|
* @return string[] |
35
|
|
|
*/ |
36
|
45 |
|
public function createMessage(PushwooshPendingMessage $message) |
37
|
|
|
{ |
38
|
45 |
|
$headers = ['Accept' => 'application/json', 'Content-Type' => 'application/json']; |
39
|
45 |
|
$payload = \GuzzleHttp\json_encode(['request' => $message]); |
40
|
45 |
|
$request = new Request('POST', 'https://cp.pushwoosh.com/json/1.3/createMessage', $headers, $payload); |
41
|
|
|
|
42
|
|
|
try { |
43
|
45 |
|
$response = $this->client->send($request); |
44
|
|
|
} catch (GuzzleException $exception) { |
45
|
|
|
throw PushwooshException::failedTransmission($exception); |
46
|
|
|
} |
47
|
|
|
|
48
|
45 |
|
$response = \GuzzleHttp\json_decode($response->getBody()->getContents()); |
49
|
|
|
|
50
|
45 |
|
if (isset($response->status_code) && $response->status_code !== 200) { |
51
|
15 |
|
throw PushwooshException::apiError($response); |
52
|
|
|
} |
53
|
|
|
|
54
|
30 |
|
if (isset($response->response->UnknownDevices)) { |
55
|
15 |
|
throw PushwooshException::unknownDevices($response); |
56
|
|
|
} |
57
|
|
|
|
58
|
15 |
|
$message->wasSent(); |
59
|
|
|
|
60
|
15 |
|
if (isset($response->response->Messages)) { |
61
|
|
|
# Pushwoosh will not assign IDs to messages sent to less than 10 unique devices |
62
|
4 |
|
return array_map(function (string $identifier) { |
63
|
15 |
|
return $identifier !== 'CODE_NOT_AVAILABLE' ? $identifier : null; |
64
|
15 |
|
}, $response->response->Messages); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the Pushwoosh API token. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
45 |
|
public function getApiToken() |
76
|
|
|
{ |
77
|
45 |
|
return $this->token; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the Pushwoosh application code. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
45 |
|
public function getApplicationCode() |
86
|
|
|
{ |
87
|
45 |
|
return $this->application; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Send the message. |
92
|
|
|
* |
93
|
|
|
* @param \NotificationChannels\Pushwoosh\PushwooshMessage $message |
94
|
|
|
* @return \NotificationChannels\Pushwoosh\PushwooshPendingMessage |
95
|
|
|
*/ |
96
|
|
|
public function send(PushwooshMessage $message) |
97
|
|
|
{ |
98
|
|
|
return (new PushwooshPendingMessage($this))->queue($message); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|