Failed Conditions
Push — develop ( eeb50e...eb62e3 )
by Remco
03:13
created

NotificationRequest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 80
ccs 19
cts 23
cp 0.8261
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A is_live() 0 2 1
A __construct() 0 3 1
A get_items() 0 2 1
A from_object() 0 26 6
1
<?php
2
/**
3
 * Notification request
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 InvalidArgumentException;
14
15
/**
16
 * Notification request
17
 *
18
 * @link    https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequest
19
 * @author  Remco Tolsma
20
 * @version 1.0.0
21
 * @since   1.0.0
22
 */
23
class NotificationRequest {
24
	/**
25
	 * Informs about the origin of the notification:
26
	 *
27
	 * - `true`: the notification originated from the live environment.
28
	 * - `false`: the notification originated from the test environment.
29
	 *
30
	 * @var boolean
31
	 */
32
	private $live;
33
34
	/**
35
	 * A container object for the details included in the notification.
36
	 *
37
	 * @var array
38
	 */
39
	private $items;
40
41
	/**
42
	 * Construct notification request.
43
	 *
44
	 * @param boolean $live  Informs about the origin of the notification.
45
	 * @param array   $items A container object for the details included in the notification.
46
	 */
47 1
	public function __construct( $live, $items ) {
48 1
		$this->live  = $live;
49 1
		$this->items = $items;
50 1
	}
51
52
	/**
53
	 * Live.
54
	 *
55
	 * @return boolean True if live, false otherwise.
56
	 */
57 1
	public function is_live() {
58 1
		return $this->live;
59
	}
60
61
	/**
62
	 * Get items.
63
	 *
64
	 * @return array
65
	 */
66 1
	public function get_items() {
67 1
		return $this->items;
68
	}
69
70
	/**
71
	 * Create notification request from object.
72
	 *
73
	 * @param object $object Object.
74
	 * @return NotificationRequest
75
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
76
	 */
77 1
	public static function from_object( $object ) {
78 1
		if ( ! isset( $object->live ) ) {
79
			throw new InvalidArgumentException( 'Object must contain `live` property.' );
80
		}
81
82 1
		if ( ! isset( $object->notificationItems ) ) {
83
			throw new InvalidArgumentException( 'Object must contain `notificationItems` property.' );
84
		}
85
86 1
		if ( ! is_array( $object->notificationItems ) ) {
87
			throw new InvalidArgumentException( 'Object property `notificationItems` must be an array.' );			
88
		}
89
90 1
		$items = array();
91
92 1
		foreach ( $object->notificationItems as $o ) {
93 1
			if ( ! isset( $o->NotificationRequestItem ) ) {
94
				throw new InvalidArgumentException( 'Object must contain `NotificationRequestItem` property.' );
95
			}
96
97 1
			$items[] = NotificationRequestItem::from_object( $o->NotificationRequestItem );
98
		}
99
100 1
		return new self(
101 1
			filter_var( $object->live, FILTER_VALIDATE_BOOLEAN ),
102 1
			$items
103
		);
104
	}
105
}
106