GetPaid_Payment_Form_Submission_Fees   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 5
A process_price_input() 0 18 4
A process_price_select() 0 29 4
1
<?php
2
/**
3
 * Processes fees for a payment form submission.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Payment form submission fees class
11
 *
12
 */
13
class GetPaid_Payment_Form_Submission_Fees {
14
15
	/**
16
	 * The fee validation error.
17
	 * @var string
18
	 */
19
	public $fee_error;
20
21
	/**
22
	 * Submission fees.
23
	 * @var array
24
	 */
25
	public $fees = array();
26
27
    /**
28
	 * Class constructor
29
	 *
30
	 * @param GetPaid_Payment_Form_Submission $submission
31
	 */
32
	public function __construct( $submission ) {
33
34
		// Process any existing invoice fees.
35
		if ( $submission->has_invoice() ) {
36
			$this->fees = $submission->get_invoice()->get_fees();
37
		}
38
39
		// Process price fields.
40
		$data         = $submission->get_data();
41
		$payment_form = $submission->get_payment_form();
42
43
		foreach ( $payment_form->get_elements() as $element ) {
44
45
			if ( 'price_input' == $element['type'] ) {
46
				$this->process_price_input( $element, $data, $submission );
47
			}
48
49
			if ( 'price_select' == $element['type'] ) {
50
				$this->process_price_select( $element, $data );
51
			}
52
}
53
54
	}
55
56
	/**
57
	 * Process a price input field.
58
	 *
59
	 * @param array $element
60
	 * @param array $data
61
	 * @param GetPaid_Payment_Form_Submission $submission
62
	 */
63
	public function process_price_input( $element, $data, $submission ) {
64
65
		// Abort if not passed.
66
		if ( empty( $data[ $element['id'] ] ) ) {
67
			return;
68
		}
69
70
		$amount  = (float) wpinv_sanitize_amount( $data[ $element['id'] ] );
71
		$minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] );
72
73
		if ( $amount < $minimum ) {
74
			throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $minimum, $submission->get_currency() ) ) );
0 ignored issues
show
Unused Code introduced by
The call to getpaid_unstandardize_amount() has too many arguments starting with $submission->get_currency(). ( Ignorable by Annotation )

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

74
			throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), /** @scrutinizer ignore-call */ getpaid_unstandardize_amount( $minimum, $submission->get_currency() ) ) );

This check compares calls to functions or methods with their respective definitions. If the call has more 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...
75
		}
76
77
		$this->fees[ $element['label'] ] = array(
78
			'name'          => $element['label'],
79
			'initial_fee'   => $amount,
80
			'recurring_fee' => 0,
81
		);
82
83
	}
84
85
	/**
86
	 * Process a price select field.
87
	 *
88
	 * @param array $element
89
	 * @param array $data
90
	 */
91
	public function process_price_select( $element, $data ) {
92
93
		// Abort if not passed.
94
		if ( empty( $data[ $element['id'] ] ) ) {
95
			return;
96
		}
97
98
		$options    = getpaid_convert_price_string_to_options( $element['options'] );
99
		$selected   = array_filter( array_map( 'trim', explode( ',', $data[ $element['id'] ] ) ) );
100
		$total      = 0;
101
		$sub_labels = array();
102
103
		foreach ( $selected as $price ) {
104
105
			if ( ! isset( $options[ $price ] ) ) {
106
				throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) );
107
			}
108
109
			$price = explode( '|', $price );
110
111
			$sub_labels[] = $price[0];
112
			$total += (float) wpinv_sanitize_amount( $price[1] );
113
		}
114
115
		$this->fees[ $element['label'] ] = array(
116
			'name'          => $element['label'],
117
			'initial_fee'   => $total,
118
			'recurring_fee' => 0,
119
			'description'   => implode( ', ', $sub_labels ),
120
		);
121
122
	}
123
124
}
125