Passed
Push — main ( 30e749...0f4ded )
by
unknown
17:24 queued 14:12
created

Notification::firebaseRequest()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 12
rs 10
1
<?php
2
3
namespace MedianetDev\CloudMessage\Drivers;
4
5
trait Notification
6
{
7
8
    protected static function firebaseRequest(string $url, array $payload, array $headers = [], $method = 'POST')
9
    {
10
        if (isset($payload['message']['notification']['data']) && ! empty($payload['message']['notification']['data'])) {
11
            foreach ($payload['message']['notification']['data'] as $key => $v) {
12
                $payload['message']['notification']['data'][$key] = (string) $v;
13
            }
14
15
            $payload['message']['data'] = $payload['message']['notification']['data'];
16
            unset($payload['message']['notification']['data']);
17
        }
18
19
        return self::request($url, json_encode($payload), $headers, $method);
20
    }
21
22
    protected static function request(string $url, string $payload, array $headers = [], $method = 'POST')
23
    {
24
        $ch = curl_init();
25
        curl_setopt($ch, CURLOPT_URL, $url);
26
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
27
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
28
        if ('POST' == $method) {
29
            curl_setopt($ch, CURLOPT_POST, true);
30
        }
31
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
32
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
33
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
34
        $data = curl_exec($ch);
35
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
36
        curl_close($ch);
37
38
        if (config('cloud_message.with_log')) {
39
            self::logRequest($url, $method, $headers, $payload, $statusCode, $data);
40
        }
41
42
        return [
43
            'data' => $data,
44
            'status' => 200 == $statusCode,
45
        ];
46
    }
47
48
    protected static function logRequest($url, $method, $headers, $payload, $statusCode, $data)
49
    {
50
        app('log')->debug(
51
            "\n------------------- Gateway request --------------------".
52
                "\n#Url: ".$url.
53
                "\n#Method: ".$method.
54
                "\n#Headers: ".json_encode($headers).
55
                "\n#Data: ".$payload.
56
                "\n------------------- Gateway response -------------------".
57
                "\n#Status code: ".$statusCode.
58
                "\n#Response: ".$data.
59
                "\n--------------------------------------------------------"
60
        );
61
    }
62
}
63