Completed
Push — master ( 631a41...46f532 )
by Pierre
02:20
created

TRelay::apiRelayRequest()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 29
c 2
b 0
f 0
nc 16
nop 4
dl 0
loc 36
ccs 0
cts 30
cp 0
crap 42
rs 8.8337
1
<?php
2
3
namespace App\Reuse\Controllers\Api;
4
5
use App\Http\Request;
6
7
trait TRelay
8
{
9
10
    protected $apiRelayResponse;
11
    protected $apiRelayHttpCode;
12
    protected $apiRelayHeaders;
13
    protected $apiRelayOptionHeader = false;
14
    protected $apiRelayOptionVerbose = false;
15
16
    /**
17
     * make http request to url with method and headers
18
     * then set apiRelayResponse with reponse content
19
     * and apiRelayHttpCode with status code
20
     *
21
     * @param string $method
22
     * @param string $url
23
     * @param array $headers
24
     * @param array $datas
25
     * @return void
26
     */
27
    protected function apiRelayRequest(string $method, string $url, array $headers = [], $datas = [])
28
    {
29
        $cha = curl_init();
30
        curl_setopt($cha, CURLOPT_VERBOSE, false);
0 ignored issues
show
Bug introduced by
It seems like $cha can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, 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

30
        curl_setopt(/** @scrutinizer ignore-type */ $cha, CURLOPT_VERBOSE, false);
Loading history...
31
        curl_setopt($cha, CURLOPT_URL, $url);
32
        curl_setopt($cha, CURLOPT_POST, ($method == Request::METHOD_POST));
33
        curl_setopt($cha, CURLOPT_TIMEOUT, 300);
34
        curl_setopt($cha, CURLOPT_USERAGENT, self::USER_AGENT);
0 ignored issues
show
Bug introduced by
The constant App\Reuse\Controllers\Api\TRelay::USER_AGENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
        curl_setopt($cha, CURLOPT_BUFFERSIZE, self::BUFFER_SIZE);
0 ignored issues
show
Bug introduced by
The constant App\Reuse\Controllers\Api\TRelay::BUFFER_SIZE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
        curl_setopt($cha, CURLOPT_HTTPHEADER, $headers);
37
        if ($this->apiRelayOptionHeader) {
38
            curl_setopt($cha, CURLOPT_VERBOSE, 1);
39
            curl_setopt($cha, CURLOPT_HEADER, 1);
40
        }
41
        if ($method == Request::METHOD_POST && $datas) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $datas of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
            curl_setopt(
43
                $cha,
44
                CURLOPT_POSTFIELDS,
45
                http_build_query($datas)
46
            );
47
        }
48
        curl_setopt($cha, CURLOPT_RETURNTRANSFER, 1);
49
        $this->apiRelayResponse = curl_exec($cha);
0 ignored issues
show
Bug introduced by
It seems like $cha can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, 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

49
        $this->apiRelayResponse = curl_exec(/** @scrutinizer ignore-type */ $cha);
Loading history...
50
        $curlInfoCode = curl_getinfo($cha, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $cha can also be of type false; however, parameter $ch of curl_getinfo() does only seem to accept resource, 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

50
        $curlInfoCode = curl_getinfo(/** @scrutinizer ignore-type */ $cha, CURLINFO_HTTP_CODE);
Loading history...
51
        $this->apiRelayHttpCode = ($curlInfoCode === false) ? 500 : $curlInfoCode;
52
        if ($this->apiRelayOptionHeader) {
53
            $this->apiRelayHeaders = [];
54
            $headerSize = curl_getinfo($cha, CURLINFO_HEADER_SIZE);
55
            $rawHeaders = substr($this->apiRelayResponse, 0, $headerSize);
56
            $this->apiRelayHeaders = explode("\r\n\r\n", $rawHeaders, 2);
57
            $this->apiRelayResponse = substr(
58
                $this->apiRelayResponse,
59
                $headerSize
60
            );
61
        }
62
        curl_close($cha);
0 ignored issues
show
Bug introduced by
It seems like $cha can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, 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

62
        curl_close(/** @scrutinizer ignore-type */ $cha);
Loading history...
63
    }
64
}
65