Completed
Push — master ( 7d5f81...069e15 )
by Jan
02:05
created

StandardCheckoutRequest::send()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 21
cts 21
cp 1
rs 9.4929
c 0
b 0
f 0
cc 3
eloc 39
nc 2
nop 1
crap 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway\Call\Masterpass;
4
5
use DateTimeImmutable;
6
use SlevomatCsobGateway\Api\ApiClient;
7
use SlevomatCsobGateway\Call\PaymentStatus;
8
use SlevomatCsobGateway\Call\ResultCode;
9
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
10
use SlevomatCsobGateway\Validator;
11
12
class StandardCheckoutRequest
13
{
14
15
	/**
16
	 * @var string
17
	 */
18
	private $merchantId;
19
20
	/**
21
	 * @var string
22
	 */
23
	private $payId;
24
25
	/** @var string */
26
	private $callbackUrl;
27
28
	/** @var null|string */
29
	private $shippingLocationProfile;
30
31 2
	public function __construct(
32
		string $merchantId,
33
		string $payId,
34
		string $callbackUrl,
35
		?string $shippingLocationProfile = null
36
	)
37
	{
38 2
		Validator::checkPayId($payId);
39 2
		Validator::checkReturnUrl($callbackUrl);
40
41 2
		$this->merchantId = $merchantId;
42 2
		$this->payId = $payId;
43 2
		$this->callbackUrl = $callbackUrl;
44 2
		$this->shippingLocationProfile = $shippingLocationProfile;
45 2
	}
46
47 1
	public function send(ApiClient $apiClient): CheckoutResponse
48
	{
49
		$requestData = [
50 1
			'merchantId' => $this->merchantId,
51 1
			'payId' => $this->payId,
52 1
			'callbackUrl' => $this->callbackUrl,
53
		];
54
55 1
		if ($this->shippingLocationProfile !== null) {
56 1
			$requestData['shippingLocationProfile'] = $this->shippingLocationProfile;
57
		}
58
59 1
		$response = $apiClient->post(
60 1
			'masterpass/standard/checkout',
61 1
			$requestData,
62 1
			new SignatureDataFormatter([
63 1
				'merchantId' => null,
64
				'payId' => null,
65
				'dttm' => null,
66
				'callbackUrl' => null,
67
				'shippingLocationProfile' => null,
68
			]),
69 1
			new SignatureDataFormatter([
70 1
				'payId' => null,
71
				'dttm' => null,
72
				'resultCode' => null,
73
				'resultMessage' => null,
74
				'paymentStatus' => null,
75
				'lightboxParams' => [
76
					'requestToken' => null,
77
					'callbackUrl' => null,
78
					'merchantCheckoutId' => null,
79
					'allowedCardTypes' => null,
80
					'suppressShippingAddressEnable' => null,
81
					'loyaltyEnabled' => null,
82
					'version' => null,
83
					'shippingLocationProfile' => null,
84
				],
85
			])
86
		);
87
88 1
		$data = $response->getData();
89
90 1
		return new CheckoutResponse(
91 1
			$data['payId'],
92 1
			DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']),
93 1
			ResultCode::get($data['resultCode']),
94 1
			$data['resultMessage'],
95 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null,
96 1
			$data['lightboxParams'] ?? null
97
		);
98
	}
99
100
}
101