Failed Conditions
Push — master ( e22298...bcbd11 )
by Reüel
10:06 queued 11s
created

OrderResults::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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