Passed
Push — master ( b3b246...bcec86 )
by Brian
05:30
created

GetPaid_Payment_Form_Submission_Fees   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 37
c 1
b 0
f 0
dl 0
loc 114
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process_price_input() 0 18 3
A set_error() 0 3 2
A process_price_select() 0 24 4
A __construct() 0 19 5
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 );
47
			}
48
49
			if ( 'price_select' == $element['type'] ) {
50
				$this->process_price_select( $element, $data );
51
			}
52
53
		}
54
55
	}
56
57
	/**
58
	 * Process a price input field.
59
	 *
60
	 * @param array $element
61
	 * @param array $data
62
	 */
63
	public function process_price_input( $element, $data ) {
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 = (float) wpinv_sanitize_amount( $element['minimum'] );
72
73
		if ( $amount < $minimum ) {
74
			return $this->set_error( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), $minimum ) );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_error(sprintf...invoicing'), $minimum)) targeting GetPaid_Payment_Form_Submission_Fees::set_error() 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...
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 = wpinv_parse_list( $data[ $element['id'] ] );
100
		$total    = 0;
101
102
		foreach ( $selected as $price ) {
103
104
			if ( ! isset( $options[ $price ] ) ) {
105
				return $this->set_error( __( 'You have selected an invalid amount', 'invoicing' ) );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_error(__('You... amount', 'invoicing')) targeting GetPaid_Payment_Form_Submission_Fees::set_error() 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...
106
			}
107
108
			$total += (float) wpinv_sanitize_amount( $price );
109
		}
110
111
		$this['fees'][ $element['label'] ] = array(
112
			'name'          => $element['label'],
113
			'initial_fee'   => $total,
114
			'recurring_fee' => 0,
115
		);
116
117
	}
118
119
	/**
120
	 * Sets an error without overwriting the previous error.
121
	 *
122
	 * @param string $error
123
	 */
124
	public function set_error( $error ) {
125
		if ( empty( $this->fee_error ) ) {
126
			$this->fee_error    = $error;
127
		}
128
	}
129
130
}
131