Failed Conditions
Push — main ( e106fe...b61967 )
by Remco
09:42 queued 16s
created

Client   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

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\Http\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.2
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 9
	public function __construct( Config $config ) {
37 9
		$this->config = $config;
38 9
	}
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 6
	public function send_request( $method, $path, $request = null ) {
50
		// Request.
51 6
		$authorization = 'Basic ' . \base64_encode( $this->config->get_username() . ':' . $this->config->get_password() );
52
53 6
		$response = Http::request(
54 6
			$this->config->get_endpoint_url( $path ),
55
			array(
56 6
				'method'  => $method,
57
				'headers' => array(
58 6
					'Authorization' => $authorization,
59 6
					'Content-Type'  => 'application/json',
60
				),
61 6
				'body'    => $request,
62
			)
63
		);
64
65 5
		$data = $response->json();
66
67
		// Object.
68 3
		if ( ! \is_object( $data ) ) {
69 1
			$response_code = $response->status();
70
71 1
			throw new \Exception(
72 1
				\sprintf(
73 1
					'Could not JSON decode Payvision response to an object, HTTP response: "%s %s", HTTP body: "%s".',
74 1
					$response_code,
75 1
					$response->message(),
76 1
					$response->body()
77
				),
78 1
				\intval( $response_code )
79
			);
80
		}
81
82 2
		return $data;
83
	}
84
}
85