Completed
Push — master ( 579110...b52e58 )
by Pierre
02:10
created

TRelay   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A apiRelayRequest() 0 35 5
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
        $this->apiRelayHttpCode = 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
        $this->apiRelayHttpCode = curl_getinfo(/** @scrutinizer ignore-type */ $cha, CURLINFO_HTTP_CODE);
Loading history...
51
        if ($this->apiRelayOptionHeader) {
52
            $this->apiRelayHeaders = [];
53
            $headerSize = curl_getinfo($cha, CURLINFO_HEADER_SIZE);
54
            $rawHeaders = substr($this->apiRelayResponse, 0, $headerSize);
55
            $this->apiRelayHeaders = explode("\r\n\r\n", $rawHeaders, 2);
56
            $this->apiRelayResponse = substr(
57
                $this->apiRelayResponse,
58
                $headerSize
59
            );
60
        }
61
        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

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