Passed
Pull Request — master (#39)
by Jan
08:36
created

InitPaymentRequest::__construct()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 49
rs 8.8977
c 1
b 0
f 0
ccs 27
cts 27
cp 1
cc 6
nc 16
nop 15
crap 6

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

216
			/** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
Loading history...
217
			ResultCode::get($data['resultCode']),
218
			$data['resultMessage'],
219
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null,
220
			$data['authCode'] ?? null,
221
			null,
222
			$data['customerCode'] ?? null
223
		);
224
	}
225
226
}
227