Test Failed
Push — feature/post-pay ( e3663d...906e88 )
by Remco
04:22
created

Notification::get_signature_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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