Passed
Push — master ( 35b47c...3bc955 )
by Brian
06:36
created

__construct()   D

Complexity

Conditions 18
Paths 21

Size

Total Lines 73
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 39
nc 21
nop 1
dl 0
loc 73
rs 4.8666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		$invoice      = $submission->get_invoice();
31
		$force_prices = array();
32
33
		// Prepare the selected items.
34
		$selected_items = array();
35
		if ( ! empty( $data['getpaid-items'] ) ) {
36
			$selected_items = wpinv_clean( $data['getpaid-items'] );
37
38
			if ( ! empty( $invoice ) && $submission->is_initial_fetch() ) {
39
				foreach ( $invoice->get_items() as $invoice_item ) {
40
					if ( isset( $selected_items[ $invoice_item->get_id() ] ) ) {
41
						$selected_items[ $invoice_item->get_id() ]['quantity'] = $invoice_item->get_quantity();
42
						$selected_items[ $invoice_item->get_id() ]['price']    = $invoice_item->get_price();
43
44
						$force_prices[ $invoice_item->get_id() ] = $invoice_item->get_price();
45
					}
46
				}
47
			}
48
		}
49
50
		// (Maybe) set form items.
51
		if ( isset( $data['getpaid-form-items'] ) ) {
52
53
			// Confirm items key.
54
			$form_items = wpinv_clean( $data['getpaid-form-items'] );
55
			if ( ! isset( $data['getpaid-form-items-key'] ) || md5( NONCE_KEY . AUTH_KEY . $form_items ) !== $data['getpaid-form-items-key'] ) {
0 ignored issues
show
Bug introduced by
Are you sure $form_items of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

55
			if ( ! isset( $data['getpaid-form-items-key'] ) || md5( NONCE_KEY . AUTH_KEY . /** @scrutinizer ignore-type */ $form_items ) !== $data['getpaid-form-items-key'] ) {
Loading history...
56
				throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) );
57
			}
58
59
			$items    = array();
60
            $item_ids = array();
61
62
            foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) {
63
                if ( ! in_array( $item_id, $item_ids ) ) {
64
                    $item = new GetPaid_Form_Item( $item_id );
65
                    $item->set_quantity( $qty );
66
67
                    if ( empty( $qty ) ) {
68
                        $item->set_allow_quantities( true );
69
                        $item->set_is_required( false );
70
                    }
71
72
					if ( ! $item->user_can_set_their_price() && isset( $force_prices[ $item_id ] ) ) {
73
						$item->set_is_dynamic_pricing( true );
74
						$item->set_minimum_price( 0 );
75
					}
76
77
                    $item_ids[] = $item->get_id();
78
                    $items[]    = $item;
79
                }
80
            }
81
82
            if ( ! $payment_form->is_default() ) {
83
84
                foreach ( $payment_form->get_items() as $item ) {
85
                    if ( ! in_array( $item->get_id(), $item_ids ) ) {
86
                        $item_ids[] = $item->get_id();
87
                        $items[]    = $item;
88
                    }
89
                }
90
			}
91
92
            $payment_form->set_items( $items );
93
94
		}
95
96
		// Process each individual item.
97
		foreach ( $payment_form->get_items() as $item ) {
98
			$this->process_item( $item, $selected_items, $submission );
99
		}
100
101
	}
102
103
	/**
104
	 * Process a single item.
105
	 *
106
	 * @param GetPaid_Form_Item $item
107
	 * @param array $selected_items
108
	 * @param GetPaid_Payment_Form_Submission $submission
109
	 */
110
	public function process_item( $item, $selected_items, $submission ) {
111
112
		// Abort if this is an optional item and it has not been selected.
113
		if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) {
114
			return;
115
		}
116
117
		// (maybe) let customers change the quantities and prices.
118
		if ( isset( $selected_items[ $item->get_id() ] ) ) {
119
120
			// Maybe change the quantities.
121
			if ( $item->allows_quantities() ) {
122
				$item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] );
123
			}
124
125
			// Maybe change the price.
126
			if ( $item->user_can_set_their_price() ) {
127
				$price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] );
128
129
				if ( $item->get_minimum_price() > $price ) {
130
					throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) );
131
				}
132
133
				$item->set_price( $price );
134
135
			}
136
		}
137
138
		if ( 0 == $item->get_quantity() ) {
139
			return;
140
		}
141
142
		// Save the item.
143
		$this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item', $item, $submission );
144
145
	}
146
147
}
148