Passed
Pull Request — master (#38)
by Jan
07:38
created

InitPaymentRequest::send()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 100
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 73
c 1
b 0
f 0
nc 32
nop 1
dl 0
loc 100
ccs 45
cts 45
cp 1
crap 8
rs 7.3446

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\Cart;
9
use SlevomatCsobGateway\CartItem;
10
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
11
use SlevomatCsobGateway\Language;
12
use SlevomatCsobGateway\Validator;
13
use function array_map;
14
use function base64_encode;
15
16
class InitPaymentRequest
17
{
18
19
	/** @var string */
20
	private $merchantId;
21
22
	/** @var string */
23
	private $orderId;
24
25
	/** @var PayOperation */
26
	private $payOperation;
27
28
	/** @var PayMethod */
29
	private $payMethod;
30
31
	/** @var bool */
32
	private $closePayment;
33
34
	/** @var string */
35
	private $returnUrl;
36
37
	/** @var HttpMethod */
38
	private $returnMethod;
39
40
	/** @var Cart */
41
	private $cart;
42
43
	/** @var string */
44
	private $description;
45
46
	/** @var string|null */
47
	private $merchantData;
48
49
	/** @var string|null */
50
	private $customerId;
51
52
	/** @var Language */
53
	private $language;
54
55
	/** @var int|null */
56
	private $ttlSec;
57
58
	/** @var int|null */
59
	private $logoVersion;
60
61
	/** @var int|null */
62
	private $colorSchemeVersion;
63
64 2
	public function __construct(
65
		string $merchantId,
66
		string $orderId,
67
		PayOperation $payOperation,
68
		PayMethod $payMethod,
69
		bool $closePayment,
70
		string $returnUrl,
71
		HttpMethod $returnMethod,
72
		Cart $cart,
73
		string $description,
74
		?string $merchantData,
75
		?string $customerId,
76
		Language $language,
77
		?int $ttlSec = null,
78
		?int $logoVersion = null,
79
		?int $colorSchemeVersion = null
80
	)
81
	{
82 2
		Validator::checkOrderId($orderId);
83 2
		Validator::checkReturnUrl($returnUrl);
84 2
		Validator::checkDescription($description);
85 2
		if ($merchantData !== null) {
86 2
			Validator::checkMerchantData($merchantData);
87
		}
88 2
		if ($customerId !== null) {
89 2
			Validator::checkCustomerId($customerId);
90
		}
91 2
		if ($ttlSec !== null) {
92 2
			Validator::checkTtlSec($ttlSec);
93
		}
94
95 2
		$this->merchantId = $merchantId;
96 2
		$this->orderId = $orderId;
97 2
		$this->payOperation = $payOperation;
98 2
		$this->payMethod = $payMethod;
99 2
		$this->closePayment = $closePayment;
100 2
		$this->returnUrl = $returnUrl;
101 2
		$this->returnMethod = $returnMethod;
102 2
		$this->cart = $cart;
103 2
		$this->description = $description;
104 2
		$this->merchantData = $merchantData;
105 2
		$this->customerId = $customerId;
106 2
		$this->language = $language;
107 2
		$this->ttlSec = $ttlSec;
108 2
		$this->logoVersion = $logoVersion;
109 2
		$this->colorSchemeVersion = $colorSchemeVersion;
110 2
	}
111
112 1
	public function send(ApiClient $apiClient): PaymentResponse
113
	{
114 1
		$price = $this->cart->getCurrentPrice();
115
116
		$requestData = [
117 1
			'merchantId' => $this->merchantId,
118 1
			'orderNo' => $this->orderId,
119 1
			'payOperation' => $this->payOperation->getValue(),
120 1
			'payMethod' => $this->payMethod->getValue(),
121 1
			'totalAmount' => $price->getAmount(),
122 1
			'currency' => $price->getCurrency()->getValue(),
123 1
			'closePayment' => $this->closePayment,
124 1
			'returnUrl' => $this->returnUrl,
125 1
			'returnMethod' => $this->returnMethod->getValue(),
126
			'cart' => array_map(static function (CartItem $cartItem) {
127
				$cartItemValues = [
128 1
					'name' => $cartItem->getName(),
129 1
					'quantity' => $cartItem->getQuantity(),
130 1
					'amount' => $cartItem->getAmount(),
131
				];
132
133 1
				if ($cartItem->getDescription() !== null) {
134 1
					$cartItemValues['description'] = $cartItem->getDescription();
135
				}
136
137 1
				return $cartItemValues;
138 1
			}, $this->cart->getItems()),
139 1
			'description' => $this->description,
140 1
			'language' => $this->language->getValue(),
141
		];
142
143 1
		if ($this->merchantData !== null) {
144 1
			$requestData['merchantData'] = base64_encode($this->merchantData);
145
		}
146
147 1
		if ($this->customerId !== null) {
148 1
			$requestData['customerId'] = $this->customerId;
149
		}
150
151 1
		if ($this->ttlSec !== null) {
152 1
			$requestData['ttlSec'] = $this->ttlSec;
153
		}
154
155 1
		if ($this->logoVersion !== null) {
156 1
			$requestData['logoVersion'] = $this->logoVersion;
157
		}
158
159 1
		if ($this->colorSchemeVersion !== null) {
160 1
			$requestData['colorSchemeVersion'] = $this->colorSchemeVersion;
161
		}
162
163 1
		$response = $apiClient->post(
164 1
			'payment/init',
165 1
			$requestData,
166 1
			new SignatureDataFormatter([
167 1
				'merchantId' => null,
168
				'orderNo' => null,
169
				'dttm' => null,
170
				'payOperation' => null,
171
				'payMethod' => null,
172
				'totalAmount' => null,
173
				'currency' => null,
174
				'closePayment' => null,
175
				'returnUrl' => null,
176
				'returnMethod' => null,
177
				'cart' => [
178
					[
179
						'name' => null,
180
						'quantity' => null,
181
						'amount' => null,
182
						'description' => null,
183
					],
184
				],
185
				'description' => null,
186
				'merchantData' => null,
187
				'customerId' => null,
188
				'language' => null,
189
				'ttlSec' => null,
190
				'logoVersion' => null,
191
				'colorSchemeVersion' => null,
192
			]),
193 1
			new SignatureDataFormatter([
194 1
				'payId' => null,
195
				'dttm' => null,
196
				'resultCode' => null,
197
				'resultMessage' => null,
198
				'paymentStatus' => null,
199
				'authCode' => null,
200
			])
201
		);
202
203 1
		$data = $response->getData();
204
205 1
		return new PaymentResponse(
206 1
			$data['payId'],
207 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

207
			/** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
Loading history...
208 1
			ResultCode::get($data['resultCode']),
209 1
			$data['resultMessage'],
210 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null,
211 1
			$data['authCode'] ?? null
212
		);
213
	}
214
215
}
216