Completed
Push — master ( 93c150...38adbe )
by Ibrahim
03:41
created

Request   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 16.66%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 34
lcom 1
cbo 7
dl 0
loc 137
ccs 16
cts 96
cp 0.1666
rs 9.2
c 1
b 1
f 0

7 Methods

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