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