Passed
Push — master ( c6d0db...756836 )
by Brian
04:55 queued 28s
created

GetPaid_Payment_Form_Submission_Items   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 13
eloc 23
dl 0
loc 74
rs 10
c 1
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 6
B process_item() 0 31 7
1
<?php
2
/**
3
 * Processes items for a payment form submission.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Payment form submission itemss class
11
 *
12
 */
13
class GetPaid_Payment_Form_Submission_Items {
14
15
	/**
16
	 * Submission items.
17
	 * @var GetPaid_Form_Item[]
18
	 */
19
	public $items = array();
20
21
    /**
22
	 * Class constructor
23
	 *
24
	 * @param GetPaid_Payment_Form_Submission $submission
25
	 */
26
	public function __construct( $submission ) {
27
28
		$data         = $submission->get_data();
29
		$payment_form = $submission->get_payment_form();
30
31
		// Prepare the selected items.
32
		$selected_items = array();
33
		if ( ! empty( $data['getpaid-items'] ) ) {
34
			$selected_items = wpinv_clean( $data['getpaid-items'] );
35
		}
36
37
		// For default forms, ensure that an item has been set.
38
		if ( $payment_form->is_default() && ! $submission->has_invoice() && isset( $data['getpaid-form-items'] ) ) {
39
			$form_items = wpinv_clean( $data['getpaid-form-items'] );
40
			$payment_form->set_items( getpaid_convert_items_to_array( $form_items ) );
41
		}
42
43
		// Process each individual item.
44
		foreach ( $payment_form->get_items() as $item ) {
45
			$this->process_item( $item, $selected_items );
0 ignored issues
show
Bug introduced by
It seems like $selected_items can also be of type string; however, parameter $selected_items of GetPaid_Payment_Form_Sub...n_Items::process_item() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

45
			$this->process_item( $item, /** @scrutinizer ignore-type */ $selected_items );
Loading history...
46
		}
47
48
	}
49
50
	/**
51
	 * Process a single item.
52
	 *
53
	 * @param GetPaid_Form_Item $item
54
	 * @param array $selected_items
55
	 */
56
	public function process_item( $item, $selected_items ) {
57
58
		// Abort if this is an optional item and it has not been selected.
59
		if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) {
60
			return;
61
		}
62
63
		// (maybe) let customers change the quantities and prices.
64
		if ( isset( $selected_items[ $item->get_id() ] ) ) {
65
66
			// Maybe change the quantities.
67
			if ( $item->allows_quantities() ) {
68
				$item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] );
69
			}
70
71
			// Maybe change the price.
72
			if ( $item->user_can_set_their_price() ) {
73
				$price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] );
74
75
				if ( $item->get_minimum_price() > $price ) {
76
					throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), wpinv_sanitize_amount( $item->get_minimum_price() ) ) );
77
				}
78
79
				$item->set_price( $price );
80
81
			}
82
83
		}
84
85
		// Save the item.
86
		$this->items[] = $item;
87
88
	}
89
90
}
91