1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Unified push notification services abstraction (http://github.com/juliangut/tify). |
5
|
|
|
* |
6
|
|
|
* @license BSD-3-Clause |
7
|
|
|
* @link https://github.com/juliangut/tify |
8
|
|
|
* @author Julián Gutiérrez <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Jgut\Tify\Adapter\Apns; |
12
|
|
|
|
13
|
|
|
use Jgut\Tify\Exception\AdapterException; |
14
|
|
|
use Jgut\Tify\Message as NotificationMessage; |
15
|
|
|
use Jgut\Tify\Notification; |
16
|
|
|
use Jgut\Tify\Receiver\ApnsReceiver; |
17
|
|
|
use ZendService\Apple\Apns\Client\AbstractClient; |
18
|
|
|
use ZendService\Apple\Apns\Client\Feedback as FeedbackClient; |
19
|
|
|
use ZendService\Apple\Apns\Client\Message as PushClient; |
20
|
|
|
use ZendService\Apple\Apns\Message\Alert as ServiceMessageAlert; |
21
|
|
|
use ZendService\Apple\Apns\Message as ServiceMessage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* APNS default service factory. |
25
|
|
|
*/ |
26
|
|
|
class DefaultFactory implements Factory |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Alert parameters list. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $alertParams = [ |
34
|
|
|
NotificationMessage::PARAMETER_TITLE, |
35
|
|
|
NotificationMessage::PARAMETER_BODY, |
36
|
|
|
NotificationMessage::PARAMETER_TITLE_LOC_KEY, |
37
|
|
|
NotificationMessage::PARAMETER_TITLE_LOC_ARGS, |
38
|
|
|
NotificationMessage::PARAMETER_BODY_LOC_KEY, |
39
|
|
|
NotificationMessage::PARAMETER_BODY_LOC_ARGS, |
40
|
|
|
NotificationMessage::PARAMETER_ACTION_LOC_KEY, |
41
|
|
|
NotificationMessage::PARAMETER_LAUNCH_IMAGE, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
* |
47
|
|
|
* @throws AdapterException |
48
|
|
|
* |
49
|
|
|
* @return PushClient |
50
|
|
|
*/ |
51
|
|
|
public function buildPushClient($certificate, $passPhrase = '', $sandbox = false) |
52
|
|
|
{ |
53
|
|
|
return $this->buildClient(new PushClient, $certificate, $passPhrase, $sandbox); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
* |
59
|
|
|
* @throws AdapterException |
60
|
|
|
* |
61
|
|
|
* @return FeedbackClient |
62
|
|
|
*/ |
63
|
|
|
public function buildFeedbackClient($certificate, $passPhrase = null, $sandbox = false) |
64
|
|
|
{ |
65
|
|
|
return $this->buildClient(new FeedbackClient, $certificate, $passPhrase, $sandbox); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get opened client. |
70
|
|
|
* |
71
|
|
|
* @param AbstractClient $client |
72
|
|
|
* @param string $certificate |
73
|
|
|
* @param string $passPhrase |
74
|
|
|
* @param bool $sandbox |
75
|
|
|
* |
76
|
|
|
* @throws AdapterException |
77
|
|
|
* |
78
|
|
|
* @return AbstractClient|PushClient|FeedbackClient |
79
|
|
|
* |
80
|
|
|
* @codeCoverageIgnore |
81
|
|
|
*/ |
82
|
|
|
protected function buildClient(AbstractClient $client, $certificate, $passPhrase = '', $sandbox = false) |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$client->open( |
86
|
|
|
(bool) $sandbox ? AbstractClient::SANDBOX_URI : AbstractClient::PRODUCTION_URI, |
87
|
|
|
$certificate, |
88
|
|
|
$passPhrase |
89
|
|
|
); |
90
|
|
|
} catch (\Exception $exception) { |
91
|
|
|
throw new AdapterException($exception->getMessage(), $exception->getCode(), $exception); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $client; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
* |
100
|
|
|
* @throws \ZendService\Apple\Exception\RuntimeException |
101
|
|
|
*/ |
102
|
|
|
public function buildPushMessage(ApnsReceiver $receiver, Notification $notification) |
103
|
|
|
{ |
104
|
|
|
$message = $notification->getMessage(); |
105
|
|
|
|
106
|
|
|
$messageId = sha1( |
107
|
|
|
sprintf( |
108
|
|
|
'%s%s%s%s', |
109
|
|
|
$receiver->getToken(), |
110
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_TITLE), |
111
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_BODY), |
112
|
|
|
time() |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
$badge = $notification->getParameter(Notification::PARAMETER_BADGE) === null |
116
|
|
|
? null |
117
|
|
|
: (int) $notification->getParameter(Notification::PARAMETER_BADGE); |
118
|
|
|
|
119
|
|
|
$pushMessage = (new ServiceMessage) |
120
|
|
|
->setId($messageId) |
121
|
|
|
->setToken($receiver->getToken()) |
122
|
|
|
->setBadge($badge) |
123
|
|
|
->setSound($notification->getParameter(Notification::PARAMETER_SOUND)) |
124
|
|
|
->setCategory($notification->getParameter(Notification::PARAMETER_CATEGORY)) |
125
|
|
|
->setCustom($message->getPayloadData()); |
126
|
|
|
|
127
|
|
|
if ($notification->getParameter(Notification::PARAMETER_CONTENT_AVAILABLE) !== null) { |
128
|
|
|
$pushMessage->setContentAvailable( |
129
|
|
|
(int) $notification->getParameter(Notification::PARAMETER_CONTENT_AVAILABLE) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (is_array($notification->getParameter(Notification::PARAMETER_URL_ARGS))) { |
134
|
|
|
$pushMessage->setUrlArgs($notification->getParameter(Notification::PARAMETER_URL_ARGS)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($notification->getParameter(Notification::PARAMETER_TTL) !== null) { |
138
|
|
|
$expire = time() + (int) $notification->getParameter(Notification::PARAMETER_TTL); |
139
|
|
|
|
140
|
|
|
$pushMessage->setExpire($expire); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($this->shouldHaveAlert($message)) { |
144
|
|
|
$pushMessage->setAlert(new ServiceMessageAlert( |
145
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_BODY), |
146
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_ACTION_LOC_KEY), |
147
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_BODY_LOC_KEY), |
148
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_BODY_LOC_ARGS), |
149
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_LAUNCH_IMAGE), |
150
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_TITLE), |
151
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_TITLE_LOC_KEY), |
152
|
|
|
$message->getParameter(NotificationMessage::PARAMETER_TITLE_LOC_ARGS) |
153
|
|
|
)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $pushMessage; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Message should have alert dictionary. |
161
|
|
|
* |
162
|
|
|
* @param NotificationMessage $message |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
private function shouldHaveAlert(NotificationMessage $message) |
167
|
|
|
{ |
168
|
|
|
$shouldHaveAlert = false; |
169
|
|
|
|
170
|
|
|
foreach (static::$alertParams as $parameter) { |
171
|
|
|
if ($message->hasParameter($parameter)) { |
172
|
|
|
$shouldHaveAlert = true; |
173
|
|
|
|
174
|
|
|
break; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $shouldHaveAlert; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.