|
1
|
|
|
<?php |
|
2
|
|
|
namespace nstdio\notymo; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class PushNotificationComponent |
|
6
|
|
|
*/ |
|
7
|
|
|
class PushNotification implements PushNotificationInterface, LifeCycleCallback |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var PushNotificationInterface[] |
|
11
|
|
|
*/ |
|
12
|
|
|
private $notificationImpl = array(); |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* PushNotification constructor. |
|
16
|
|
|
* |
|
17
|
|
|
* @param array $config Configuration for notification implementations. |
|
18
|
|
|
* |
|
19
|
|
|
* Available keys. |
|
20
|
|
|
* - skipApns Whether use APNS notifications. |
|
21
|
|
|
* - skipGcm Whether use GCM notifications |
|
22
|
|
|
* - apns |
|
23
|
|
|
* - live |
|
24
|
|
|
* - cert |
|
25
|
|
|
* - sandboxCert |
|
26
|
|
|
* - gcm |
|
27
|
|
|
* - apiKey Google Api key for GCM service. |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function __construct($config = array()) |
|
30
|
|
|
{ |
|
31
|
3 |
|
$this->notificationImpl = array(); |
|
32
|
|
|
|
|
33
|
3 |
|
$this->initApns($config); |
|
34
|
2 |
|
$this->initGcm($config); |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param $config |
|
39
|
|
|
*/ |
|
40
|
3 |
|
private function initApns($config) |
|
41
|
|
|
{ |
|
42
|
3 |
|
if (!isset($config['skipApns'])) { |
|
43
|
2 |
|
if (!isset($config['apns'])) { |
|
44
|
1 |
|
throw new \InvalidArgumentException("Configuration required for APNSNotification."); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
$args = array_merge(array('live' => false, 'cert' => null, 'sandboxCert' => null), $config['apns']); |
|
48
|
1 |
|
$this->notificationImpl['apns'] = new APNSNotification($args['live'], $args['cert'], $args['sandboxCert']); |
|
|
|
|
|
|
49
|
1 |
|
} |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $config |
|
54
|
|
|
*/ |
|
55
|
2 |
|
private function initGcm($config) |
|
56
|
|
|
{ |
|
57
|
2 |
|
if (!isset($config['skipGcm'])) { |
|
58
|
2 |
|
if (!isset($config['gcm']) || !isset($config['gcm']['apiKey'])) { |
|
59
|
1 |
|
throw new \InvalidArgumentException("Configuration required for GCMNotification."); |
|
60
|
|
|
} |
|
61
|
1 |
|
$this->notificationImpl['gcm'] = new GCMNotification($config['gcm']['apiKey']); |
|
62
|
1 |
|
} |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* |
|
67
|
|
|
*/ |
|
68
|
|
|
public function send() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->invokeMethod('send'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function invokeMethod($method, $args = array()) |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($this->notificationImpl as $item) { |
|
76
|
|
|
call_user_func_array(array($item, $method), $args); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function enqueue(MessageInterface $message) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($message->getType() === MessageInterface::TYPE_IOS && isset($this->notificationImpl['apns'])) { |
|
83
|
|
|
$this->notificationImpl['apns']->enqueue($message); |
|
84
|
|
|
} elseif ($message->getType() === MessageInterface::TYPE_ANDROID && isset($this->notificationImpl['gcm'])) { |
|
85
|
|
|
$this->notificationImpl['gcm']->enqueue($message); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function setStreamWrapper(Connection $wrapper) |
|
90
|
|
|
{ |
|
91
|
|
|
throw new \RuntimeException('Not yet implemented.'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function onComplete(callable $param) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->invokeMethod('onSent', array($param)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Will be called when the every message was sent. |
|
101
|
|
|
* |
|
102
|
|
|
* @param callable $callback |
|
103
|
|
|
*/ |
|
104
|
|
|
public function onEachSent(callable $callback) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->invokeMethod('onEachSent', array($callback)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Removes all callbacks. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function detach() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->invokeMethod('detach'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param int $count |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setRetryCount($count) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->invokeMethod('setRetryCount', array($count)); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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.