NotificationRequest::get_items()   A
last analyzed

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