1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Message\MessageInterface, |
13
|
|
|
Zbox\UnifiedPush\Message\RecipientDevice; |
14
|
|
|
use Zbox\UnifiedPush\Exception\DomainException, |
15
|
|
|
Zbox\UnifiedPush\Exception\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Application |
19
|
|
|
* @package Zbox\UnifiedPush |
20
|
|
|
*/ |
21
|
|
|
class Application |
22
|
|
|
{ |
23
|
|
|
const APPS_CREDENTIALS_FILENAME = 'applications_credentials.json'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $credentialsFilePath; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $applicationName; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $config; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \ArrayObject |
42
|
|
|
*/ |
43
|
|
|
private $messages; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \ArrayIterator |
47
|
|
|
*/ |
48
|
|
|
private $invalidRecipients; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $applicationName |
52
|
|
|
*/ |
53
|
|
|
public function __construct($applicationName) |
54
|
|
|
{ |
55
|
|
|
$this->setApplicationName($applicationName); |
56
|
|
|
$this->messages = new \ArrayObject(); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns path to application-based notification services credentials |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getCredentialsFilePath() |
67
|
|
|
{ |
68
|
|
|
if (!file_exists($this->credentialsFilePath)) { |
69
|
|
|
$credentialsDistFilename = sprintf('%s.dist', static::APPS_CREDENTIALS_FILENAME); |
|
|
|
|
70
|
|
|
|
71
|
|
|
throw new InvalidArgumentException( |
72
|
|
|
sprintf( |
73
|
|
|
"Application credentials file '%s' not exists. See example %s", |
74
|
|
|
$this->credentialsFilePath, |
75
|
|
|
$credentialsDistFilename |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
return $this->credentialsFilePath; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $filePath |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function setCredentialsFilePath($filePath) |
87
|
|
|
{ |
88
|
|
|
$this->credentialsFilePath = $filePath; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getApplicationName() |
96
|
|
|
{ |
97
|
|
|
return $this->applicationName; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $applicationName |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function setApplicationName($applicationName) |
105
|
|
|
{ |
106
|
|
|
$this->applicationName = $applicationName; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets an iterator from an ArrayObject instance |
112
|
|
|
* |
113
|
|
|
* @return \ArrayIterator |
114
|
|
|
*/ |
115
|
|
|
public function getMessagesIterator() |
116
|
|
|
{ |
117
|
|
|
return $this->messages->getIterator(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Adds a message to collection |
122
|
|
|
* |
123
|
|
|
* @param MessageInterface $message |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function addMessage(MessageInterface $message) |
127
|
|
|
{ |
128
|
|
|
$messageId = $message->getMessageIdentifier(); |
129
|
|
|
$this->messages->offsetSet($messageId, $message); |
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Removes the given message from collection |
135
|
|
|
* |
136
|
|
|
* @param MessageInterface $message |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function unsetMessage(MessageInterface $message) |
140
|
|
|
{ |
141
|
|
|
$messageId = $message->getMessageIdentifier(); |
142
|
|
|
$this->messages->offsetUnset($messageId); |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return \ArrayIterator |
148
|
|
|
*/ |
149
|
|
|
public function getInvalidRecipients() |
150
|
|
|
{ |
151
|
|
|
return $this->invalidRecipients; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Adds a invalid recipient to collection |
156
|
|
|
* |
157
|
|
|
* @param string $serviceName |
158
|
|
|
* @param RecipientDevice|string $recipient |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function addInvalidRecipient($serviceName, $recipient) |
162
|
|
|
{ |
163
|
|
|
if (!in_array($serviceName, $this->getInitializedServices())) { |
164
|
|
|
throw new DomainException( |
165
|
|
|
sprintf("Recipient of an unsupported service '%s'", $serviceName) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (is_string($recipient)) { |
170
|
|
|
$messageClassName = 'Zbox\UnifiedPush\Message\Type\\' . $serviceName; |
171
|
|
|
$recipient = new RecipientDevice($recipient, $messageClassName()); |
172
|
|
|
|
173
|
|
|
$recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_REGISTERED); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->invalidRecipients->append($recipient); |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns the list of names of notification services available for the application |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function getInitializedServices() |
187
|
|
|
{ |
188
|
|
|
return array_keys($this->config); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns credentials for notification service |
193
|
|
|
* |
194
|
|
|
* @param string $serviceName |
195
|
|
|
* @throws DomainException |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getCredentialsByService($serviceName) |
199
|
|
|
{ |
200
|
|
|
if ($this->getApplicationName() && !$this->config) { |
|
|
|
|
201
|
|
|
$this->loadApplicationConfig(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (!in_array($serviceName, $this->getInitializedServices())) { |
205
|
|
|
throw new DomainException( |
206
|
|
|
sprintf("Credentials for service '%s' was not initialized", $serviceName) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
return $this->config[$serviceName]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Load sender`s notification services credentials by application name |
214
|
|
|
* |
215
|
|
|
* @return $this |
216
|
|
|
* @throws DomainException |
217
|
|
|
*/ |
218
|
|
|
public function loadApplicationConfig() |
219
|
|
|
{ |
220
|
|
|
$configFilePath = $this->getCredentialsFilepath(); |
221
|
|
|
$applicationsConfig = json_decode(file_get_contents($configFilePath), true); |
222
|
|
|
|
223
|
|
|
$applicationName = $this->getApplicationName(); |
224
|
|
|
|
225
|
|
|
if (!array_key_exists($applicationName, $applicationsConfig)) { |
226
|
|
|
throw new DomainException( |
227
|
|
|
sprintf("Application '%s' is not defined.", $applicationName) |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
$this->config = $applicationsConfig[$applicationName]; |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.