InitApplePayRequest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 92.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 57
dl 0
loc 102
ccs 35
cts 38
cp 0.9211
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 51 4
A __construct() 0 25 3
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway\Call\ApplePay;
4
5
use DateTimeImmutable;
6
use SlevomatCsobGateway\Api\ApiClient;
7
use SlevomatCsobGateway\Call\PaymentResponse;
8
use SlevomatCsobGateway\Call\PaymentStatus;
9
use SlevomatCsobGateway\Call\ResultCode;
10
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
11
use SlevomatCsobGateway\Price;
12
use SlevomatCsobGateway\Validator;
13
use function base64_encode;
14
15
class InitApplePayRequest
16
{
17
18
	/** @var string */
19
	private $merchantId;
20
21
	/** @var string */
22
	private $orderId;
23
24
	/** @var string|null */
25
	private $clientIp;
26
27
	/** @var bool */
28
	private $closePayment;
29
30
	/** @var Price */
31
	private $totalPrice;
32
33
	/** @var string|null */
34
	private $merchantData;
35
36
	/** @var int|null */
37
	private $ttlSec;
38
39 2
	public function __construct(
40
		string $merchantId,
41
		string $orderId,
42
		string $clientIp,
43
		Price $totalPrice,
44
		bool $closePayment,
45
		?string $merchantData,
46
		?int $ttlSec = null
47
	)
48
	{
49 2
		Validator::checkOrderId($orderId);
50 2
		if ($merchantData !== null) {
51 1
			Validator::checkMerchantData($merchantData);
52
		}
53 2
		if ($ttlSec !== null) {
54
			Validator::checkTtlSec($ttlSec);
55
		}
56
57 2
		$this->merchantId = $merchantId;
58 2
		$this->orderId = $orderId;
59 2
		$this->clientIp = $clientIp;
60 2
		$this->totalPrice = $totalPrice;
61 2
		$this->closePayment = $closePayment;
62 2
		$this->merchantData = $merchantData;
63 2
		$this->ttlSec = $ttlSec;
64 2
	}
65
66 1
	public function send(ApiClient $apiClient): PaymentResponse
67
	{
68
		$requestData = [
69 1
			'merchantId' => $this->merchantId,
70 1
			'orderNo' => $this->orderId,
71 1
			'totalAmount' => $this->totalPrice->getAmount(),
72 1
			'currency' => $this->totalPrice->getCurrency()->getValue(),
73 1
			'closePayment' => $this->closePayment,
74 1
			'clientIp' => $this->clientIp,
75
		];
76
77 1
		if ($this->merchantData !== null) {
78
			$requestData['merchantData'] = base64_encode($this->merchantData);
79
		}
80
81 1
		if ($this->ttlSec !== null) {
82
			$requestData['ttlSec'] = $this->ttlSec;
83
		}
84
85 1
		$response = $apiClient->post(
86 1
			'applepay/init',
87
			$requestData,
88 1
			new SignatureDataFormatter([
89 1
				'merchantId' => null,
90
				'orderNo' => null,
91
				'dttm' => null,
92
				'clientIp' => null,
93
				'totalAmount' => null,
94
				'currency' => null,
95
				'closePayment' => null,
96
				'merchantData' => null,
97
				'ttlSec' => null,
98
			]),
99 1
			new SignatureDataFormatter([
100 1
				'payId' => null,
101
				'dttm' => null,
102
				'resultCode' => null,
103
				'resultMessage' => null,
104
				'paymentStatus' => null,
105
			])
106
		);
107
108 1
		$data = $response->getData();
109 1
		$responseDateTime = DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']);
110
111 1
		return new PaymentResponse(
112 1
			$data['payId'],
113
			$responseDateTime,
0 ignored issues
show
Bug introduced by
It seems like $responseDateTime 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

113
			/** @scrutinizer ignore-type */ $responseDateTime,
Loading history...
114 1
			ResultCode::get($data['resultCode']),
115 1
			$data['resultMessage'],
116 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null
117
		);
118
	}
119
120
}
121