1
|
|
|
<?php |
2
|
|
|
namespace nstdio\notymo; |
3
|
|
|
use Closure; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class PushNotificationComponent |
7
|
|
|
*/ |
8
|
|
|
class PushNotification implements PushNotificationInterface, LifeCycleCallback |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var PushNotificationInterface[] |
12
|
|
|
*/ |
13
|
|
|
private $notificationImpl = array(); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PushNotification constructor. |
17
|
|
|
* |
18
|
|
|
* @param array $config Configuration for notification implementations. |
19
|
|
|
* |
20
|
|
|
* Available keys. |
21
|
|
|
* - skipApns Whether use APNS notifications. |
22
|
|
|
* - skipGcm Whether use GCM notifications |
23
|
|
|
* - apns |
24
|
|
|
* - live |
25
|
|
|
* - cert |
26
|
|
|
* - sandboxCert |
27
|
|
|
* - gcm |
28
|
|
|
* - apiKey Google Api key for GCM service. |
29
|
|
|
*/ |
30
|
3 |
|
public function __construct($config = array()) |
31
|
|
|
{ |
32
|
3 |
|
$this->notificationImpl = array(); |
33
|
|
|
|
34
|
3 |
|
$this->initApns($config); |
35
|
2 |
|
$this->initGcm($config); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $config |
40
|
|
|
*/ |
41
|
3 |
|
private function initApns($config) |
42
|
|
|
{ |
43
|
3 |
|
if (!isset($config['skipApns'])) { |
44
|
2 |
|
if (!isset($config['apns'])) { |
45
|
1 |
|
throw new \InvalidArgumentException("Configuration required for APNSNotification."); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$args = array_merge(array('live' => false, 'cert' => null, 'sandboxCert' => null), $config['apns']); |
49
|
1 |
|
$this->notificationImpl['apns'] = new APNSNotification($args['live'], $args['cert'], $args['sandboxCert']); |
|
|
|
|
50
|
1 |
|
} |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $config |
55
|
|
|
*/ |
56
|
2 |
|
private function initGcm($config) |
57
|
|
|
{ |
58
|
2 |
|
if (!isset($config['skipGcm'])) { |
59
|
2 |
|
if (!isset($config['gcm']) || !isset($config['gcm']['apiKey'])) { |
60
|
1 |
|
throw new \InvalidArgumentException("Configuration required for GCMNotification."); |
61
|
|
|
} |
62
|
1 |
|
$this->notificationImpl['gcm'] = new GCMNotification($config['gcm']['apiKey']); |
63
|
1 |
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
*/ |
69
|
|
|
public function send() |
70
|
|
|
{ |
71
|
|
|
$this->invokeMethod('send'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function invokeMethod($method, $args = array()) |
75
|
|
|
{ |
76
|
|
|
foreach ($this->notificationImpl as $item) { |
77
|
|
|
call_user_func_array(array($item, $method), $args); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function enqueue(MessageInterface $message) |
82
|
|
|
{ |
83
|
|
|
if ($message->getType() === MessageInterface::TYPE_IOS && isset($this->notificationImpl['apns'])) { |
84
|
|
|
$this->notificationImpl['apns']->enqueue($message); |
85
|
|
|
} elseif ($message->getType() === MessageInterface::TYPE_ANDROID && isset($this->notificationImpl['gcm'])) { |
86
|
|
|
$this->notificationImpl['gcm']->enqueue($message); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setStreamWrapper(Connection $wrapper) |
91
|
|
|
{ |
92
|
|
|
throw new \RuntimeException('Not yet implemented.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
|
|
public function onComplete(\Closure $param) |
99
|
|
|
{ |
100
|
|
|
$this->invokeMethod('onSent', array($param)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
|
|
public function onEachSent(\Closure $callback) |
107
|
|
|
{ |
108
|
|
|
$this->invokeMethod('onEachSent', array($callback)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Removes all callbacks. |
113
|
|
|
*/ |
114
|
|
|
public function detach() |
115
|
|
|
{ |
116
|
|
|
$this->invokeMethod('detach'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param int $count |
121
|
|
|
*/ |
122
|
|
|
public function setRetryCount($count) |
123
|
|
|
{ |
124
|
|
|
$this->invokeMethod('setRetryCount', array($count)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
130
|
|
|
public function onError(Closure $callback) |
131
|
|
|
{ |
132
|
|
|
$this->invokeMethod('onError', array($callback)); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.