StartApplePayRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 42
c 1
b 0
f 0
dl 0
loc 81
ccs 28
cts 30
cp 0.9333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A send() 0 45 4
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\Validator;
12
use const JSON_ERROR_NONE;
13
use const JSON_UNESCAPED_SLASHES;
14
use const JSON_UNESCAPED_UNICODE;
15
use function base64_encode;
16
use function json_encode;
17
use function json_last_error;
18
use function json_last_error_msg;
19
20
class StartApplePayRequest
21
{
22
23
	/** @var string */
24
	private $merchantId;
25
26
	/** @var string */
27
	private $payId;
28
29
	/** @var array|mixed[] */
30
	private $payload;
31
32
	/** @var int|null */
33
	private $totalAmount;
34
35
	/**
36
	 * @param string $merchantId
37
	 * @param string $payId
38
	 * @param mixed[] $payload
39
	 * @param int|null $totalAmount
40
	 */
41 2
	public function __construct(
42
		string $merchantId,
43
		string $payId,
44
		array $payload,
45
		?int $totalAmount
46
	)
47
	{
48 2
		Validator::checkPayId($payId);
49
50 2
		$this->merchantId = $merchantId;
51 2
		$this->payId = $payId;
52 2
		$this->payload = $payload;
53 2
		$this->totalAmount = $totalAmount;
54 2
	}
55
56 1
	public function send(ApiClient $apiClient): PaymentResponse
57
	{
58 1
		$payloadData = json_encode($this->payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
59 1
		$error = json_last_error();
60 1
		if ($error !== JSON_ERROR_NONE) {
61
			throw new InvalidJsonPayloadException(json_last_error_msg(), $error);
62
		}
63
		$requestData = [
64 1
			'merchantId' => $this->merchantId,
65 1
			'payId' => $this->payId,
66 1
			'payload' => base64_encode((string) $payloadData),
67
		];
68
69 1
		if ($this->totalAmount !== null) {
70
			$requestData['totalAmount'] = $this->totalAmount;
71
		}
72
73 1
		$response = $apiClient->post(
74 1
			'applepay/start',
75
			$requestData,
76 1
			new SignatureDataFormatter([
77 1
				'merchantId' => null,
78
				'payId' => null,
79
				'payload' => null,
80
				'totalAmount' => null,
81
				'dttm' => null,
82
			]),
83 1
			new SignatureDataFormatter([
84 1
				'payId' => null,
85
				'dttm' => null,
86
				'resultCode' => null,
87
				'resultMessage' => null,
88
				'paymentStatus' => null,
89
			])
90
		);
91
92 1
		$data = $response->getData();
93 1
		$responseDateTime = DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']);
94
95 1
		return new PaymentResponse(
96 1
			$data['payId'],
97
			$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

97
			/** @scrutinizer ignore-type */ $responseDateTime,
Loading history...
98 1
			ResultCode::get($data['resultCode']),
99 1
			$data['resultMessage'],
100 1
			isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null
101
		);
102
	}
103
104
}
105