Passed
Pull Request — master (#785)
by
unknown
04:48
created

process_item()   D

Complexity

Conditions 19
Paths 42

Size

Total Lines 78
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 42
nc 42
nop 3
dl 0
loc 78
rs 4.5166
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 ( is_array( $submission->get_field( 'getpaid-variable-items' ) ) ) {
0 ignored issues
show
introduced by
The condition is_array($submission->ge...tpaid-variable-items')) is always false.
Loading history...
Bug introduced by
Are you sure the usage of $submission->get_field('getpaid-variable-items') targeting GetPaid_Payment_Form_Submission::get_field() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
39
                $selected_prices = $submission->get_field( 'getpaid-variable-items' );
40
41
                $selected_items = array_filter(
42
                    $selected_items,
43
                    function ( $item ) use ( $selected_prices ) {
44
                        return isset( $item['price_id'] ) && in_array( (int) $item['price_id'], $selected_prices );
45
                    }
46
                );
47
            }
48
49
			if ( ! empty( $invoice ) && $submission->is_initial_fetch() ) {
50
				foreach ( $invoice->get_items() as $invoice_item ) {
51
                    if ( ! $invoice_item->has_variable_pricing() && isset( $selected_items[ $invoice_item->get_id() ] ) ) {
52
                        $selected_items[ $invoice_item->get_id() ]['quantity'] = $invoice_item->get_quantity();
53
                        $selected_items[ $invoice_item->get_id() ]['price']    = $invoice_item->get_price();
54
55
                        $force_prices[ $invoice_item->get_id() ] = $invoice_item->get_price();
56
                    }
57
				}
58
			}
59
		}
60
61
		// (Maybe) set form items.
62
		if ( isset( $data['getpaid-form-items'] ) ) {
63
64
			// Confirm items key.
65
			$form_items = wpinv_clean( $data['getpaid-form-items'] );
66
			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

66
			if ( ! isset( $data['getpaid-form-items-key'] ) || md5( NONCE_KEY . AUTH_KEY . /** @scrutinizer ignore-type */ $form_items ) !== $data['getpaid-form-items-key'] ) {
Loading history...
67
				throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) );
68
			}
69
70
			$items    = array();
71
            $item_ids = array();
72
73
            foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) {
74
                if ( ! in_array( $item_id, $item_ids ) ) {
75
                    $item = new GetPaid_Form_Item( $item_id );
76
77
                    if ( ! $item->has_variable_pricing() ) {
78
                        $item->set_quantity( $qty );
79
80
                        if ( empty( $qty ) ) {
81
                            $item->set_allow_quantities( true );
82
                            $item->set_is_required( false );
83
                        }
84
85
                        if ( ! $item->user_can_set_their_price() && isset( $force_prices[ $item_id ] ) ) {
86
                            $item->set_is_dynamic_pricing( true );
87
                            $item->set_minimum_price( 0 );
88
                        }
89
                    }
90
91
                    $item_ids[] = $item->get_id();
92
                    $items[]    = $item;
93
                }
94
            }
95
96
            if ( ! $payment_form->is_default() ) {
97
98
                foreach ( $payment_form->get_items() as $item ) {
99
                    if ( ! in_array( $item->get_id(), $item_ids ) ) {
100
                        $item_ids[] = $item->get_id();
101
                        $items[]    = $item;
102
                    }
103
                }
104
			}
105
106
            $payment_form->set_items( $items );
107
		}
108
109
		// Process each individual item.
110
		foreach ( $payment_form->get_items() as $item ) {
111
			$this->process_item( $item, $selected_items, $submission );
112
		}
113
	}
114
115
	/**
116
	 * Process a single item.
117
	 *
118
	 * @param GetPaid_Form_Item $item
119
	 * @param array $selected_items
120
	 * @param GetPaid_Payment_Form_Submission $submission
121
	 */
122
	public function process_item( $item, $selected_items, $submission ) {
123
124
        if ( $item->has_variable_pricing() ) {
125
126
            $selected_items = array_filter(
127
                $selected_items,
128
                function ( $selected_item ) use ( $item ) {
129
                    return (int) $selected_item['item_id'] === (int) $item->get_id();
130
                }
131
            );
132
133
            if ( ! $item->is_required() && ! count( $selected_items ) ) {
134
                return;
135
            }
136
137
            $price_options = $item->get_variable_prices();
138
            $price = current( $selected_items );
139
140
            $item->set_price_id( $price['price_id'] );
141
            $item->set_quantity( $price['quantity'] );
142
143
            $price = isset( $price_options[ $price['price_id'] ] ) ? $price_options[ $price['price_id'] ] : $price;
144
            $item->set_price( (float) $price['amount'] );
145
146
            if ( isset( $price['is-recurring'] ) && 'yes' === $price['is-recurring'] ) {
147
                if ( isset( $price['trial-interval'], $price['trial-period'] ) && $price['trial-interval'] > 0 ) {
148
                    $trial_interval = (int) $price['trial-interval'];
149
                    $trial_period = $price['trial-period'];
150
151
                    $item->set_is_free_trial( 1 );
152
                    $item->set_trial_interval( $trial_interval );
153
                    $item->set_trial_period( $trial_period );
154
                }
155
156
                if ( isset( $price['recurring-interval'], $price['recurring-period'] ) && $price['recurring-interval'] > 0 ) {
157
                    $recurring_interval = (int) $price['recurring-interval'];
158
                    $recurring_period = $price['recurring-period'];
159
                    $recurring_limit = isset( $price['recurring-limit'] ) ? (int) $price['recurring-limit'] : 0;
160
161
                    $item->set_is_recurring( 1 );
162
                    $item->set_recurring_interval( $recurring_interval );
163
                    $item->set_recurring_period( $recurring_period );
164
                    $item->set_recurring_limit( $recurring_limit );
165
                }
166
            }
167
        } else {
168
		    // Abort if this is an optional item and it has not been selected.
169
            if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) {
170
                return;
171
            }
172
173
            // (maybe) let customers change the quantities and prices.
174
            if ( isset( $selected_items[ $item->get_id() ] ) ) {
175
176
                // Maybe change the quantities.
177
                if ( $item->allows_quantities() ) {
178
                    $item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] );
179
                }
180
181
                // Maybe change the price.
182
                if ( $item->user_can_set_their_price() ) {
183
                    $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] );
184
185
                    if ( $item->get_minimum_price() > $price ) {
186
                        throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) );
187
                    }
188
189
                    $item->set_price( $price );
190
                }
191
            }
192
        }
193
194
		if ( 0 == $item->get_quantity() ) {
195
			return;
196
		}
197
198
		// Save the item.
199
		$this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item', $item, $submission );
200
	}
201
}
202