1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Push notification services abstraction (http://github.com/juliangut/tify) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/tify for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://github.com/juliangut/tify/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Tify\Service; |
11
|
|
|
|
12
|
|
|
use Jgut\Tify\Exception\NotificationException; |
13
|
|
|
use Jgut\Tify\Exception\ServiceException; |
14
|
|
|
use Jgut\Tify\Notification\AbstractNotification; |
15
|
|
|
use Jgut\Tify\Notification\ApnsNotification as ApnsNotification; |
16
|
|
|
use Jgut\Tify\Result; |
17
|
|
|
use Jgut\Tify\Service\Client\ApnsBuilder as ClientBuilder; |
18
|
|
|
use Jgut\Tify\Service\Message\ApnsBuilder as MessageBuilder; |
19
|
|
|
use ZendService\Apple\Exception\RuntimeException as ServiceRuntimeException; |
20
|
|
|
|
21
|
|
|
class ApnsService extends AbstractService implements SendInterface, FeedbackInterface |
22
|
|
|
{ |
23
|
|
|
const RESULT_OK = 0; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Status codes translation. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private static $statusCodes = [ |
31
|
|
|
0 => 'OK', |
32
|
|
|
1 => 'Processing Error', |
33
|
|
|
2 => 'Missing Recipient Token', |
34
|
|
|
3 => 'Missing Topic', |
35
|
|
|
4 => 'Missing Payload', |
36
|
|
|
5 => 'Invalid Token Size', |
37
|
|
|
6 => 'Invalid Topic Size', |
38
|
|
|
7 => 'Invalid Payload Size', |
39
|
|
|
8 => 'Invalid Token', |
40
|
|
|
10 => 'Shutdown', |
41
|
|
|
255 => 'Unknown Error', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \ZendService\Apple\Apns\Client\Message |
46
|
|
|
*/ |
47
|
|
|
protected $pushClient; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \ZendService\Apple\Apns\Client\Feedback |
51
|
|
|
*/ |
52
|
|
|
protected $feedbackClient; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
* |
57
|
|
|
* @throws \Jgut\Tify\Exception\ServiceException |
58
|
|
|
*/ |
59
|
|
|
public function __construct(array $parameters = [], $sandbox = false) |
60
|
|
|
{ |
61
|
|
|
parent::__construct($parameters, $sandbox); |
62
|
|
|
|
63
|
|
|
$certificatePath = $this->getParameter('certificate'); |
64
|
|
|
|
65
|
|
|
if ($certificatePath === null || !file_exists($certificatePath)) { |
66
|
|
|
throw new ServiceException(sprintf('Certificate file "%s" does not exist', $certificatePath)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* |
73
|
|
|
* @throws \InvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
public function send(AbstractNotification $notification) |
76
|
|
|
{ |
77
|
|
|
if (!$notification instanceof ApnsNotification) { |
78
|
|
|
throw new \InvalidArgumentException('Notification must be an accepted APNS notification'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$service = $this->getPushService(); |
82
|
|
|
|
83
|
|
|
$results = []; |
84
|
|
|
|
85
|
|
|
/* @var \Jgut\Tify\Recipient\ApnsRecipient $recipient */ |
86
|
|
|
foreach ($notification->getRecipients() as $recipient) { |
87
|
|
|
$message = MessageBuilder::build($recipient, $notification); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$result = new Result($recipient->getToken(), new \DateTime); |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$response = $service->send($message); |
93
|
|
|
|
94
|
|
|
if ($response->getCode() !== static::RESULT_OK) { |
95
|
|
|
$result->setStatus(Result::STATUS_ERROR); |
96
|
|
|
$result->setStatusMessage(self::$statusCodes[$response->getCode()]); |
97
|
|
|
} |
98
|
|
|
} catch (ServiceRuntimeException $exception) { |
99
|
|
|
$result->setStatus(Result::STATUS_ERROR); |
100
|
|
|
$result->setStatusMessage($exception->getMessage()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$results[] = $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$notification->setSent($results); |
107
|
|
|
|
108
|
|
|
$service->close(); |
109
|
|
|
$this->pushClient = null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
* |
115
|
|
|
* @throws \Jgut\Tify\Exception\NotificationException |
116
|
|
|
*/ |
117
|
|
|
public function feedback() |
118
|
|
|
{ |
119
|
|
|
$service = $this->getFeedbackService(); |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
$feedbackResponse = $service->feedback(); |
123
|
|
|
} catch (ServiceRuntimeException $exception) { |
124
|
|
|
throw new NotificationException($exception->getMessage(), $exception->getCode(), $exception); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$responses = []; |
128
|
|
|
|
129
|
|
|
foreach ($feedbackResponse as $response) { |
130
|
|
|
$time = new \DateTime(date('c', $response->getTime())); |
131
|
|
|
|
132
|
|
|
$responses[$response->getToken()] = $time; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$service->close(); |
136
|
|
|
$this->feedbackClient = null; |
137
|
|
|
|
138
|
|
|
return $responses; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get opened ServiceClient |
143
|
|
|
* |
144
|
|
|
* @return \ZendService\Apple\Apns\Client\Message |
145
|
|
|
*/ |
146
|
|
|
protected function getPushService() |
147
|
|
|
{ |
148
|
|
|
if (!isset($this->pushClient)) { |
149
|
|
|
$this->pushClient = ClientBuilder::buildPush( |
150
|
|
|
$this->getParameter('certificate'), |
151
|
|
|
$this->getParameter('pass_phrase'), |
152
|
|
|
$this->sandbox |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->pushClient; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get opened ServiceFeedbackClient |
161
|
|
|
* |
162
|
|
|
* @return \ZendService\Apple\Apns\Client\Feedback |
163
|
|
|
*/ |
164
|
|
|
protected function getFeedbackService() |
165
|
|
|
{ |
166
|
|
|
if (!isset($this->feedbackClient)) { |
167
|
|
|
$this->feedbackClient = ClientBuilder::buildFeedback( |
168
|
|
|
$this->getParameter('certificate'), |
169
|
|
|
$this->getParameter('pass_phrase'), |
170
|
|
|
$this->sandbox |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $this->feedbackClient; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
protected function getDefinedParameters() |
181
|
|
|
{ |
182
|
|
|
return ['certificate', 'pass_phrase']; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
protected function getDefaultParameters() |
189
|
|
|
{ |
190
|
|
|
return [ |
191
|
|
|
'pass_phrase' => null, |
192
|
|
|
]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
|
|
protected function getRequiredParameters() |
199
|
|
|
{ |
200
|
|
|
return ['certificate']; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.