StatusStringParser   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 68
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 53 4
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\TargetPay;
4
5
/**
6
 * Title: Status string parser
7
 * Description:
8
 * Copyright: 2005-2021 Pronamic
9
 * Company: Pronamic
10
 *
11
 * @author  Remco Tolsma
12
 * @version 2.0.0
13
 * @since   1.0.0
14
 */
15
class StatusStringParser {
16
	/**
17
	 * Token used by TargetPay to separate some values
18
	 *
19
	 * @var string
20
	 */
21
	const TOKEN = ' |';
22
23
	/**
24
	 * Parse an TargetPay status string to an object
25
	 *
26
	 * @param string $string an TargetPay status string
27
	 *
28
	 * @return Status
29
	 */
30 3
	public static function parse( $string ) {
31 3
		$status = new Status();
32
33 3
		$position_space = strpos( $string, ' ' );
34 3
		$position_pipe  = strpos( $string, '|' );
35
36 3
		if ( false !== $position_space ) {
37
			/*
38
			 * @link https://www.targetpay.com/info/ideal-docu
39
			 *
40
			 * If the payment is valid the following response will be returned:
41
			 * 000000 OK
42
			 *
43
			 * If the payment is not valid (yet) the following response will be returned:
44
			 * TP0010 Transaction has not been completed, try again later
45
			 * TP0011 Transaction has been cancelled
46
			 * TP0012 Transaction has expired (max. 10 minutes)
47
			 * TP0013 The transaction could not be processed
48
			 * TP0014 Already used
49
			 *
50
			 * TP0020 Layoutcode not entered
51
			 * TP0021 Tansaction ID not entered
52
			 * TP0022 No transaction found with this ID
53
			 * TP0023 Layoutcode does not match this transaction
54
			 */
55 3
			$status->code = substr( $string, 0, $position_space );
56
57 3
			$position_description = $position_space + 1;
58 3
			if ( false !== $position_pipe ) {
59 1
				$length = $position_pipe - $position_description;
60
61 1
				$status->description = substr( $string, $position_description, $length );
62
			} else {
63 2
				$status->description = substr( $string, $position_description );
64
			}
65
66 3
			if ( false !== $position_pipe ) {
67 1
				$extra = substr( $string, $position_pipe + 1 );
68
69
				/*
70
				 * @link https://www.targetpay.com/info/directdebit-docu
71
				 *
72
				 * The response of the ideal/check call will be:
73
				 * 00000 OK|accountnumber|accountname|accountcity
74
				 * You may use accountnumber and accountname as input for the cbank and cname parameters
75
				 */
76 1
				$status->account_number = strtok( $extra, self::TOKEN );
77 1
				$status->account_name   = strtok( self::TOKEN );
78 1
				$status->account_city   = strtok( self::TOKEN );
79
			}
80
		}
81
82 3
		return $status;
83
	}
84
}
85