|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MedianetDev\CloudMessage\Facade; |
|
4
|
|
|
|
|
5
|
|
|
class CloudMessage |
|
6
|
|
|
{ |
|
7
|
|
|
public static function sendToAll(array $message, string $os) |
|
8
|
|
|
{ |
|
9
|
|
|
// Get driver by OS |
|
10
|
|
|
$driver = self::getDriverByOs($os); |
|
11
|
|
|
|
|
12
|
|
|
// Detect driver class |
|
13
|
|
|
$class = self::getDriverClass($driver); |
|
14
|
|
|
|
|
15
|
|
|
// Call driver method |
|
16
|
|
|
return $class::sendToAll($message, $os); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public static function sendToTokens(array $message, array $tokens, string $os) |
|
20
|
|
|
{ |
|
21
|
|
|
// Get driver by OS |
|
22
|
|
|
$driver = self::getDriverByOs($os); |
|
23
|
|
|
|
|
24
|
|
|
// Detect driver class |
|
25
|
|
|
$class = self::getDriverClass($driver); |
|
26
|
|
|
|
|
27
|
|
|
// Call driver method |
|
28
|
|
|
return $class::sendToTokens($message, $tokens, $os); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function sendToTopic(array $message, string $topic, string $os) |
|
32
|
|
|
{ |
|
33
|
|
|
// Get driver by OS |
|
34
|
|
|
$driver = self::getDriverByOs($os); |
|
35
|
|
|
|
|
36
|
|
|
// Detect driver class |
|
37
|
|
|
$class = self::getDriverClass($driver); |
|
38
|
|
|
|
|
39
|
|
|
// Call driver method |
|
40
|
|
|
return $class::sendToTopic($message, $topic, $os); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function subscribeToTopic(string $topic, array $tokens, string $os) |
|
44
|
|
|
{ |
|
45
|
|
|
// Get driver by OS |
|
46
|
|
|
$driver = self::getDriverByOs($os); |
|
47
|
|
|
|
|
48
|
|
|
// Detect driver class |
|
49
|
|
|
$class = self::getDriverClass($driver); |
|
50
|
|
|
|
|
51
|
|
|
// Call driver method |
|
52
|
|
|
return $class::subscribeToTopic($topic, $tokens); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function unsubscribeToTopic(string $topic, array $tokens, string $os) |
|
56
|
|
|
{ |
|
57
|
|
|
// Get driver by OS |
|
58
|
|
|
$driver = self::getDriverByOs($os); |
|
59
|
|
|
|
|
60
|
|
|
// Detect driver class |
|
61
|
|
|
$class = self::getDriverClass($driver); |
|
62
|
|
|
|
|
63
|
|
|
// Call driver method |
|
64
|
|
|
return $class::unsubscribeToTopic($topic, $tokens); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected static function getDriverClass(string $driver) |
|
68
|
|
|
{ |
|
69
|
|
|
$drivers = [ |
|
70
|
|
|
'firebase' => 'MedianetDev\CloudMessage\Drivers\FirebaseNotification', |
|
71
|
|
|
'huawei' => 'MedianetDev\CloudMessage\Drivers\HuaweiNotification', |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
if (! array_key_exists($driver, $drivers)) { |
|
75
|
|
|
throw new \Exception('Driver not found'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $drivers[$driver]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected static function getDriverByOs(string $os) |
|
82
|
|
|
{ |
|
83
|
|
|
$os = strtolower($os); |
|
84
|
|
|
$osTypes = config('cloud_message.os_types'); |
|
85
|
|
|
|
|
86
|
|
|
switch ($os) { |
|
87
|
|
|
case strtolower($osTypes['android']): |
|
88
|
|
|
case strtolower($osTypes['ios']): |
|
89
|
|
|
return 'firebase'; |
|
90
|
|
|
case strtolower($osTypes['huawei']): |
|
91
|
|
|
return 'huawei'; |
|
92
|
|
|
default: |
|
93
|
|
|
throw new \InvalidArgumentException('OS type not supported'); |
|
94
|
|
|
throw new \Exception("OS type '{$os}' is not supported. Allowed values are: {$allowed}"); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.