1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushwoosh; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use NotificationChannels\Pushwoosh\Exceptions\PushwooshException; |
|
|
|
|
9
|
|
|
|
10
|
|
|
class Pushwoosh |
11
|
|
|
{ |
12
|
|
|
protected $application; |
13
|
|
|
protected $client; |
14
|
|
|
protected $token; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Create a new Pushwoosh API client. |
18
|
|
|
* |
19
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
20
|
|
|
* @param string $application |
21
|
|
|
* @param string $token |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
45 |
|
public function __construct(ClientInterface $client, string $application, string $token) |
25
|
|
|
{ |
26
|
45 |
|
$this->application = $application; |
27
|
45 |
|
$this->client = $client; |
28
|
45 |
|
$this->token = $token; |
29
|
45 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create the given message in the Pushwoosh API. |
33
|
|
|
* |
34
|
|
|
* @param \NotificationChannels\Pushwoosh\PushwooshPendingMessage $message |
35
|
|
|
* @return string[] |
36
|
|
|
*/ |
37
|
45 |
|
public function createMessage(PushwooshPendingMessage $message) |
38
|
|
|
{ |
39
|
45 |
|
$headers = ['Accept' => 'application/json', 'Content-Type' => 'application/json']; |
40
|
45 |
|
$payload = \GuzzleHttp\json_encode(['request' => $message]); |
41
|
45 |
|
$request = new Request('POST', 'https://cp.pushwoosh.com/json/1.3/createMessage', $headers, $payload); |
42
|
|
|
|
43
|
|
|
try { |
44
|
45 |
|
$response = $this->client->send($request); |
45
|
|
|
} catch (GuzzleException $exception) { |
46
|
|
|
throw PushwooshException::failedTransmission($exception); |
47
|
|
|
} |
48
|
|
|
|
49
|
45 |
|
$response = \GuzzleHttp\json_decode($response->getBody()->getContents()); |
50
|
|
|
|
51
|
45 |
|
if (isset($response->status_code) && $response->status_code !== 200) { |
52
|
15 |
|
throw PushwooshException::apiError($response); |
53
|
|
|
} |
54
|
|
|
|
55
|
30 |
|
if (isset($response->response->UnknownDevices)) { |
56
|
15 |
|
throw PushwooshException::unknownDevices($response); |
57
|
|
|
} |
58
|
|
|
|
59
|
15 |
|
$message->wasSent(); |
60
|
|
|
|
61
|
15 |
|
if (isset($response->response->Messages)) { |
62
|
|
|
# Pushwoosh will not assign IDs to messages sent to less than 10 unique devices |
63
|
4 |
|
return array_map(function (string $identifier) { |
64
|
15 |
|
return $identifier !== 'CODE_NOT_AVAILABLE' ? $identifier : null; |
65
|
15 |
|
}, $response->response->Messages); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the Pushwoosh API token. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
45 |
|
public function getApiToken() |
77
|
|
|
{ |
78
|
45 |
|
return $this->token; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the Pushwoosh application code. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
45 |
|
public function getApplicationCode() |
87
|
|
|
{ |
88
|
45 |
|
return $this->application; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Send the message. |
93
|
|
|
* |
94
|
|
|
* @param \NotificationChannels\Pushwoosh\PushwooshMessage $message |
95
|
|
|
* @return \NotificationChannels\Pushwoosh\PushwooshPendingMessage |
96
|
|
|
*/ |
97
|
|
|
public function send(PushwooshMessage $message) |
98
|
|
|
{ |
99
|
|
|
return (new PushwooshPendingMessage($this))->queue($message); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: