Failed Conditions
Push — feature/post-pay ( 4d43ec...e3663d )
by Remco
04:52
created

src/OrderItems.php (2 issues)

Labels
Severity
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 );
0 ignored issues
show
The call to Pronamic\WordPress\Pay\G...rderItem::__construct() has too few arguments starting with quantity. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
			$order_item = /** @scrutinizer ignore-call */ new OrderItem( $order_item );

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
$order_item of type array is incompatible with the type string expected by parameter $name of Pronamic\WordPress\Pay\G...rderItem::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
			$order_item = new OrderItem( /** @scrutinizer ignore-type */ $order_item );
Loading history...
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 array|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'          => strval( $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;
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[] = strval( $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
			/*
131
			 * tax is an optional field, if it is not present, it is considered as a single empty field.
132
			 * If it is present, the amount and currency are added as two separate fields.
133
			 */
134
			$tax = $item->get_tax();
135
136
			if ( empty( $tax ) ) {
137
				$data[] = '';
138
			} else {
139
				$data[] = $tax->get_currency();
140
				$data[] = $tax->get_amount();
141
			}
142
143
			// Required category.
144
			$data[] = $item->get_category();
145
146
			// Optional VAT category.
147
			$vat_category = $item->get_vat_category();
148
149
			if ( ! empty( $vat_category ) ) {
150
				$data[] = $vat_category;
151
			}
152
153
			var_dump( $data );
154
		}
155
156
		return $data;
157
	}
158
}
159