Test Failed
Push — main ( e0d6a9...e106fe )
by Reüel
07:26 queued 21s
created

Client   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 27.27%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 6
cts 22
cp 0.2727
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A send_request() 0 35 2
1
<?php
2
/**
3
 * Payvision client
4
 *
5
 * @author Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license GPL-3.0-or-later
8
 * @package Pronamic\WordPress\Pay\Gateways\Payvision
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Payvision;
12
13
use Pronamic\WordPress\Pay\Facades\Http;
14
15
/**
16
 * Payvision client
17
 *
18
 * @link https://github.com/payvisionpayments/php/blob/master/generatepaymentform.php
19
 * @author Remco Tolsma
20
 * @version 1.1.0
21
 * @since 1.0.0
22
 */
23
class Client {
24
	/**
25
	 * Config.
26
	 *
27
	 * @var Config
28
	 */
29
	private $config;
30
31
	/**
32
	 * Constructs and initializes an Payvision client object.
33
	 *
34
	 * @param Config $config Payvision config.
35
	 */
36 7
	public function __construct( Config $config ) {
37 7
		$this->config = $config;
38 7
	}
39
40
	/**
41
	 * Send request with the specified action and parameters
42
	 *
43
	 * @param string                       $method  Payvision API method.
44
	 * @param string                       $path    Path.
45
	 * @param object|string[]|string|false $request Request object.
46
	 * @return object
47
	 * @throws \Exception Throws exception when error occurs.
48
	 */
49 4
	public function send_request( $method, $path, $request = null ) {
50
		// Request.
51 4
		$authorization = 'Basic ' . \base64_encode( $this->config->get_username() . ':' . $this->config->get_password() );
52
53 4
		$response = Http::request(
54
			$this->config->get_endpoint_url( $path ),
55
			array(
56
				'method'  => $method,
57
				'headers' => array(
58
					'Authorization' => $authorization,
59
					'Content-Type'  => 'application/json',
60
				),
61
				'body'    => $request,
62
			)
63
		);
64
65
		$data = $response->json();
66
67
		// Object.
68
		if ( ! \is_object( $data ) ) {
69
			$response_code = $response->status();
70
71
			throw new \Exception(
72
				\sprintf(
73
					'Could not JSON decode Payvision response to an object, HTTP response: "%s %s", HTTP body: "%s".',
74
					$response_code,
75
					$response->message(),
76
					$response->body()
77
				),
78
				\intval( $response_code )
79
			);
80
		}
81
82
		return $data;
83
	}
84
}
85