Completed
Pull Request — master (#16)
by Josef
02:58
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
		$requestData = [
143
			'merchantId' => $this->merchantId,
144
			'orderNo' => $this->orderId,
145
			'payOperation' => $this->payOperation->getValue(),
146
			'payMethod' => $this->payMethod->getValue(),
147
			'totalAmount' => $this->cart->countTotalAmount(),
148
			'currency' => $this->cart->getCurrency()->getValue(),
149
			'closePayment' => $this->closePayment,
150
			'returnUrl' => $this->returnUrl,
151
			'returnMethod' => $this->returnMethod->getValue(),
152
			'cart' => array_map(function (CartItem $cartItem) {
153
				$cartItemValues = [
154
					'name' => $cartItem->getName(),
155
					'quantity' => $cartItem->getQuantity(),
156
					'amount' => $cartItem->getAmount(),
157
				];
158
159
				if ($cartItem->getDescription() !== null) {
160
					$cartItemValues['description'] = $cartItem->getDescription();
161
				}
162
163
				return $cartItemValues;
164
165
			}, $this->cart->getItems()),
166
			'description' => $this->description,
167
			'language' => $this->language->getValue(),
168
		];
169
170
		if ($this->merchantData !== null) {
171
			$requestData['merchantData'] = base64_encode($this->merchantData);
172
		}
173
174
		if ($this->customerId !== null) {
175
			$requestData['customerId'] = $this->customerId;
176
		}
177
178
		if ($this->ttlSec !== null) {
179
			$requestData['ttlSec'] = $this->ttlSec;
180
		}
181
182
		if ($this->logoVersion !== null) {
183
			$requestData['logoVersion'] = $this->logoVersion;
184
		}
185
186
		if ($this->colorSchemeVersion !== null) {
187
			$requestData['colorSchemeVersion'] = $this->colorSchemeVersion;
188
		}
189
190
		$response = $apiClient->post(
191
			'payment/init',
192
			$requestData,
193
			new SignatureDataFormatter([
194
				'merchantId' => null,
195
				'orderNo' => null,
196
				'dttm' => null,
197
				'payOperation' => null,
198
				'payMethod' => null,
199
				'totalAmount' => null,
200
				'currency' => null,
201
				'closePayment' => null,
202
				'returnUrl' => null,
203
				'returnMethod' => null,
204
				'cart' => [
205
					'name' => null,
206
					'quantity' => null,
207
					'amount' => null,
208
					'description' => null,
209
				],
210
				'description' => null,
211
				'merchantData' => null,
212
				'customerId' => null,
213
				'language' => null,
214
				'ttlSec' => null,
215
				'logoVersion' => null,
216
				'colorSchemeVersion' => null,
217
			]),
218
			new SignatureDataFormatter([
219
				'payId' => null,
220
				'dttm' => null,
221
				'resultCode' => null,
222
				'resultMessage' => null,
223
				'paymentStatus' => null,
224
				'authCode' => null,
225
			])
226
		);
227
228
		$data = $response->getData();
229
230
		return new PaymentResponse(
231
			$data['payId'],
232
			DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
233
			new ResultCode($data['resultCode']),
234
			$data['resultMessage'],
235
			isset($data['paymentStatus']) ? new PaymentStatus($data['paymentStatus']) : null,
236
			$data['authCode'] ?? null
237
		);
238
	}
239
240
}
241