From 35909939254af1f7a44a972baebf92907eb99d3e Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 1 Mar 2024 16:48:01 +0000 Subject: [PATCH] - Added a process to native handler --- src/NativeHandler.php | 19 +++++++++++++++- src/Transport/Curl.php | 42 ++++++++++++++++++++++++++++------- src/Util/ExceptionHandler.php | 2 +- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/NativeHandler.php b/src/NativeHandler.php index 85ca5e2..33847ee 100644 --- a/src/NativeHandler.php +++ b/src/NativeHandler.php @@ -8,5 +8,22 @@ class NativeHandler extends Handler { { return $this->exceptionHandler->extractExceptionData($ex, $_SERVER, $_POST); } - + + public function process(callable $callback) : mixed + { + try { + return $callback(); + } + catch (\Throwable $ex) + { + $this->pushData($ex); + + if ( $this->callback ?? false ) { + return call_user_func_array($this->callback, [ $ex ] ); + } + else { + throw $ex; + } + } + } } diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index c21e1de..dd6d250 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -4,9 +4,11 @@ namespace Negundo\Client\Transport; class Curl implements TransportInterface { - public $timeout = 1; + public $timeout = 2; - public $throwErrors = false; + public $throwErrors = true; + + public $verifySsl = false; public $headers = []; @@ -20,22 +22,46 @@ class Curl implements TransportInterface { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&')); curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifySsl); $exec = curl_exec($ch); if ( ( false === $exec ) && $this->throwErrors ) { $errno = curl_errno($ch); - $errors = array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ]); - - throw new CurlException(implode(PHP_EOL, $errors), $errno); + curl_close($ch); + + throw new CurlException(implode(PHP_EOL, array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ])), $errno); } - + curl_close($ch); return $exec; } - + + public function get(string $url) : ? object + { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers ); + curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifySsl); + $execute = curl_exec($ch); + + if ( ( false === $execute ) && $this->throwErrors ) { + $errno = curl_errno($ch); + + curl_close($ch); + throw new CurlException(implode(PHP_EOL, array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ])), $errno); + } + else { + curl_close($ch); + } + + return $execute ? (object) $execute : null; + } + const CURL_ERROR = [ 0 => 'CURLE_OK', 1 => 'CURLE_UNSUPPORTED_PROTOCOL', diff --git a/src/Util/ExceptionHandler.php b/src/Util/ExceptionHandler.php index 74539a6..3c0ffb0 100644 --- a/src/Util/ExceptionHandler.php +++ b/src/Util/ExceptionHandler.php @@ -22,7 +22,7 @@ class ExceptionHandler { 'HTTP_USER_AGENT' => $serverData['HTTP_USER_AGENT'] ?? null, 'REMOTE_ADDR' => $serverData['REMOTE_ADDR'] ?? null, ]; - + $post = [ 'code' => $ex->getCode(), 'file' => $ex->getFile(),