Failed Conditions
Push — develop ( def963...184746 )
by Remco
02:41
created

NotificationRequestItem::from_object()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 20.9461

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 11
nop 1
dl 0
loc 44
ccs 13
cts 23
cp 0.5652
crap 20.9461
rs 7.3166
c 0
b 0
f 0

How to fix   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
 * Notification request item
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use DateTime;
14
use InvalidArgumentException;
15
16
/**
17
 * Notification request item
18
 *
19
 * @link    https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequestitem
20
 * @author  Remco Tolsma
21
 * @version 1.0.0
22
 * @since   1.0.0
23
 */
24
class NotificationRequestItem {
25
	/**
26
	 * Amount.
27
	 *
28
	 * @var Amount
29
	 */
30
	private $amount;
31
32
	/**
33
	 * Adyen's 16-character unique reference associated with the transaction/the request. This value is globally unique; quote it when communicating with us about this request.
34
	 *
35
	 * @var string
36
	 */
37
	private $psp_reference;
0 ignored issues
show
introduced by
The private property $psp_reference is not used, and could be removed.
Loading history...
38
39
	/**
40
	 * The type of event the notification item refers to.
41
	 *
42
	 * @var string
43
	 */
44
	private $event_code;
0 ignored issues
show
introduced by
The private property $event_code is not used, and could be removed.
Loading history...
45
46
	/**
47
	 * The time when the event was generated.
48
	 *
49
	 * @var DateTime
50
	 */
51
	private $event_date;
0 ignored issues
show
introduced by
The private property $event_date is not used, and could be removed.
Loading history...
52
53
	/**
54
	 * The merchant account identifier used in the transaction the notification item refers to.
55
	 *
56
	 * @var string
57
	 */
58
	private $merchant_account_code;
0 ignored issues
show
introduced by
The private property $merchant_account_code is not used, and could be removed.
Loading history...
59
60
	/**
61
	 * This field holds a list of the modification operations supported by the transaction the notification item refers to.
62
	 *
63
	 * @var array
64
	 */
65
	private $operations;
66
67
	/**
68
	 * A reference to uniquely identify the payment. 
69
	 *
70
	 * @var string
71
	 */
72
	private $merchant_reference;
0 ignored issues
show
introduced by
The private property $merchant_reference is not used, and could be removed.
Loading history...
73
74
	/**
75
	 * The payment method used in the transaction the notification item refers to.
76
	 *
77
	 * @var string
78
	 */
79
	private $payment_method;
0 ignored issues
show
introduced by
The private property $payment_method is not used, and could be removed.
Loading history...
80
81
	/**
82
	 * Informs about the outcome of the event (`eventCode`) the notification refers to:
83
	 *
84
	 * - `true`: the event (`eventCode`) the notification refers to was executed successfully.
85
	 * - `false`: the event was not executed successfully.
86
	 *
87
	 * @var boolean
88
	 */
89
	private $success;
90
91
	/**
92
	 * Create notification request item from object.
93
	 *
94
	 * @param object $object Object.
95
	 * @return NotificationRequestItem
96
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
97
	 */
98 1
	public static function from_object( $object ) {
99 1
		if ( ! isset( $object->amount ) ) {
100
			throw new InvalidArgumentException( 'Object must contain `amount` property.' );
101
		}
102
103 1
		if ( ! isset( $object->pspReference ) ) {
104
			throw new InvalidArgumentException( 'Object must contain `pspReference` property.' );
105
		}
106
107 1
		if ( ! isset( $object->eventCode ) ) {
108
			throw new InvalidArgumentException( 'Object must contain `eventCode` property.' );
109
		}
110
111 1
		if ( ! isset( $object->eventDate ) ) {
112
			throw new InvalidArgumentException( 'Object must contain `eventDate` property.' );
113
		}
114
115 1
		if ( ! isset( $object->merchantAccountCode ) ) {
116
			throw new InvalidArgumentException( 'Object must contain `merchantAccountCode` property.' );
117
		}
118
119 1
		if ( ! isset( $object->operations ) ) {
120
			throw new InvalidArgumentException( 'Object must contain `operations` property.' );
121
		}
122
123 1
		if ( ! isset( $object->merchantReference ) ) {
124
			throw new InvalidArgumentException( 'Object must contain `merchantReference` property.' );
125
		}
126
127 1
		if ( ! isset( $object->paymentMethod ) ) {
128
			throw new InvalidArgumentException( 'Object must contain `paymentMethod` property.' );
129
		}
130
131 1
		if ( ! isset( $object->success ) ) {
132
			throw new InvalidArgumentException( 'Object must contain `success` property.' );
133
		}
134
135 1
		if ( ! is_array( $object->operations ) ) {
136
			throw new InvalidArgumentException( 'Object property `operations` must be an array.' );			
137
		}
138
139 1
		$item = new self();
140
141 1
		return $item;
142
	}
143
}
144