Completed
Pull Request — master (#16)
by Josef
03:30
created

InitPaymentRequest::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 47
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 47
ccs 26
cts 26
cp 1
rs 8.6845
c 2
b 0
f 0
cc 4
eloc 40
nc 8
nop 15
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
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 2
	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 2
		Validator::checkOrderId($orderId);
111 2
		Validator::checkReturnUrl($returnUrl);
112 2
		Validator::checkDescription($description);
113 2
		if ($merchantData !== null) {
114 2
			Validator::checkMerchantData($merchantData);
115
		}
116 2
		if ($customerId !== null) {
117 2
			Validator::checkCustomerId($customerId);
118
		}
119 2
		if ($ttlSec !== null) {
120 2
			Validator::checkTtlSec($ttlSec);
121
		}
122
123 2
		$this->merchantId = $merchantId;
124 2
		$this->orderId = $orderId;
125 2
		$this->payOperation = $payOperation;
126 2
		$this->payMethod = $payMethod;
127 2
		$this->closePayment = $closePayment;
128 2
		$this->returnUrl = $returnUrl;
129 2
		$this->returnMethod = $returnMethod;
130 2
		$this->cart = $cart;
131 2
		$this->description = $description;
132 2
		$this->merchantData = $merchantData;
133 2
		$this->customerId = $customerId;
134 2
		$this->language = $language;
135 2
		$this->ttlSec = $ttlSec;
136 2
		$this->logoVersion = $logoVersion;
137 2
		$this->colorSchemeVersion = $colorSchemeVersion;
138 2
	}
139
140 1
	public function send(ApiClient $apiClient): PaymentResponse
141
	{
142 1
		$price = $this->cart->getCurrentPrice();
143
144
		$requestData = [
145 1
			'merchantId' => $this->merchantId,
146 1
			'orderNo' => $this->orderId,
147 1
			'payOperation' => $this->payOperation->getValue(),
148 1
			'payMethod' => $this->payMethod->getValue(),
149 1
			'totalAmount' => $price->getAmount(),
150 1
			'currency' => $price->getCurrency()->getValue(),
151 1
			'closePayment' => $this->closePayment,
152 1
			'returnUrl' => $this->returnUrl,
153 1
			'returnMethod' => $this->returnMethod->getValue(),
154 1
			'cart' => array_map(function (CartItem $cartItem) {
155
				$cartItemValues = [
156 1
					'name' => $cartItem->getName(),
157 1
					'quantity' => $cartItem->getQuantity(),
158 1
					'amount' => $cartItem->getAmount(),
159
				];
160
161 1
				if ($cartItem->getDescription() !== null) {
162 1
					$cartItemValues['description'] = $cartItem->getDescription();
163
				}
164
165 1
				return $cartItemValues;
166
167 1
			}, $this->cart->getItems()),
168 1
			'description' => $this->description,
169 1
			'language' => $this->language->getValue(),
170
		];
171
172 1
		if ($this->merchantData !== null) {
173 1
			$requestData['merchantData'] = base64_encode($this->merchantData);
174
		}
175
176 1
		if ($this->customerId !== null) {
177 1
			$requestData['customerId'] = $this->customerId;
178
		}
179
180 1
		if ($this->ttlSec !== null) {
181 1
			$requestData['ttlSec'] = $this->ttlSec;
182
		}
183
184 1
		if ($this->logoVersion !== null) {
185 1
			$requestData['logoVersion'] = $this->logoVersion;
186
		}
187
188 1
		if ($this->colorSchemeVersion !== null) {
189 1
			$requestData['colorSchemeVersion'] = $this->colorSchemeVersion;
190
		}
191
192 1
		$response = $apiClient->post(
193 1
			'payment/init',
194
			$requestData,
195 1
			new SignatureDataFormatter([
196 1
				'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 1
			new SignatureDataFormatter([
221 1
				'payId' => null,
222
				'dttm' => null,
223
				'resultCode' => null,
224
				'resultMessage' => null,
225
				'paymentStatus' => null,
226
				'authCode' => null,
227
			])
228
		);
229
230 1
		$data = $response->getData();
231
232 1
		return new PaymentResponse(
233 1
			$data['payId'],
234 1
			DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
235 1
			new ResultCode($data['resultCode']),
236 1
			$data['resultMessage'],
237 1
			isset($data['paymentStatus']) ? new PaymentStatus($data['paymentStatus']) : null,
238 1
			$data['authCode'] ?? null
239
		);
240
	}
241
242
}
243