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

OrderResults   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getIterator() 0 2 1
A from_object() 0 27 6
A from_json() 0 10 1
A get_signature_data() 0 19 3
A more_available() 0 2 1
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.0.2
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
	public function __construct( $more_available, array $order_results, $signature ) {
51
		parent::__construct( $signature );
52
53
		$this->more_available = $more_available;
54
		$this->order_results  = $order_results;
55
	}
56
57
	/**
58
	 * More available.
59
	 *
60
	 * @return bool True if more order results available, false oterwise.
61
	 */
62
	public function more_available() {
63
		return $this->more_available;
64
	}
65
66
	/**
67
	 * Get signature data.
68
	 *
69
	 * @return array
70
	 */
71
	public function get_signature_data() {
72
		$data = array();
73
74
		$data[] = $this->more_available() ? 'true' : 'false';
75
76
		foreach ( $this->order_results as $order_result ) {
77
			$data[] = $order_result->get_merchant_order_id();
78
			$data[] = $order_result->get_omnikassa_order_id();
79
			$data[] = $order_result->get_poi_id();
80
			$data[] = $order_result->get_order_status();
81
			$data[] = $order_result->get_order_status_datetime();
82
			$data[] = $order_result->get_error_code();
83
			$data[] = $order_result->get_paid_amount()->get_currency();
84
			$data[] = $order_result->get_paid_amount()->get_amount();
85
			$data[] = $order_result->get_total_amount()->get_currency();
86
			$data[] = $order_result->get_total_amount()->get_amount();
87
		}
88
89
		return $data;
90
	}
91
92
	/**
93
	 * Get iterator.
94
	 *
95
	 * @return ArrayIterator
96
	 */
97
	public function getIterator() {
98
		return new ArrayIterator( $this->order_results );
99
	}
100
101
	/**
102
	 * Create order results from object.
103
	 *
104
	 * @param stdClass $object Object.
105
	 * @return OrderResults
106
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
107
	 */
108
	public static function from_object( stdClass $object ) {
109
		if ( ! isset( $object->signature ) ) {
110
			throw new InvalidArgumentException( 'Object must contain `signature` property.' );
111
		}
112
113
		if ( ! isset( $object->moreOrderResultsAvailable ) ) {
114
			throw new InvalidArgumentException( 'Object must contain `moreOrderResultsAvailable` property.' );
115
		}
116
117
		if ( ! isset( $object->orderResults ) ) {
118
			throw new InvalidArgumentException( 'Object must contain `orderResults` property.' );
119
		}
120
121
		if ( ! is_array( $object->orderResults ) ) {
122
			throw new InvalidArgumentException( 'The `orderResults` property must be an array.' );
123
		}
124
125
		$order_results = array();
126
127
		foreach ( $object->orderResults as $o ) {
128
			$order_results[] = OrderResult::from_object( $o );
129
		}
130
131
		return new self(
132
			$object->moreOrderResultsAvailable,
133
			$order_results,
134
			$object->signature
135
		);
136
	}
137
138
	/**
139
	 * Create notification from JSON string.
140
	 *
141
	 * @param string $json JSON string.
142
	 * @return Notification
143
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
144
	 */
145
	public static function from_json( $json ) {
146
		$data = json_decode( $json );
147
148
		$validator = new Validator();
149
150
		$validator->validate( $data, (object) array(
151
			'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/order-results.json' ),
152
		), Constraint::CHECK_MODE_EXCEPTIONS );
153
154
		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\OrderResults which is incompatible with the documented return type Pronamic\WordPress\Pay\G...OmniKassa2\Notification.
Loading history...
155
	}
156
}
157