Passed
Push — master ( 84a4a3...6ac4b2 )
by Brian
04:53
created

__construct()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 56
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 12
eloc 28
c 1
b 1
f 0
nc 14
nop 1
dl 0
loc 56
rs 6.9666

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
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
		// (Maybe) set form items.
38
		if ( isset( $data['getpaid-form-items'] ) ) {
39
40
			// Confirm items key.
41
			$form_items = wpinv_clean( $data['getpaid-form-items'] );
42
			if ( ! isset( $data['getpaid-form-items-key'] ) || $data['getpaid-form-items-key'] !== md5( NONCE_KEY . AUTH_KEY . $form_items ) ) {
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

42
			if ( ! isset( $data['getpaid-form-items-key'] ) || $data['getpaid-form-items-key'] !== md5( NONCE_KEY . AUTH_KEY . /** @scrutinizer ignore-type */ $form_items ) ) {
Loading history...
43
				throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) );
44
			}
45
46
			$items    = array();
47
            $item_ids = array();
48
49
            foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) {
50
                if ( ! in_array( $item_id, $item_ids ) ) {
51
                    $item = new GetPaid_Form_Item( $item_id );
52
                    $item->set_quantity( $qty );
53
54
                    if ( 0 == $qty ) {
55
                        $item->set_allow_quantities( true );
56
                        $item->set_is_required( false );
57
                    }
58
59
                    $item_ids[] = $item->get_id();
60
                    $items[]    = $item;
61
                }
62
            }
63
64
            if ( ! $payment_form->is_default() ) {
65
66
                foreach ( $payment_form->get_items() as $item ) {
67
                    if ( ! in_array( $item->get_id(), $item_ids ) ) {
68
                        $item_ids[] = $item->get_id();
69
                        $items[]    = $item;
70
                    }
71
                }
72
73
            }
74
75
            $payment_form->set_items( $items );
76
77
		}
78
79
		// Process each individual item.
80
		foreach ( $payment_form->get_items() as $item ) {
81
			$this->process_item( $item, $selected_items, $submission );
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

81
			$this->process_item( $item, /** @scrutinizer ignore-type */ $selected_items, $submission );
Loading history...
82
		}
83
84
	}
85
86
	/**
87
	 * Process a single item.
88
	 *
89
	 * @param GetPaid_Form_Item $item
90
	 * @param array $selected_items
91
	 * @param GetPaid_Payment_Form_Submission $submission
92
	 */
93
	public function process_item( $item, $selected_items, $submission ) {
94
95
		// Abort if this is an optional item and it has not been selected.
96
		if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) {
97
			return;
98
		}
99
100
		// (maybe) let customers change the quantities and prices.
101
		if ( isset( $selected_items[ $item->get_id() ] ) ) {
102
103
			// Maybe change the quantities.
104
			if ( $item->allows_quantities() ) {
105
				$item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] );
106
			}
107
108
			// Maybe change the price.
109
			if ( $item->user_can_set_their_price() ) {
110
				$price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] );
111
112
				if ( $item->get_minimum_price() > $price ) {
113
					throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) );
114
				}
115
116
				$item->set_price( $price );
117
118
			}
119
120
		}
121
122
		if ( 0 == $item->get_quantity() ) {
123
			return;
124
		}
125
126
		// Save the item.
127
		$this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item' , $item, $submission );
128
129
	}
130
131
}
132