Completed
Pull Request — master (#16)
by Josef
05:49
created

InitPaymentRequest::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 47
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 40
nc 8
nop 15
dl 0
loc 47
rs 8.6845
c 2
b 0
f 0

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