Completed
Pull Request — master (#8)
by Jan
06:58
created

InitPaymentRequest::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 38
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 38
cc 3
eloc 32
nc 4
nop 12
rs 8.8571

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|null
74
	 */
75
	private $language;
76
77
	public function __construct(
78
		string $merchantId,
79
		string $orderId,
80
		PayOperation $payOperation,
81
		PayMethod $payMethod,
82
		bool $closePayment,
83
		string $returnUrl,
84
		HttpMethod $returnMethod,
85
		Cart $cart,
86
		string $description,
87
		string $merchantData = null,
88
		string $customerId = null,
89
		Language $language = null
90
	)
91
	{
92
		Validator::checkOrderId($orderId);
93
		Validator::checkReturnUrl($returnUrl);
94
		Validator::checkDescription($description);
95
		if ($merchantData !== null) {
96
			Validator::checkMerchantData($merchantData);
97
		}
98
		if ($customerId !== null) {
99
			Validator::checkCustomerId($customerId);
100
		}
101
102
		$this->merchantId = $merchantId;
103
		$this->orderId = $orderId;
104
		$this->payOperation = $payOperation;
105
		$this->payMethod = $payMethod;
106
		$this->closePayment = $closePayment;
107
		$this->returnUrl = $returnUrl;
108
		$this->returnMethod = $returnMethod;
109
		$this->cart = $cart;
110
		$this->description = $description;
111
		$this->merchantData = $merchantData;
112
		$this->customerId = $customerId;
113
		$this->language = $language;
114
	}
115
116
	public function send(ApiClient $apiClient): PaymentResponse
117
	{
118
		$requestData = [
119
			'merchantId' => $this->merchantId,
120
			'orderNo' => $this->orderId,
121
			'payOperation' => $this->payOperation->getValue(),
122
			'payMethod' => $this->payMethod->getValue(),
123
			'totalAmount' => $this->cart->countTotalAmount(),
124
			'currency' => $this->cart->getCurrency()->getValue(),
125
			'closePayment' => $this->closePayment,
126
			'returnUrl' => $this->returnUrl,
127
			'returnMethod' => $this->returnMethod->getValue(),
128
			'cart' => array_map(function (CartItem $cartItem) {
129
				$cartItemValues = [
130
					'name' => $cartItem->getName(),
131
					'quantity' => $cartItem->getQuantity(),
132
					'amount' => $cartItem->getAmount(),
133
				];
134
135
				if ($cartItem->getDescription() !== null) {
136
					$cartItemValues['description'] = $cartItem->getDescription();
137
				}
138
139
				return $cartItemValues;
140
141
			}, $this->cart->getItems()),
142
			'description' => $this->description,
143
		];
144
145
		if ($this->merchantData !== null) {
146
			$requestData['merchantData'] = base64_encode($this->merchantData);
147
		}
148
149
		if ($this->customerId !== null) {
150
			$requestData['customerId'] = $this->customerId;
151
		}
152
153
		if ($this->language !== null) {
154
			$requestData['language'] = $this->language->getValue();
155
		}
156
157
		$response = $apiClient->post(
158
			'payment/init',
159
			$requestData,
160
			new SignatureDataFormatter([
161
				'merchantId' => null,
162
				'orderNo' => null,
163
				'dttm' => null,
164
				'payOperation' => null,
165
				'payMethod' => null,
166
				'totalAmount' => null,
167
				'currency' => null,
168
				'closePayment' => null,
169
				'returnUrl' => null,
170
				'returnMethod' => null,
171
				'cart' => [
172
					'name' => null,
173
					'quantity' => null,
174
					'amount' => null,
175
					'description' => null,
176
				],
177
				'description' => null,
178
				'merchantData' => null,
179
				'customerId' => null,
180
				'language' => null,
181
			]),
182
			new SignatureDataFormatter([
183
				'payId' => null,
184
				'dttm' => null,
185
				'resultCode' => null,
186
				'resultMessage' => null,
187
				'paymentStatus' => null,
188
				'authCode' => null,
189
			])
190
		);
191
192
		$data = $response->getData();
193
194
		return new PaymentResponse(
195
			$data['payId'],
196
			DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
197
			new ResultCode($data['resultCode']),
198
			$data['resultMessage'],
199
			isset($data['paymentStatus']) ? new PaymentStatus($data['paymentStatus']) : null,
200
			$data['authCode'] ?? null
201
		);
202
	}
203
204
}
205