Completed
Push — main ( 8b18f5...30e749 )
by
unknown
17s queued 15s
created

Notification::logRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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