CloudMessage   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 33
c 3
b 0
f 0
dl 0
loc 90
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A subscribeToTopic() 0 10 1
A sendToTokens() 0 10 1
A getDriverByOs() 0 14 4
A unsubscribeToTopic() 0 10 1
A sendToAll() 0 10 1
A sendToTopic() 0 10 1
A getDriverClass() 0 12 2
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