Failed Conditions
Push — master ( e6b844...0f20c1 )
by Reüel
09:09 queued 12s
created

OrderResult::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 2
b 0
f 0
nc 1
nop 8
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 10

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