Passed
Push — master ( 99a753...df0863 )
by Remco
19:34 queued 09:07
created

OrderResults::get_signature_fields()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

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