PluginProbe
MultiSafepay plugin for WooCommerce / trunk
MultiSafepay plugin for WooCommerce vtrunk
6.11.1 6.12.0 6.13.0 6.2.0 6.2.1 6.3.0 6.3.1 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.6.0 6.6.1 6.6.2 6.7.0 6.7.1 6.7.2 6.7.3 6.8.0 6.8.1 6.8.2 6.8.3 6.9.0 All 84 releases
multisafepay / src / Client / MultiSafepayClient.php

MultiSafepayClient.php in MultiSafepay plugin for WooCommerce trunk, at src/Client/MultiSafepayClient.php

76 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace MultiSafepay\WooCommerce\Client;
4
5 use Exception;
6 use MultiSafepay\WooCommerce\Utils\Logger;
7 use Nyholm\Psr7\Response;
8 use Psr\Http\Client\ClientInterface;
9 use Psr\Http\Message\RequestInterface;
10 use Psr\Http\Message\ResponseInterface;
11
12 /**
13 * Class MultiSafepayClient
14 */
15 class MultiSafepayClient implements ClientInterface {
16
17 /**
18 * @var Logger
19 */
20 private $logger;
21
22 /**
23 * @param Logger|null $logger
24 */
25 public function __construct( ?Logger $logger = null ) {
26 $this->logger = $logger ?? new Logger();
27 }
28
29 /**
30 * Sends a request using wp_remote_request for the given PSR-7 request (RequestInterface)
31 * and returns a PSR-7 response (ResponseInterface).
32 *
33 * @param RequestInterface $request
34 * @return ResponseInterface
35 * @throws Exception
36 */
37 public function sendRequest( RequestInterface $request ): ResponseInterface {
38 $request->getBody()->rewind();
39 $args = $this->get_headers_from_request_interface( $request );
40
41 try {
42 $response_data = wp_remote_request( $request->getUri()->__toString(), $args );
43 if ( is_wp_error( $response_data ) ) {
44 throw new Exception( $response_data->get_error_message() );
45 }
46 } catch ( Exception $exception ) {
47 $this->logger->log_error( 'Error when process request via MultiSafepayClient: ' . $exception->getMessage() );
48 throw new Exception( $exception->getMessage() );
49 }
50
51 $body = wp_remote_retrieve_body( $response_data );
52 $response = new Response( $response_data['response']['code'], $response_data['headers']->getAll(), $body, '1.1', null );
53 $response->getBody()->rewind();
54 return $response;
55 }
56
57 /**
58 * Return an array of headers to be used in wp_remote_request
59 *
60 * @param RequestInterface $request
61 * @return array
62 */
63 private function get_headers_from_request_interface( RequestInterface $request ): array {
64 $args = array(
65 'method' => $request->getMethod(),
66 'body' => $request->getBody()->getContents(),
67 'httpversion' => $request->getProtocolVersion(),
68 'timeout' => 30,
69 );
70 foreach ( $request->getHeaders() as $name => $value ) {
71 $args['headers'][ $name ] = $value[0];
72 }
73 return $args;
74 }
75 }
76