Notification   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 45
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 23 3
A firebaseRequest() 0 3 1
A logRequest() 0 12 1
1
<?php
2
3
namespace MedianetDev\CloudMessage\Drivers;
4
5
trait Notification
6
{
7
    protected static function firebaseRequest(string $url, array $payload, array $headers = [], $method = 'POST')
8
    {
9
        return self::request($url, json_encode($payload), $headers, $method);
10
    }
11
12
    protected static function request(string $url, string $payload, array $headers = [], $method = 'POST')
13
    {
14
        $ch = curl_init();
15
        curl_setopt($ch, CURLOPT_URL, $url);
16
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
17
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
18
        if ('POST' == $method) {
19
            curl_setopt($ch, CURLOPT_POST, true);
20
        }
21
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
22
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
23
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
24
        $data = curl_exec($ch);
25
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
26
        curl_close($ch);
27
28
        if (config('cloud_message.with_log')) {
29
            self::logRequest($url, $method, $headers, $payload, $statusCode, $data);
30
        }
31
32
        return [
33
            'data' => $data,
34
            'status' => 200 == $statusCode,
35
        ];
36
    }
37
38
    protected static function logRequest($url, $method, $headers, $payload, $statusCode, $data)
39
    {
40
        app('log')->debug(
41
            "\n------------------- Gateway request --------------------".
42
                "\n#Url: ".$url.
43
                "\n#Method: ".$method.
44
                "\n#Headers: ".json_encode($headers).
45
                "\n#Data: ".$payload.
46
                "\n------------------- Gateway response -------------------".
47
                "\n#Status code: ".$statusCode.
48
                "\n#Response: ".$data.
49
                "\n--------------------------------------------------------"
50
        );
51
    }
52
}
53