Completed
Push — master ( 882aff...f627fa )
by Alex
02:58
created

CurlWrapper::isHeaderExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
namespace Mezon\CustomClient;
3
4
/**
5
 * Class CurlWrapper
6
 *
7
 * @package CustomClient
8
 * @subpackage CurlWrapper
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/08/07)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * Wrapper for CURL routines
16
 */
17
class CurlWrapper
18
{
19
20
    /**
21
     * Does the specified header exists
22
     *
23
     * @param array $headers
24
     *            list of headers to be analized
25
     * @param string $header
26
     *            header to be found
27
     * @return bool true if the header was found, false otherwise
28
     */
29
    public static function isHeaderExists(array $headers, string $header): bool
0 ignored issues
show
Unused Code introduced by
The parameter $header is not used and could be removed. ( Ignorable by Annotation )

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

29
    public static function isHeaderExists(array $headers, /** @scrutinizer ignore-unused */ string $header): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        foreach ($headers as $header) {
32
            if (stripos($header, $header) !== false) {
33
                return true;
34
            }
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * Method send HTTP request
42
     *
43
     * @param string $url
44
     *            URL
45
     * @param array $headers
46
     *            Headers
47
     * @param string $method
48
     *            Request HTTP Method
49
     * @param array $data
50
     *            Request data
51
     * @return array Response body and HTTP code
52
     */
53
    public static function sendRequest(string $url, array $headers, string $method, array $data = []): array
54
    {
55
        $ch = curl_init();
56
57
        $curlConfig = [
58
            CURLOPT_URL => $url,
59
            CURLOPT_HTTPHEADER => $headers,
60
            CURLOPT_POST => ($method == 'POST'),
61
            CURLOPT_RETURNTRANSFER => true,
62
            CURLOPT_SSL_VERIFYPEER => true
63
        ];
64
65
        if ($method === 'POST') {
66
            if (self::isHeaderExists($headers, 'Content-type: application/json')) {
67
                $formData = json_encode($data);
68
            } else {
69
                $formData = [];
70
                foreach ($data as $key => $value) {
71
                    $formData[] = $key . '=' . urlencode($value);
72
                }
73
                $formData = implode('&', $formData);
74
            }
75
76
            $curlConfig[CURLOPT_POSTFIELDS] = $formData;
77
        }
78
79
        curl_setopt_array($ch, $curlConfig);
80
81
        $body = curl_exec($ch);
82
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
83
84
        curl_close($ch);
85
86
        return [
87
            $body,
88
            $code
89
        ];
90
    }
91
}
92