Passed
Push — master ( b8e26c...2f113f )
by Remco
05:42
created

OrderResult::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 9
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Order result
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 ArrayIterator;
14
use InvalidArgumentException;
15
use IteratorAggregate;
16
use stdClass;
17
use JsonSchema\Constraints\Constraint;
18
use JsonSchema\Exception\ValidationException;
19
use JsonSchema\Validator;
20
21
/**
22
 * Order result
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.2
26
 * @since   2.0.2
27
 */
28
class OrderResult {
29
	/**
30
	 * OrderId as delivered during the Order Announce.
31
	 *
32
	 * @var string
33
	 */
34
	private $merchant_order_id;
35
36
	/**
37
	 * The unique id that the omnikassa has assigned to this order.
38
	 *
39
	 * @var string
40
	 */
41
	private $omnikassa_order_id;
42
43
	/**
44
	 * Unique identification of the webshop (point of interaction), seen from ROK.
45
	 * This is relevant if several webshops use the same webhook URL.
46
	 *
47
	 * @var int|string
48
	 */
49
	private $poi_id;
50
51
	/**
52
	 * The status of the order. See chapter "Consumer returns at the webshop" for an overview of the possible statuses.
53
	 *
54
	 * @var string
55
	 */
56
	private $order_status;
57
58
	/**
59
	 * The moment this status is reached.
60
	 *
61
	 * @var string
62
	 */
63
	private $order_status_datetime;
64
65
	/**
66
	 * Future field, for now: always empty.
67
	 *
68
	 * @var string
69
	 */
70
	private $error_code;
71
72
	/**
73
	 * Paid amount.
74
	 *
75
	 * @var Money
76
	 */
77
	private $paid_amount;
78
79
	/**
80
	 * Total amount.
81
	 *
82
	 * @var Money
83
	 */
84
	private $total_amount;
85
86
	/**
87
	 * Construct order result.
88
	 *
89
	 * @param string     $merchant_order_id     Merchant order ID.
90
	 * @param string     $omnikassa_order_id    OmniKassa order ID.
91
	 * @param int|string $poi_id                Point of interaction ID.
92
	 * @param string     $order_status          Order status.
93
	 * @param string     $order_status_datetime Order status datetime.
94
	 * @param string     $error_code            Error code.
95
	 * @param Money      $paid_amount           Paid amount.
96
	 * @param Money      $total_amount          Total amount.
97
	 */
98
	public function __construct( $merchant_order_id, $omnikassa_order_id, $poi_id, $order_status, $order_status_datetime, $error_code, Money $paid_amount, Money $total_amount ) {
99
		$this->merchant_order_id     = $merchant_order_id;
100
		$this->omnikassa_order_id    = $omnikassa_order_id;
101
		$this->poi_id                = $poi_id;
102
		$this->order_status          = $order_status;
103
		$this->order_status_datetime = $order_status_datetime;
104
		$this->error_code            = $error_code;
105
		$this->paid_amount           = $paid_amount;
106
		$this->total_amount          = $total_amount;
107
	}
108
109
	/**
110
	 * Get merchant order ID.
111
	 *
112
	 * @return string
113
	 */
114
	public function get_merchant_order_id() {
115
		return $this->merchant_order_id;
116
	}
117
118
	/**
119
	 * Get OmniKassa order ID.
120
	 *
121
	 * @return string
122
	 */
123
	public function get_omnikassa_order_id() {
124
		return $this->omnikassa_order_id;
125
	}
126
127
	/**
128
	 * Get point of interaction ID.
129
	 *
130
	 * @return string
131
	 */
132
	public function get_poi_id() {
133
		return $this->poi_id;
134
	}
135
136
	/**
137
	 * Get order status.
138
	 *
139
	 * @return string
140
	 */
141
	public function get_order_status() {
142
		return $this->order_status;
143
	}
144
145
	/**
146
	 * Get order status datetime.
147
	 *
148
	 * @return string
149
	 */
150
	public function get_order_status_datetime() {
151
		return $this->order_status_datetime;
152
	}
153
154
	/**
155
	 * Get error code.
156
	 *
157
	 * @return string
158
	 */
159
	public function get_error_code() {
160
		return $this->error_code;
161
	}
162
163
	/**
164
	 * Get paid amount.
165
	 *
166
	 * @return Money
167
	 */
168
	public function get_paid_amount() {
169
		return $this->paid_amount;
170
	}
171
172
	/**
173
	 * Get total amount.
174
	 *
175
	 * @return Money
176
	 */
177
	public function get_total_amount() {
178
		return $this->total_amount;
179
	}
180
181
	/**
182
	 * Get JSON.
183
	 *
184
	 * @return object
185
	 */
186
	public function get_json() {
187
		return (object) array(
188
			'merchantOrderId'     => $this->get_merchant_order_id(),
189
			'omnikassaOrderId'    => $this->get_omnikassa_order_id(),
190
			'poiId'               => $this->get_poi_id(),
191
			'orderStatus'         => $this->get_order_status(),
192
			'orderStatusDateTime' => $this->get_order_status_datetime(),
193
			'errorCode'           => $this->get_error_code(),
194
			'paidAmount'          => $this->get_paid_amount()->get_json(),
195
			'totalAmount'         => $this->get_total_amount()->get_json(),
196
		);
197
	}
198
199
	/**
200
	 * Create order result from object.
201
	 *
202
	 * @param stdClass $object Object.
203
	 * @return OrderResult
204
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
205
	 */
206
	public static function from_object( stdClass $object ) {
207
		if ( ! isset( $object->merchantOrderId ) ) {
208
			throw new InvalidArgumentException( 'Object must contain `merchantOrderId` property.' );
209
		}
210
211
		if ( ! isset( $object->omnikassaOrderId ) ) {
212
			throw new InvalidArgumentException( 'Object must contain `omnikassaOrderId` property.' );
213
		}
214
215
		if ( ! isset( $object->poiId ) ) {
216
			throw new InvalidArgumentException( 'Object must contain `poiId` property.' );
217
		}
218
219
		if ( ! isset( $object->orderStatus ) ) {
220
			throw new InvalidArgumentException( 'Object must contain `orderStatus` property.' );
221
		}
222
223
		if ( ! isset( $object->orderStatusDateTime ) ) {
224
			throw new InvalidArgumentException( 'Object must contain `orderStatusDateTime` property.' );
225
		}
226
227
		if ( ! isset( $object->errorCode ) ) {
228
			throw new InvalidArgumentException( 'Object must contain `errorCode` property.' );
229
		}
230
231
		if ( ! isset( $object->paidAmount ) ) {
232
			throw new InvalidArgumentException( 'Object must contain `paidAmount` property.' );
233
		}
234
235
		if ( ! isset( $object->totalAmount ) ) {
236
			throw new InvalidArgumentException( 'Object must contain `totalAmount` property.' );
237
		}
238
239
		return new self(
240
			$object->merchantOrderId,
241
			$object->omnikassaOrderId,
242
			$object->poiId,
243
			$object->orderStatus,
244
			$object->orderStatusDateTime,
245
			$object->errorCode,
246
			Money::from_object( $object->paidAmount ),
247
			Money::from_object( $object->totalAmount )
248
		);
249
	}
250
251
	/**
252
	 * Create notification from JSON string.
253
	 *
254
	 * @param string $json JSON string.
255
	 * @return Notification
256
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
257
	 */
258
	public static function from_json( $json ) {
259
		$data = json_decode( $json );
260
261
		$validator = new Validator();
262
263
		$validator->validate( $data, (object) array(
264
			'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/json-schema-order-result.json' ),
265
		), Constraint::CHECK_MODE_EXCEPTIONS );
266
267
		return self::from_object( $data );
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::from_object($data) returns the type Pronamic\WordPress\Pay\G...\OmniKassa2\OrderResult which is incompatible with the documented return type Pronamic\WordPress\Pay\G...OmniKassa2\Notification.
Loading history...
268
	}
269
}
270