Completed
Push — master ( 7d5f81...069e15 )
by Jan
02:05
created

PaymentButtonRequest::send()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 53
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 28
cts 28
cp 1
rs 9.5797
c 0
b 0
f 0
cc 3
eloc 40
nc 2
nop 1
crap 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway\Call;
4
5
use DateTimeImmutable;
6
use SlevomatCsobGateway\Api\ApiClient;
7
use SlevomatCsobGateway\Api\HttpMethod;
8
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
9
use SlevomatCsobGateway\Validator;
10
11
class PaymentButtonRequest
12
{
13
14
	/**
15
	 * @var string
16
	 */
17
	private $merchantId;
18
19
	/**
20
	 * @var string
21
	 */
22
	private $payId;
23
24
	/** @var \SlevomatCsobGateway\Call\PaymentButtonBrand */
25
	private $brand;
26
27 2
	public function __construct(
28
		string $merchantId,
29
		string $payId,
30
		PaymentButtonBrand $brand
31
	)
32
	{
33 2
		Validator::checkPayId($payId);
34
35 2
		$this->merchantId = $merchantId;
36 2
		$this->payId = $payId;
37 2
		$this->brand = $brand;
38 2
	}
39
40 1
	public function send(ApiClient $apiClient): PaymentButtonResponse
41
	{
42
		$requestData = [
43 1
			'merchantId' => $this->merchantId,
44 1
			'payId' => $this->payId,
45 1
			'brand' => $this->brand->getValue(),
46
		];
47
48 1
		$response = $apiClient->post(
49 1
			'payment/button',
50 1
			$requestData,
51 1
			new SignatureDataFormatter([
52 1
				'merchantId' => null,
53
				'payId' => null,
54
				'brand' => null,
55
				'dttm' => null,
56
			]),
57 1
			new SignatureDataFormatter([
58 1
				'payId' => null,
59
				'dttm' => null,
60
				'resultCode' => null,
61
				'resultMessage' => null,
62
				'paymentStatus' => null,
63
				'redirect' => [
64
					'method' => null,
65
					'url' => null,
66
					'params' => null,
67
				],
68
			])
69
		);
70
71 1
		$data = $response->getData();
72
73 1
		$redirectUrl = null;
74 1
		$redirectMethod = null;
75 1
		$redirectParams = [];
76 1
		if (isset($data['redirect'])) {
77 1
			$redirectUrl = $data['redirect']['url'];
78 1
			$redirectMethod = HttpMethod::get($data['redirect']['method']);
79 1
			$redirectParams = $data['redirect']['params'] ?? null;
80
		}
81
82 1
		return new PaymentButtonResponse(
83 1
			$data['payId'],
84 1
			DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
85 1
			ResultCode::get($data['resultCode']),
86 1
			$data['resultMessage'],
87 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null,
88 1
			$redirectMethod,
89 1
			$redirectUrl,
90 1
			$redirectParams
91
		);
92
	}
93
94
}
95