OrderResults::from_json()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Order results
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
 * Order results.
15
 *
16
 * @author  Remco Tolsma
17
 * @version 2.1.10
18
 * @since   1.0.0
19
 * @implements \IteratorAggregate<int, OrderResult>
20
 */
21
class OrderResults extends ResponseMessage implements \IteratorAggregate {
22
	/**
23
	 * More order results available flag.
24
	 *
25
	 * @var bool
26
	 */
27
	private $more_available;
28
29
	/**
30
	 * Order results.
31
	 *
32
	 * @var array<OrderResult>
33
	 */
34
	private $order_results;
35
36
	/**
37
	 * Construct order results message.
38
	 *
39
	 * @param bool               $more_available True if more order results available, false otherwise.
40
	 * @param array<OrderResult> $order_results  Order results.
41
	 * @param string             $signature      Signature.
42
	 */
43 1
	public function __construct( $more_available, array $order_results, $signature ) {
44 1
		parent::__construct( $signature );
45
46 1
		$this->more_available = $more_available;
47 1
		$this->order_results  = $order_results;
48 1
	}
49
50
	/**
51
	 * More available.
52
	 *
53
	 * @return bool True if more order results available, false otherwise.
54
	 */
55 1
	public function more_available() {
56 1
		return $this->more_available;
57
	}
58
59
	/**
60
	 * Get signature data.
61
	 *
62
	 * @return array<string>
63
	 */
64 1
	public function get_signature_fields() {
65 1
		$fields = array();
66
67 1
		$fields[] = $this->more_available() ? 'true' : 'false';
68
69 1
		foreach ( $this->order_results as $order_result ) {
70 1
			$fields[] = $order_result->get_merchant_order_id();
71 1
			$fields[] = $order_result->get_omnikassa_order_id();
72 1
			$fields[] = \strval( $order_result->get_poi_id() );
73 1
			$fields[] = $order_result->get_order_status();
74 1
			$fields[] = $order_result->get_order_status_datetime();
75 1
			$fields[] = $order_result->get_error_code();
76
77 1
			$fields = $order_result->get_paid_amount()->get_signature_fields( $fields );
78 1
			$fields = $order_result->get_total_amount()->get_signature_fields( $fields );
79
		}
80
81 1
		return $fields;
82
	}
83
84
	/**
85
	 * Get iterator.
86
	 *
87
	 * @return \ArrayIterator<int, OrderResult>
88
	 */
89 1
	public function getIterator() {
90 1
		return new \ArrayIterator( $this->order_results );
91
	}
92
93
	/**
94
	 * Create order results from object.
95
	 *
96
	 * @param object $object Object.
97
	 * @return OrderResults
98
	 * @throws \InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
99
	 */
100 1
	public static function from_object( $object ) {
101 1
		if ( ! isset( $object->signature ) ) {
102
			throw new \InvalidArgumentException( 'Object must contain `signature` property.' );
103
		}
104
105 1
		if ( ! isset( $object->moreOrderResultsAvailable ) ) {
106
			throw new \InvalidArgumentException( 'Object must contain `moreOrderResultsAvailable` property.' );
107
		}
108
109 1
		if ( ! isset( $object->orderResults ) ) {
110
			throw new \InvalidArgumentException( 'Object must contain `orderResults` property.' );
111
		}
112
113 1
		if ( ! \is_array( $object->orderResults ) ) {
114
			throw new \InvalidArgumentException( 'The `orderResults` property must be an array.' );
115
		}
116
117 1
		$order_results = array();
118
119 1
		foreach ( $object->orderResults as $o ) {
120 1
			$order_results[] = OrderResult::from_object( $o );
121
		}
122
123 1
		return new self( $object->moreOrderResultsAvailable, $order_results, $object->signature );
124
	}
125
126
	/**
127
	 * Create notification from JSON string.
128
	 *
129
	 * @param string $json JSON string.
130
	 * @return OrderResults
131
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
132
	 */
133 1
	public static function from_json( $json ) {
134 1
		$data = \json_decode( $json );
135
136 1
		$validator = new \JsonSchema\Validator();
137
138 1
		$validator->validate(
139 1
			$data,
140
			(object) array(
141 1
				'$ref' => 'file://' . \realpath( __DIR__ . '/../json-schemas/order-results.json' ),
142
			),
143 1
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
144
		);
145
146 1
		return self::from_object( $data );
147
	}
148
}
149