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

NotificationRequest::is_live()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
	 * Create notification request from object.
63
	 *
64
	 * @param object $object Object.
65
	 * @return NotificationRequest
66
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
67
	 */
68 1
	public static function from_object( $object ) {
69 1
		if ( ! isset( $object->live ) ) {
70
			throw new InvalidArgumentException( 'Object must contain `live` property.' );
71
		}
72
73 1
		if ( ! isset( $object->notificationItems ) ) {
74
			throw new InvalidArgumentException( 'Object must contain `notificationItems` property.' );
75
		}
76
77 1
		if ( ! is_array( $object->notificationItems ) ) {
78
			throw new InvalidArgumentException( 'Object property `notificationItems` must be an array.' );			
79
		}
80
81 1
		$items = array();
82
83 1
		foreach ( $object->notificationItems as $o ) {
84 1
			if ( ! isset( $o->NotificationRequestItem ) ) {
85
				throw new InvalidArgumentException( 'Object must contain `NotificationRequestItem` property.' );
86
			}
87
88 1
			$items[] = NotificationRequestItem::from_object( $o->NotificationRequestItem );
89
		}
90
91 1
		return new self(
92 1
			filter_var( $object->live, FILTER_VALIDATE_BOOLEAN ),
93 1
			$items
94
		);
95
	}
96
}
97