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

PaymentButtonRequest::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 26
rs 9.8666
c 1
b 0
f 0
cc 2
nc 2
nop 9

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\Button;
4
5
use DateTimeImmutable;
6
use SlevomatCsobGateway\Api\ApiClient;
7
use SlevomatCsobGateway\Api\HttpMethod;
8
use SlevomatCsobGateway\Call\PaymentStatus;
9
use SlevomatCsobGateway\Call\ResultCode;
10
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
11
use SlevomatCsobGateway\Language;
12
use SlevomatCsobGateway\Price;
13
use SlevomatCsobGateway\Validator;
14
15
class PaymentButtonRequest
16
{
17
18
	/** @var string */
19
	private $merchantId;
20
21
	/** @var string */
22
	private $orderId;
23
24
	/** @var string */
25
	private $clientIp;
26
27
	/** @var \SlevomatCsobGateway\Price */
28
	private $totalPrice;
29
30
	/** @var string */
31
	private $returnUrl;
32
33
	/** @var \SlevomatCsobGateway\Api\HttpMethod */
34
	private $returnMethod;
35
36
	/** @var \SlevomatCsobGateway\Call\Button\PaymentButtonBrand */
37
	private $brand;
38
39
	/** @var string|null */
40
	private $merchantData;
41
42
	/** @var \SlevomatCsobGateway\Language */
43
	private $language;
44
45
	public function __construct(
46
		string $merchantId,
47
		string $orderId,
48
		string $clientIp,
49
		Price $totalPrice,
50
		string $returnUrl,
51
		HttpMethod $returnMethod,
52
		PaymentButtonBrand $brand,
53
		?string $merchantData,
54
		Language $language
55
	)
56
	{
57
		Validator::checkReturnUrl($returnUrl);
58
		if ($merchantData !== null) {
59
			Validator::checkMerchantData($merchantData);
60
		}
61
62
		$this->merchantId = $merchantId;
63
		$this->orderId = $orderId;
64
		$this->clientIp = $clientIp;
65
		$this->totalPrice = $totalPrice;
66
		$this->returnUrl = $returnUrl;
67
		$this->returnMethod = $returnMethod;
68
		$this->brand = $brand;
69
		$this->merchantData = $merchantData;
70
		$this->language = $language;
71
	}
72
73
	public function send(ApiClient $apiClient): PaymentButtonResponse
74
	{
75
		$requestData = [
76
			'merchantId' => $this->merchantId,
77
			'orderNo' => $this->orderId,
78
			'clientIp' => $this->clientIp,
79
			'totalAmount' => $this->totalPrice->getAmount(),
80
			'currency' => $this->totalPrice->getCurrency()->getValue(),
81
			'returnUrl' => $this->returnUrl,
82
			'returnMethod' => $this->returnMethod->getValue(),
83
			'brand' => $this->brand->getValue(),
84
			'language' => $this->language->getValue(),
85
		];
86
87
		if ($this->merchantData !== null) {
88
			$requestData['merchantData'] = $this->merchantData;
89
		}
90
91
		$response = $apiClient->post(
92
			'button/init',
93
			$requestData,
94
			new SignatureDataFormatter([
95
				'merchantId' => null,
96
				'orderNo' => null,
97
				'dttm' => null,
98
				'clientIp' => null,
99
				'totalAmount' => null,
100
				'currency' => null,
101
				'returnUrl' => null,
102
				'returnMethod' => null,
103
				'brand' => null,
104
				'merchantData' => null,
105
				'language' => null,
106
			]),
107
			new SignatureDataFormatter([
108
				'payId' => null,
109
				'dttm' => null,
110
				'resultCode' => null,
111
				'resultMessage' => null,
112
				'paymentStatus' => null,
113
				'redirect' => [
114
					'method' => null,
115
					'url' => null,
116
					'params' => null,
117
				],
118
			])
119
		);
120
121
		$data = $response->getData();
122
123
		$redirectUrl = null;
124
		$redirectMethod = null;
125
		$redirectParams = [];
126
		if (isset($data['redirect'])) {
127
			$redirectUrl = $data['redirect']['url'];
128
			$redirectMethod = HttpMethod::get($data['redirect']['method']);
129
			$redirectParams = $data['redirect']['params'] ?? null;
130
		}
131
132
		return new PaymentButtonResponse(
133
			$data['payId'],
134
			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

134
			/** @scrutinizer ignore-type */ DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
Loading history...
135
			ResultCode::get($data['resultCode']),
136
			$data['resultMessage'],
137
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null,
138
			$redirectMethod,
139
			$redirectUrl,
140
			$redirectParams
141
		);
142
	}
143
144
}
145