PaymentButtonRequest::send()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 68
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 4.0004

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 68
ccs 31
cts 32
cp 0.9688
rs 9.0036
c 1
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0004

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\Button;
4
5
use DateTimeImmutable;
6
use SlevomatCsobGateway\Api\ApiClient;
7
use SlevomatCsobGateway\Api\HttpMethod;
8
use SlevomatCsobGateway\Call\PaymentStatus;
9
use SlevomatCsobGateway\Call\ResultCode;
10
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
11
use SlevomatCsobGateway\Language;
12
use SlevomatCsobGateway\Price;
13
use SlevomatCsobGateway\Validator;
14
15
class PaymentButtonRequest
16
{
17
18
	/** @var string */
19
	private $merchantId;
20
21
	/** @var string */
22
	private $orderId;
23
24
	/** @var string */
25
	private $clientIp;
26
27
	/** @var Price */
28
	private $totalPrice;
29
30
	/** @var string */
31
	private $returnUrl;
32
33
	/** @var HttpMethod */
34
	private $returnMethod;
35
36
	/** @var PaymentButtonBrand */
37
	private $brand;
38
39
	/** @var string|null */
40
	private $merchantData;
41
42
	/** @var Language */
43
	private $language;
44
45 2
	public function __construct(
46
		string $merchantId,
47
		string $orderId,
48
		string $clientIp,
49
		Price $totalPrice,
50
		string $returnUrl,
51
		HttpMethod $returnMethod,
52
		PaymentButtonBrand $brand,
53
		?string $merchantData,
54
		Language $language
55
	)
56
	{
57 2
		Validator::checkReturnUrl($returnUrl);
58 2
		if ($merchantData !== null) {
59
			Validator::checkMerchantData($merchantData);
60
		}
61
62 2
		$this->merchantId = $merchantId;
63 2
		$this->orderId = $orderId;
64 2
		$this->clientIp = $clientIp;
65 2
		$this->totalPrice = $totalPrice;
66 2
		$this->returnUrl = $returnUrl;
67 2
		$this->returnMethod = $returnMethod;
68 2
		$this->brand = $brand;
69 2
		$this->merchantData = $merchantData;
70 2
		$this->language = $language;
71 2
	}
72
73 1
	public function send(ApiClient $apiClient): PaymentButtonResponse
74
	{
75
		$requestData = [
76 1
			'merchantId' => $this->merchantId,
77 1
			'orderNo' => $this->orderId,
78 1
			'clientIp' => $this->clientIp,
79 1
			'totalAmount' => $this->totalPrice->getAmount(),
80 1
			'currency' => $this->totalPrice->getCurrency()->getValue(),
81 1
			'returnUrl' => $this->returnUrl,
82 1
			'returnMethod' => $this->returnMethod->getValue(),
83 1
			'brand' => $this->brand->getValue(),
84 1
			'language' => $this->language->getValue(),
85
		];
86
87 1
		if ($this->merchantData !== null) {
88
			$requestData['merchantData'] = $this->merchantData;
89
		}
90
91 1
		$response = $apiClient->post(
92 1
			'button/init',
93
			$requestData,
94 1
			new SignatureDataFormatter([
95 1
				'merchantId' => null,
96
				'orderNo' => null,
97
				'dttm' => null,
98
				'clientIp' => null,
99
				'totalAmount' => null,
100
				'currency' => null,
101
				'returnUrl' => null,
102
				'returnMethod' => null,
103
				'brand' => null,
104
				'merchantData' => null,
105
				'language' => null,
106
			]),
107 1
			new SignatureDataFormatter([
108 1
				'payId' => null,
109
				'dttm' => null,
110
				'resultCode' => null,
111
				'resultMessage' => null,
112
				'paymentStatus' => null,
113
				'redirect' => [
114
					'method' => null,
115
					'url' => null,
116
					'params' => null,
117
				],
118
			])
119
		);
120
121 1
		$data = $response->getData();
122
123 1
		$redirectUrl = null;
124 1
		$redirectMethod = null;
125 1
		$redirectParams = [];
126 1
		if (isset($data['redirect'])) {
127 1
			$redirectUrl = $data['redirect']['url'];
128 1
			$redirectMethod = HttpMethod::get($data['redirect']['method']);
129 1
			$redirectParams = $data['redirect']['params'] ?? null;
130
		}
131
132 1
		return new PaymentButtonResponse(
133 1
			$data['payId'],
134 1
			DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
0 ignored issues
show
Bug introduced by
It seems like DateTimeImmutable::creat...YmdHis', $data['dttm']) can also be of type false; however, parameter $responseDateTime of SlevomatCsobGateway\Call...Response::__construct() does only seem to accept DateTimeImmutable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
			/** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
Loading history...
135 1
			ResultCode::get($data['resultCode']),
136 1
			$data['resultMessage'],
137 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null,
138
			$redirectMethod,
139
			$redirectUrl,
140
			$redirectParams
141
		);
142
	}
143
144
}
145