stadem /
viva-payments
| 1 | <?php |
||||
| 2 | namespace Stadem\VivaPayments\Services; |
||||
| 3 | use Stadem\VivaPayments\Traits\getConfigSettings; |
||||
| 4 | |||||
| 5 | class CurlWrapper { |
||||
| 6 | |||||
| 7 | use getConfigSettings; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 8 | |||||
| 9 | private $ch; |
||||
| 10 | private $url; |
||||
| 11 | private $headers = array(); |
||||
| 12 | private $statusCode; |
||||
| 13 | |||||
| 14 | public function __construct($url) { |
||||
| 15 | $this->url = $url; |
||||
| 16 | $this->ch = curl_init($this->url); |
||||
| 17 | curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); |
||||
| 18 | } |
||||
| 19 | |||||
| 20 | public function addHeader($header) { |
||||
| 21 | $this->headers[] = $header; |
||||
| 22 | } |
||||
| 23 | |||||
| 24 | public function get($params = array()) { |
||||
|
0 ignored issues
–
show
The parameter
$params is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 25 | $this->setOption(CURLOPT_HTTPGET, true); |
||||
| 26 | return $this->execute(); |
||||
| 27 | } |
||||
| 28 | |||||
| 29 | public function post($params = array()) { |
||||
| 30 | $this->setOption(CURLOPT_POST, true); |
||||
| 31 | $this->setOption(CURLOPT_POSTFIELDS, http_build_query($params)); |
||||
| 32 | return $this->execute(); |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | public function postRaw($params) { |
||||
| 36 | $this->setOption(CURLOPT_POST, true); |
||||
| 37 | $this->setOption(CURLOPT_POSTFIELDS, $params); |
||||
| 38 | // $this->addHeader('Content-Type: application/json'); |
||||
| 39 | // $this->addHeader('User-Agent: PostmanRuntime/0.0.1'); |
||||
| 40 | return $this->execute(); |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | public function put($params = array()) { |
||||
| 44 | $this->setOption(CURLOPT_CUSTOMREQUEST, 'PUT'); |
||||
| 45 | $this->setOption(CURLOPT_POSTFIELDS, http_build_query($params)); |
||||
| 46 | return $this->execute(); |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | public function delete($params = array()) { |
||||
| 50 | $this->setOption(CURLOPT_CUSTOMREQUEST, 'DELETE'); |
||||
| 51 | $this->setOption(CURLOPT_POSTFIELDS, http_build_query($params)); |
||||
| 52 | return $this->execute(); |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | public function setBasicAuth($username, $password) { |
||||
| 56 | $auth_string = base64_encode("$username:$password"); |
||||
| 57 | $this->addHeader("Authorization: Basic $auth_string"); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | public function setBearer($token) { |
||||
| 61 | $this->addHeader("Authorization: Bearer $token"); |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | private function setOption($option, $value) { |
||||
| 65 | curl_setopt($this->ch, $option, $value); |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | private function execute() { |
||||
| 69 | |||||
| 70 | //For debuging |
||||
| 71 | if ($this->getCurlDebugIsEnable()){ |
||||
| 72 | $parentDir = dirname(dirname(dirname(__FILE__))); |
||||
| 73 | $fp = fopen($parentDir.'/debug_viva_curl.txt', 'w'); |
||||
| 74 | curl_setopt($this->ch, CURLOPT_VERBOSE, 1); |
||||
| 75 | curl_setopt($this->ch, CURLOPT_STDERR, $fp); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers); |
||||
| 79 | |||||
| 80 | $response = curl_exec($this->ch); |
||||
| 81 | |||||
| 82 | //For debuging |
||||
| 83 | if ($this->getCurlDebugIsEnable()){ |
||||
| 84 | $method = __METHOD__; |
||||
| 85 | $parentDir = dirname(dirname(dirname(__FILE__))); |
||||
| 86 | $fp = fopen($parentDir . '/debug_viva_curl_response.txt', 'a+'); |
||||
| 87 | fwrite($fp, '------- '. $method.' -------'.PHP_EOL. $response.PHP_EOL.'------------------------------'.PHP_EOL); |
||||
|
0 ignored issues
–
show
Are you sure
$response of type string|true can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 88 | fclose($fp); |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | if ($response === false) { |
||||
| 92 | $errorCode = curl_errno($this->ch); |
||||
| 93 | $errorMsg = curl_error($this->ch); |
||||
| 94 | |||||
| 95 | |||||
| 96 | |||||
| 97 | |||||
| 98 | throw new \Exception('Curl error: ' . $errorMsg.' '. $errorCode); |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | $data = json_decode($response, true); |
||||
|
0 ignored issues
–
show
It seems like
$response can also be of type true; however, parameter $json of json_decode() 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
Loading history...
|
|||||
| 102 | $this->statusCode = $data[0]['status'] ?? curl_getinfo($this->ch, CURLINFO_HTTP_CODE); |
||||
| 103 | return $response; |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | public function getStatusCode():int { |
||||
| 107 | return $this->statusCode; |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | public function close() { |
||||
| 111 | curl_close($this->ch); |
||||
| 112 | } |
||||
| 113 | } |