1
|
|
|
<?php |
2
|
|
|
namespace nstdio\notymo; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class PushNotificationComponent |
6
|
|
|
*/ |
7
|
|
|
class PushNotification implements PushNotificationInterface |
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
|
|
|
} |
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.