Notification::from_object()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.9157

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 27
ccs 12
cts 17
cp 0.7059
crap 6.9157
rs 9.1111
1
<?php
2
/**
3
 * Notification
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
/**
14
 * Notification
15
 *
16
 * @author  Remco Tolsma
17
 * @version 2.1.10
18
 * @since   2.0.2
19
 */
20
class Notification extends ResponseMessage {
21
	/**
22
	 * The token that can be used to do the status pull.
23
	 *
24
	 * @var string
25
	 */
26
	private $authentication;
27
28
	/**
29
	 * The validity period of the token, in the ISO-8601 format (yyyy- MM-ddTHH: mm: ss.SSSZZ).
30
	 *
31
	 * @var string
32
	 */
33
	private $expiry;
34
35
	/**
36
	 * The type of notification. For the time being this is always: `merchant.order.status.changed`.
37
	 *
38
	 * @var string
39
	 */
40
	private $event_name;
41
42
	/**
43
	 * Identification of the webshop (point of interaction), seen from ROK. This is relevant if several webshops
44
	 * use the same webhook URL.
45
	 *
46
	 * @var int|string
47
	 */
48
	private $poi_id;
49
50
	/**
51
	 * Construct notification message.
52
	 *
53
	 * @param string     $authentication Authentication.
54
	 * @param string     $expiry         Expiry.
55
	 * @param string     $event_name     Event name.
56
	 * @param int|string $poi_id         POI ID.
57
	 * @param string     $signature      Signature.
58
	 */
59 1
	public function __construct( $authentication, $expiry, $event_name, $poi_id, $signature ) {
60 1
		parent::__construct( $signature );
61
62 1
		$this->authentication = $authentication;
63 1
		$this->expiry         = $expiry;
64 1
		$this->event_name     = $event_name;
65 1
		$this->poi_id         = $poi_id;
66 1
	}
67
68
	/**
69
	 * Get authentication.
70
	 *
71
	 * @return string
72
	 */
73 1
	public function get_authentication() {
74 1
		return $this->authentication;
75
	}
76
77
	/**
78
	 * Get expiry.
79
	 *
80
	 * @return string
81
	 */
82 1
	public function get_expiry() {
83 1
		return $this->expiry;
84
	}
85
86
	/**
87
	 * Check if this notice (authentication token) is expired.
88
	 *
89
	 * @return bool True if notice authentication token is expired, false otherwise.
90
	 */
91
	public function is_expired() {
92
		$timestamp = \strtotime( $this->get_expiry() );
93
94
		if ( false === $timestamp ) {
95
			return true;
96
		}
97
98
		return $timestamp > \time();
99
	}
100
101
	/**
102
	 * Get event name.
103
	 *
104
	 * @return string
105
	 */
106 1
	public function get_event_name() {
107 1
		return $this->event_name;
108
	}
109
110
	/**
111
	 * Get point of interaction ID.
112
	 *
113
	 * @return int|string
114
	 */
115 1
	public function get_poi_id() {
116 1
		return $this->poi_id;
117
	}
118
119
	/**
120
	 * Get signature fields.
121
	 *
122
	 * @return array<string>
123
	 */
124 1
	public function get_signature_fields() {
125
		return array(
126 1
			$this->get_authentication(),
127 1
			$this->get_expiry(),
128 1
			$this->get_event_name(),
129 1
			\strval( $this->get_poi_id() ),
130
		);
131
	}
132
133
	/**
134
	 * Create notification from object.
135
	 *
136
	 * @param \stdClass $object Object.
137
	 * @return Notification
138
	 * @throws \InvalidArgumentException Throws invalid argument exception when object does not contains the required
139
	 * properties.
140
	 */
141 1
	public static function from_object( \stdClass $object ) {
142 1
		if ( ! isset( $object->signature ) ) {
143
			throw new \InvalidArgumentException( 'Object must contain `signature` property.' );
144
		}
145
146 1
		if ( ! isset( $object->authentication ) ) {
147
			throw new \InvalidArgumentException( 'Object must contain `authentication` property.' );
148
		}
149
150 1
		if ( ! isset( $object->expiry ) ) {
151
			throw new \InvalidArgumentException( 'Object must contain `expiry` property.' );
152
		}
153
154 1
		if ( ! isset( $object->eventName ) ) {
155
			throw new \InvalidArgumentException( 'Object must contain `eventName` property.' );
156
		}
157
158 1
		if ( ! isset( $object->poiId ) ) {
159
			throw new \InvalidArgumentException( 'Object must contain `poiId` property.' );
160
		}
161
162 1
		return new self(
163 1
			$object->authentication,
164 1
			$object->expiry,
165 1
			$object->eventName,
166 1
			$object->poiId,
167 1
			$object->signature
168
		);
169
	}
170
171
	/**
172
	 * Create notification from JSON string.
173
	 *
174
	 * @param string $json JSON string.
175
	 * @return Notification
176
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
177
	 */
178 1
	public static function from_json( $json ) {
179 1
		$data = \json_decode( $json );
180
181 1
		$validator = new \JsonSchema\Validator();
182
183 1
		$validator->validate(
184 1
			$data,
185
			(object) array(
186 1
				'$ref' => 'file://' . \realpath( __DIR__ . '/../json-schemas/notification.json' ),
187
			),
188 1
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
189
		);
190
191 1
		return self::from_object( $data );
192
	}
193
}
194