HuaweiNotification   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sendToTokens() 0 14 1
A sendToTopic() 0 2 1
A subscribeToTopic() 0 2 1
A unsubscribeToTopic() 0 2 1
A getAccessToken() 0 12 1
A sendToAll() 0 2 1
1
<?php
2
3
namespace MedianetDev\CloudMessage\Drivers;
4
5
use MedianetDev\CloudMessage\Contracts\NotificationInterface;
6
7
class HuaweiNotification implements NotificationInterface
8
{
9
    use Notification;
10
11
    private static $urlAuth = 'https://login.vmall.com/oauth2/token';
12
    private static $pushUrl = 'https://push-api.cloud.huawei.com/v1/';
13
14
    public static function sendToAll(array $message)
15
    {
16
        // TODO: Send to all devices
17
    }
18
19
    public static function sendToTokens(array $message, array $tokens)
20
    {
21
        $structureData = [];
22
        $structureData['validate_only'] = false;
23
        $structureData['message']['data'] = json_encode($message);
24
        $structureData['message']['token'] = $tokens;
25
26
        $headers = ['Content-Type: application/json', 'Authorization: Bearer '.self::getAccessToken()];
27
28
        $url = self::$pushUrl.config('cloud_message.huawei.app_id').'/messages:send';
29
30
        $response = self::request($url, json_encode($structureData), $headers);
31
32
        return $response;
33
    }
34
35
    // Other required interface methods
36
    private static function getAccessToken()
37
    {
38
        $data = [
39
            'grant_type' => config('cloud_message.huawei.grant_type'),
40
            'client_id' => config('cloud_message.huawei.app_id'),
41
            'client_secret' => config('cloud_message.huawei.app_secret'),
42
        ];
43
44
        $response = self::request(self::$urlAuth, http_build_query($data));
45
        $result = json_decode($response['data'], true);
0 ignored issues
show
Bug introduced by
It seems like $response['data'] can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $result = json_decode(/** @scrutinizer ignore-type */ $response['data'], true);
Loading history...
46
47
        return $result['access_token'] ?? '';
48
    }
49
50
    public static function sendToTopic(array $message, string $topic)
51
    {
52
    }
53
54
    public static function subscribeToTopic(string $topic, array $tokens)
55
    {
56
    }
57
58
    public static function unsubscribeToTopic(string $topic, array $tokens)
59
    {
60
    }
61
}
62