Completed
Push — master ( a70f1d...75b407 )
by Ibrahim
06:01
created

Request   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 17.02%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 33
lcom 1
cbo 6
dl 0
loc 134
ccs 16
cts 94
cp 0.1702
rs 9.3999
c 1
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A flattenedHeaders() 0 8 2
A send() 0 11 3
D attemptGuzzle() 0 41 17
A attemptFileGetContents() 0 19 2
B attemptCurl() 0 26 6
A getResponse() 0 4 1
1
<?php
2
3
namespace Yabacon\Paystack\Http;
4
5
use \Yabacon\Paystack\Contracts\RouteInterface;
6
7
class Request
8
{
9
    public $method;
10
    public $endpoint;
11
    public $body = '';
12
    public $headers = [];
13
    protected $response;
14
    protected $paystackObj;
15
16 9
    public function __construct($paystackObj = null)
17
    {
18 9
        $this->response = new Response();
19 9
        $this->paystackObj = $paystackObj;
20 9
        $this->response->forApi = !is_null($paystackObj);
21 9
        if ($this->response->forApi) {
22 5
            $this->headers['Content-Type'] = 'application/json';
23 5
        }
24 9
    }
25
26 1
    public function getResponse()
27
    {
28 1
        return $this->response;
29
    }
30
31 1
    public function flattenedHeaders()
32
    {
33 1
        $_ = [];
34 1
        foreach ($this->headers as $key => $value) {
35 1
            $_[] = $key . ": " . $value;
36 1
        }
37 1
        return $_;
38
    }
39
40
    public function send()
41
    {
42
        $this->attemptGuzzle();
43
        if (!$this->response->okay) {
44
            $this->attemptCurl();
45
        }
46
        if (!$this->response->okay) {
47
            $this->attemptFileGetContents();
48
        }
49
        return $this->response;
50
    }
51
52
    public function attemptGuzzle()
53
    {
54
        if (isset($this->paystackObj) && !$this->paystackObj->use_guzzle) {
55
            $this->response->okay = false;
56
            return;
57
        }
58
        if (class_exists('\\GuzzleHttp\\Exception\\BadResponseException')
59
            && class_exists('\\GuzzleHttp\\Exception\\ClientException')
60
            && class_exists('\\GuzzleHttp\\Exception\\ConnectException')
61
            && class_exists('\\GuzzleHttp\\Exception\\RequestException')
62
            && class_exists('\\GuzzleHttp\\Exception\\ServerException')
63
            && class_exists('\\GuzzleHttp\\Client')
64
            && class_exists('\\GuzzleHttp\\Psr7\\Request')
65
        ) {
66
            $request = new \GuzzleHttp\Psr7\Request(
67
                strtoupper($this->method),
68
                $this->endpoint,
69
                $this->headers,
70
                $this->body
71
            );
72
            $client = new \GuzzleHttp\Client();
73
            try {
74
                $psr7response = $client->send($request);
75
                $this->response->body = $psr7response->getBody()->getContents();
76
                $this->response->okay = true;
77
            } catch (\Exception $e) {
78
                if (($e instanceof \GuzzleHttp\Exception\BadResponseException
79
                    || $e instanceof \GuzzleHttp\Exception\ClientException
80
                    || $e instanceof \GuzzleHttp\Exception\ConnectException
81
                    || $e instanceof \GuzzleHttp\Exception\RequestException
82
                    || $e instanceof \GuzzleHttp\Exception\ServerException)
83
                ) {
84
                    if ($e->hasResponse()) {
85
                        $this->response->body = $e->getResponse()->getBody()->getContents();
86
                    }
87
                    $this->response->okay = true;
88
                }
89
                $this->response->messages[] = $e->getMessage();
90
            }
91
        }
92
    }
93
94
    public function attemptFileGetContents()
95
    {
96
        $context = stream_context_create(
97
            [
98
                'http'=>array(
99
                  'method'=>$this->method,
100
                  'header'=>$this->flattenedHeaders(),
101
                  'content'=>$this->body,
102
                  'ignore_errors' => true
103
                )
104
            ]
105
        );
106
        $this->response->body = file_get_contents($this->endpoint, false, $context);
107
        if ($this->response->body === false) {
108
            $this->response->messages[] = 'file_get_contents failed with response: \'' . error_get_last() . '\'.';
109
        } else {
110
            $this->response->okay = true;
111
        }
112
    }
113
114
    public function attemptCurl()
115
    {
116
        //open connection
117
        $ch = \curl_init();
118
        \curl_setopt($ch, \CURLOPT_URL, $this->endpoint);
119
        ($this->method === RouteInterface::POST_METHOD) && \curl_setopt($ch, \CURLOPT_POST, true);
120
        ($this->method === RouteInterface::PUT_METHOD) && \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
121
122
        if ($this->method !== RouteInterface::GET_METHOD) {
123
            \curl_setopt($ch, \CURLOPT_POSTFIELDS, $this->body);
124
        }
125
        \curl_setopt($ch, \CURLOPT_HTTPHEADER, $this->flattenedHeaders());
126
        \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
127
        $this->response->forApi && \curl_setopt($ch, \CURLOPT_SSLVERSION, 6);
128
129
        $this->response->body = \curl_exec($ch);
130
131
        if (\curl_errno($ch)) {
132
            $cerr = \curl_error($ch);
133
            $this->response->messages[] = 'Curl failed with response: \'' . $cerr . '\'.';
134
        } else {
135
            $this->response->okay = true;
136
        }
137
138
        \curl_close($ch);
139
    }
140
}
141