Passed
Push — develop ( 6cb854...ee4d39 )
by Remco
03:23
created

NotificationRequest::from_object()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
crap 3.0032
rs 9.7998
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
use JsonSchema\Constraints\Constraint;
15
use JsonSchema\Exception\ValidationException;
16
use JsonSchema\Validator;
17
18
/**
19
 * Notification request
20
 *
21
 * @link https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequest
22
 *
23
 * @author  Remco Tolsma
24
 * @version 1.0.0
25
 * @since   1.0.0
26
 */
27
class NotificationRequest extends ResponseObject {
28
	/**
29
	 * Informs about the origin of the notification:
30
	 *
31
	 * - `true`: the notification originated from the live environment.
32
	 * - `false`: the notification originated from the test environment.
33
	 *
34
	 * @var boolean
35
	 */
36
	private $live;
37
38
	/**
39
	 * A container object for the details included in the notification.
40
	 *
41
	 * @var array
42
	 */
43
	private $items;
44
45
	/**
46
	 * Construct notification request.
47
	 *
48
	 * @param boolean                   $live  Informs about the origin of the notification.
49
	 * @param NotificationRequestItem[] $items A container object for the details included in the notification.
50
	 */
51 4
	public function __construct( $live, $items ) {
52 4
		$this->live  = $live;
53 4
		$this->items = $items;
54 4
	}
55
56
	/**
57
	 * Live.
58
	 *
59
	 * @return boolean True if live, false otherwise.
60
	 */
61 1
	public function is_live() {
62 1
		return $this->live;
63
	}
64
65
	/**
66
	 * Get items.
67
	 *
68
	 * @return NotificationRequestItem[]
69
	 */
70 4
	public function get_items() {
71 4
		return $this->items;
72
	}
73
74
	/**
75
	 * Create notification request from object.
76
	 *
77
	 * @param object $object Object.
78
	 * @return NotificationRequest
79
	 * @throws InvalidArgumentException Throws JSON schema validation exception when JSON is invalid.
80
	 */
81 6
	public static function from_object( $object ) {
82 6
		$validator = new Validator();
83
84 6
		$validator->validate(
85 6
			$object,
86
			(object) array(
87 6
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/notification-request.json' ),
88
			),
89 6
			Constraint::CHECK_MODE_EXCEPTIONS
90
		);
91
92 5
		$items = array();
93
94 5
		foreach ( $object->notificationItems as $o ) {
95 5
			if ( ! isset( $o->NotificationRequestItem ) ) {
96
				throw new InvalidArgumentException( 'Object must contain `NotificationRequestItem` property.' );
97
			}
98
99 5
			$items[] = NotificationRequestItem::from_object( $o->NotificationRequestItem );
100
		}
101
102 4
		return new self(
103 4
			filter_var( $object->live, FILTER_VALIDATE_BOOLEAN ),
104 4
			$items
105
		);
106
	}
107
}
108