CloudMessage::subscribeToTopic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 10
rs 10
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}");
0 ignored issues
show
Unused Code introduced by
ThrowNode is not reachable.

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, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
95
        }
96
    }
97
}
98