Completed
Push — master ( 81b3a2...8e331d )
by Pierre
03:08 queued 45s
created

TRelay   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 57.89%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 42
c 5
b 0
f 0
dl 0
loc 79
ccs 22
cts 38
cp 0.5789
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apiRelayRequestSetPostData() 0 8 4
B apiRelayRequest() 0 41 9
1
<?php
2
3
namespace App\Reuse\Controllers\Api;
4
5
use App\Http\Request;
6
use App\Interfaces\Controllers\IApi;
7
8
trait TRelay
9
{
10
11
    protected $apiRelayResponse;
12
    protected $apiRelayHttpCode;
13
    protected $apiRelayHeaders;
14
    protected $apiRelayOptionHeader = false;
15
    protected $apiRelayOptionVerbose = false;
16
17
    /**
18
     * make http request to url with method and headers
19
     * then set apiRelayResponse with reponse content
20
     * and apiRelayHttpCode with status code
21
     *
22
     * @param string $method
23
     * @param string $url
24
     * @param array $headers
25
     * @param array $datas
26
     * @return self
27
     */
28 2
    protected function apiRelayRequest(
29
        string $method,
30
        string $url,
31
        array $headers = [],
32
        $datas = []
33
    ): self {
34 2
        $cha = curl_init();
35 2
        if (false !== $cha) {
36 2
            curl_setopt($cha, CURLOPT_VERBOSE, false);
37 2
            curl_setopt($cha, CURLOPT_URL, $url);
38 2
            curl_setopt($cha, CURLOPT_POST, ($method == Request::METHOD_POST));
39 2
            curl_setopt($cha, CURLOPT_TIMEOUT, 300);
40 2
            curl_setopt($cha, CURLOPT_USERAGENT, IApi::USER_AGENT);
41 2
            curl_setopt($cha, CURLOPT_BUFFERSIZE, IApi::BUFFER_SIZE);
42 2
            curl_setopt($cha, CURLOPT_HTTPHEADER, $headers);
43 2
            if ($this->apiRelayOptionHeader) {
44
                curl_setopt($cha, CURLOPT_VERBOSE, 1);
45
                curl_setopt($cha, CURLOPT_HEADER, 1);
46
            }
47 2
            $this->apiRelayRequestSetPostData($cha, $method, $datas);
48 2
            curl_setopt($cha, CURLOPT_RETURNTRANSFER, 1);
49 2
            $result = curl_exec($cha);
50 2
            $error = (false === $result);
51 2
            $this->apiRelayResponse = ($error) ? 'Api relay error' : $result;
52 2
            $curlInfoCode = ($error) ? 500 : curl_getinfo($cha, CURLINFO_HTTP_CODE);
53 2
            $this->apiRelayHttpCode = (false === $curlInfoCode) ? 500 : $curlInfoCode;
54 2
            if ($this->apiRelayOptionHeader && false === $error) {
55
                $this->apiRelayHeaders = [];
56
                $headerSize = curl_getinfo($cha, CURLINFO_HEADER_SIZE);
57
                $rawHeaders = substr($this->apiRelayResponse, 0, $headerSize);
0 ignored issues
show
Bug introduced by
It seems like $this->apiRelayResponse can also be of type true; however, parameter $string of substr() does only seem to accept string, 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

57
                $rawHeaders = substr(/** @scrutinizer ignore-type */ $this->apiRelayResponse, 0, $headerSize);
Loading history...
58
                $this->apiRelayHeaders = explode("\r\n\r\n", $rawHeaders, 2);
59
                $this->apiRelayResponse = substr(
60
                    $this->apiRelayResponse,
61
                    $headerSize
62
                );
63
            }
64 2
            if (false === $error) {
65 2
                curl_close($cha);
66
            }
67
        }
68 2
        return $this;
69
    }
70
71
    /**
72
     * patch curl instance to provide posted datas if required
73
     *
74
     * @param mixed $cha
75
     * @param string $method
76
     * @param array $datas
77
     * @return void
78
     */
79
    protected function apiRelayRequestSetPostData(&$cha, string $method, array $datas)
80
    {
81
        if (is_resource($cha)) {
82
            if (Request::METHOD_POST == $method && !empty($datas)) {
83
                curl_setopt(
84
                    $cha,
85
                    CURLOPT_POSTFIELDS,
86
                    http_build_query($datas)
87
                );
88
            }
89
        }
90
    }
91
}
92