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

InitPaymentRequest::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 24
c 1
b 0
f 0
nc 8
nop 15
dl 0
loc 46
rs 9.536
ccs 25
cts 25
cp 1
crap 4

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 1
	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 1
		Validator::checkOrderId($orderId);
83 1
		Validator::checkReturnUrl($returnUrl);
84 1
		Validator::checkDescription($description);
85 1
		if ($merchantData !== null) {
86 1
			Validator::checkMerchantData($merchantData);
87
		}
88 1
		if ($customerId !== null) {
89 1
			Validator::checkCustomerId($customerId);
90
		}
91 1
		if ($ttlSec !== null) {
92 1
			Validator::checkTtlSec($ttlSec);
93
		}
94
95 1
		$this->merchantId = $merchantId;
96 1
		$this->orderId = $orderId;
97 1
		$this->payOperation = $payOperation;
98 1
		$this->payMethod = $payMethod;
99 1
		$this->closePayment = $closePayment;
100 1
		$this->returnUrl = $returnUrl;
101 1
		$this->returnMethod = $returnMethod;
102 1
		$this->cart = $cart;
103 1
		$this->description = $description;
104 1
		$this->merchantData = $merchantData;
105 1
		$this->customerId = $customerId;
106 1
		$this->language = $language;
107 1
		$this->ttlSec = $ttlSec;
108 1
		$this->logoVersion = $logoVersion;
109 1
		$this->colorSchemeVersion = $colorSchemeVersion;
110 1
	}
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
			$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