Failed Conditions
Push — feature/post-pay ( d719f9 )
by Reüel
08:19
created

OrderItems::add_item()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Order items.
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
/**
14
 * Order items.
15
 *
16
 * @author  Reüel van der Steege
17
 * @version 2.0.3
18
 * @since   2.0.3
19
 */
20
class OrderItems {
21
	/**
22
	 * Order items.
23
	 *
24
	 * @var array
25
	 */
26
	private $order_items;
27
28
	/**
29
	 * Construct order results message.
30
	 *
31
	 * @param array $items Order items.
32
	 */
33
	public function __construct( $items = null ) {
34
		if ( is_array( $items ) ) {
35
			foreach ( $items as $item ) {
36
				$this->add_item( $item );
37
			}
38
		}
39
	}
40
41
	/**
42
	 * Add order item.
43
	 *
44
	 * @param OrderItem|array $order_item Order item.
45
	 *
46
	 * @return void
47
	 */
48
	public function add_item( $order_item ) {
49
		if ( ! ( $order_item instanceof OrderItem ) ) {
50
			$order_item = new OrderItem( $order_item );
51
		}
52
53
		$this->order_items[] = $order_item;
54
	}
55
56
	/**
57
	 * Get order items.
58
	 *
59
	 * @return array
60
	 */
61
	public function get_order_items() {
62
		return $this->order_items;
63
	}
64
65
	/**
66
	 * Get JSON.
67
	 *
68
	 * @return object|null
69
	 */
70
	public function get_json() {
71
		$data = array();
72
73
		$items = $this->get_order_items();
74
75
		foreach ( $items as $item ) {
76
			$amount = $item->get_amount();
77
			$tax    = $item->get_tax();
78
79
			$item_data = array(
80
				'id'          => $item->get_id(),
81
				'name'        => $item->get_name(),
82
				'description' => $item->get_description(),
83
				'quantity'    => $item->get_quantity(),
84
				'amount'      => ( $amount instanceof Money ) ? $amount->get_json() : null,
85
				'tax'         => ( $tax instanceof Money ) ? $tax->get_json() : null,
86
				'category'    => $item->get_category(),
87
				'vatCategory' => $item->get_vat_category(),
88
			);
89
90
			$item_data = array_filter( $item_data );
91
92
			if ( ! empty( $item_data ) ) {
93
				$data[] = (object) $item_data;
94
			}
95
		}
96
97
		if ( empty( $data ) ) {
98
			return null;
99
		}
100
101
		return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data returns the type array|array<mixed,object> which is incompatible with the documented return type object|null.
Loading history...
102
	}
103
104
	/**
105
	 * Get signature data.
106
	 *
107
	 * @return array
108
	 */
109
	public function get_signature_data() {
110
		$data = array();
111
112
		$order_items = $this->get_order_items();
113
114
		foreach ( $order_items as $item ) {
115
			// Optional ID.
116
			$item_id = $item->get_id();
117
118
			if ( ! empty( $item_id ) ) {
119
				$data[] = $item_id;
120
			}
121
122
			// Required fields.
123
			$data[] = $item->get_name();
124
			$data[] = $item->get_description();
125
			$data[] = $item->get_quantity();
126
			$data[] = $item->get_amount()->get_currency();
127
			$data[] = $item->get_amount()->get_amount();
128
129
			// Required tax field.
130
			$tax = $item->get_tax();
131
132
			if ( empty( $tax ) ) {
133
				$data[] = $tax;
134
			} else {
135
				$data[] = $tax->get_currency();
136
				$data[] = $tax->get_amount();
137
			}
138
139
			// Required category.
140
			$data[] = $item->get_category();
141
142
			// Optional VAT category.
143
			$vat_category = $item->get_vat_category();
144
145
			if ( ! empty( $vat_category ) ) {
146
				$data[] = $vat_category;
147
			}
148
		}
149
150
		return $data;
151
	}
152
}
153