Util   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 50
ccs 16
cts 16
cp 1
rs 10
c 3
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_invoice_number() 0 21 2
A get_transaction_service() 0 12 4
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Buckaroo;
4
5
use Pronamic\WordPress\Pay\Payments\Payment;
6
7
/**
8
 * Title: Buckaroo utility class
9
 * Description:
10
 * Copyright: 2005-2022 Pronamic
11
 * Company: Pronamic
12
 *
13
 * @author Remco Tolsma
14
 * @version 2.0.0
15
 * @since 1.0.0
16
 */
17
class Util {
18
	/**
19
	 * Get invoice number.
20
	 *
21
	 * @param string $invoice_number
22
	 * @param Payment $payment
23
	 *
24
	 * @return string
25
	 */
26 6
	public static function get_invoice_number( $invoice_number, Payment $payment ) {
27
		// Replacements definition
28
		$replacements = array(
29 6
			'{order_id}'   => $payment->get_order_id(),
30 6
			'{payment_id}' => $payment->get_id(),
31
		);
32
33
		// Find and replace
34 6
		$invoice_number = str_replace(
35 6
			array_keys( $replacements ),
36 6
			array_values( $replacements ),
37
			$invoice_number,
38
			$count
39
		);
40
41
		// Make sure there is an dynamic part in the order ID
42 6
		if ( 0 === $count ) {
43 1
			$invoice_number .= $payment->get_id();
44
		}
45
46 6
		return $invoice_number;
47
	}
48
49
	/**
50
	 * Get first service name from transaction.
51
	 *
52
	 * @param object $transaction Transaction object.
53
	 * @return string|null
54
	 */
55 2
	public static function get_transaction_service( $transaction ) {
56 2
		if ( \property_exists( $transaction, 'Services' ) ) {
57 2
			foreach ( $transaction->Services as $service ) {
58 2
				if ( ! \property_exists( $service, 'Name' ) ) {
59 2
					continue;
60
				}
61
62 1
				return $service->Name;
63
			}
64
		}
65
66 1
		return null;
67
	}
68
}
69