TransactionParser::parse()   F
last analyzed

Complexity

Conditions 18
Paths > 20000

Size

Total Lines 74
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 74
ccs 0
cts 54
cp 0
rs 0.7
cc 18
nc 55296
nop 1
crap 342

How to fix   Long Method    Complexity   

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
2
/**
3
 * Transaction parser
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Payments
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Sisow\XML;
12
13
use Pronamic\WordPress\DateTime\DateTime;
14
use Pronamic\WordPress\Pay\Core\Util;
15
use Pronamic\WordPress\Pay\Core\XML\Security;
16
use Pronamic\WordPress\Pay\Gateways\Sisow\Transaction;
17
use SimpleXMLElement;
18
19
/**
20
 * Transaction parser
21
 * Description:
22
 * Copyright: 2005-2022 Pronamic
23
 * Company: Pronamic
24
 *
25
 * @author  Remco Tolsma
26
 * @version 2.0.4
27
 * @since   1.0.0
28
 */
29
class TransactionParser implements Parser {
30
	/**
31
	 * Parse XML element.
32
	 *
33
	 * @param SimpleXMLElement $xml XML element to parse.
34
	 * @return Transaction
35
	 */
36
	public static function parse( SimpleXMLElement $xml ) {
37
		$transaction = new Transaction();
38
39
		// Transaction request.
40
		if ( isset( $xml->trxid ) ) {
41
			$transaction->id = Security::filter( $xml->trxid );
42
		}
43
44
		if ( isset( $xml->issuerurl ) ) {
45
			$issuer_url = Security::filter( $xml->issuerurl );
46
47
			if ( null !== $issuer_url ) {
48
				$transaction->issuer_url = urldecode( $issuer_url );
49
			}
50
		}
51
52
		// Status response.
53
		if ( isset( $xml->status ) ) {
54
			$transaction->status = Security::filter( $xml->status );
55
		}
56
57
		if ( isset( $xml->amount ) ) {
58
			$amount = Security::filter( $xml->amount );
59
60
			if ( null !== $amount ) {
61
				$transaction->amount = $amount / 100;
62
			}
63
		}
64
65
		if ( isset( $xml->purchaseid ) ) {
66
			$transaction->purchase_id = Security::filter( $xml->purchaseid );
67
		}
68
69
		if ( isset( $xml->description ) ) {
70
			$transaction->description = Security::filter( $xml->description );
71
		}
72
73
		if ( isset( $xml->entrancecode ) ) {
74
			$transaction->entrance_code = Security::filter( $xml->entrancecode );
75
		}
76
77
		if ( isset( $xml->issuerid ) ) {
78
			$transaction->issuer_id = Security::filter( $xml->issuerid );
79
		}
80
81
		if ( isset( $xml->timestamp ) ) {
82
			$timestamp = Security::filter( $xml->timestamp );
83
84
			if ( null !== $timestamp ) {
85
				$transaction->timestamp = new DateTime( $timestamp );
86
			}
87
		}
88
89
		if ( isset( $xml->consumername ) ) {
90
			$transaction->consumer_name = Security::filter( $xml->consumername );
91
		}
92
93
		if ( isset( $xml->consumeraccount ) ) {
94
			$transaction->consumer_account = Security::filter( $xml->consumeraccount );
95
		}
96
97
		if ( isset( $xml->consumercity ) ) {
98
			$transaction->consumer_city = Security::filter( $xml->consumercity );
99
		}
100
101
		if ( isset( $xml->consumeriban ) ) {
102
			$transaction->consumer_iban = Security::filter( $xml->consumeriban );
103
		}
104
105
		if ( isset( $xml->consumerbic ) ) {
106
			$transaction->consumer_bic = Security::filter( $xml->consumerbic );
107
		}
108
109
		return $transaction;
110
	}
111
}
112