CloudMessage::sendToTopic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
rs 10
1
<?php
2
3
namespace MedianetDev\CloudMessage\Facade;
4
5
class CloudMessage
6
{
7
    public static function sendToAll(array $message, string $driver = 'firebase')
8
    {
9
        // Detect driver class
10
        $class = self::getDriverClass($driver);
11
12
        // Call driver method
13
        return $class::sendToAll($message);
14
    }
15
16
    public static function sendToTokens(array $message, array $tokens, string $driver = 'firebase')
17
    {
18
        // Detect driver class
19
        $class = self::getDriverClass($driver);
20
21
        // Call driver method
22
        return $class::sendToTokens($message, $tokens);
23
    }
24
25
    public static function sendToTopic(array $message, string $topic, string $driver = 'firebase')
26
    {
27
        // Detect driver class
28
        $class = self::getDriverClass($driver);
29
30
        // Call driver method
31
        return $class::sendToTopic($message, $topic);
32
    }
33
34
    public static function subscribeToTopic(string $topic, array $tokens, string $driver = 'firebase')
35
    {
36
        // Detect driver class
37
        $class = self::getDriverClass($driver);
38
39
        // Call driver method
40
        return $class::subscribeToTopic($topic, $tokens);
41
    }
42
43
    public static function unsubscribeToTopic(string $topic, array $tokens, string $driver = 'firebase')
44
    {
45
        // Detect driver class
46
        $class = self::getDriverClass($driver);
47
48
        // Call driver method
49
        return $class::unsubscribeToTopic($topic, $tokens);
50
    }
51
52
    protected static function getDriverClass(string $driver)
53
    {
54
        $drivers = [
55
            'firebase' => 'MedianetDev\CloudMessage\Drivers\FirebaseNotification',
56
            'huawei' => 'MedianetDev\CloudMessage\Drivers\HuaweiNotification',
57
        ];
58
59
        if (! array_key_exists($driver, $drivers)) {
60
            throw new \Exception('Driver not found');
61
        }
62
63
        return $drivers[$driver];
64
    }
65
}
66