Passed
Pull Request — master (#375)
by Brian
85:55 queued 10s
created

GetPaid_Form_Item::set_allow_quantities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Form Item Class
8
 *
9
 */
10
class GetPaid_Form_Item  extends WPInv_Item {
11
12
    /**
13
	 * Stores a custom description for the item.
14
	 *
15
	 * @var string
16
	 */
17
	protected $custom_description = '';
18
19
	/**
20
	 * Stores the item quantity.
21
	 *
22
	 * @var int
23
	 */
24
	protected $quantity = 1;
25
26
	/**
27
	 * Is this item required?
28
	 *
29
	 * @var int
30
	 */
31
	protected $is_required = true;
32
33
	/**
34
	 * Are quantities allowed?
35
	 *
36
	 * @var int
37
	 */
38
	protected $allow_quantities = false;
39
40
    /*
41
	|--------------------------------------------------------------------------
42
	| CRUD methods
43
	|--------------------------------------------------------------------------
44
	|
45
	| Methods which create, read, update and delete items from the object.
46
	|
47
    */
48
49
    /*
50
	|--------------------------------------------------------------------------
51
	| Getters
52
	|--------------------------------------------------------------------------
53
    */
54
55
    /**
56
	 * Get the item name.
57
	 *
58
	 * @since 1.0.19
59
	 * @param  string $context View or edit context.
60
	 * @return string
61
	 */
62
	public function get_name( $context = 'view' ) {
63
		$name = parent::get_name( $context );
64
		return $name . wpinv_get_item_suffix( $this );
65
	}
66
67
	/**
68
	 * Get the item description.
69
	 *
70
	 * @since 1.0.19
71
	 * @param  string $context View or edit context.
72
	 * @return string
73
	 */
74
	public function get_description( $context = 'view' ) {
75
76
		if ( ! empty( $this->custom_description ) ) {
77
			return $this->custom_description;
78
		}
79
80
		return parent::get_description( $context );
81
	}
82
	
83
	/**
84
	 * Get the item qantity.
85
	 *
86
	 * @since 1.0.19
87
	 * @param  string $context View or edit context.
88
	 * @return int
89
	 */
90
	public function get_qantity( $context = 'view' ) {
91
		$quantity = (int) $this->quantity;
92
93
		if ( empty( $quantity ) || 1 > $quantity ) {
94
			$quantity = 1;
95
		}
96
97
		if ( 'view' == $context ) {
98
			return apply_filters( 'getpaid_payment_form_item_quanity', $quantity, $this );
99
		}
100
101
		return $quantity;
102
103
	}
104
105
	/**
106
	 * Returns whether or not customers can update the item quantity.
107
	 *
108
	 * @since 1.0.19
109
	 * @param  string $context View or edit context.
110
	 * @return bool
111
	 */
112
	public function get_allow_quantities( $context = 'view' ) {
113
		$allow_quantities = (bool) $this->allow_quantities;
114
115
		if ( 'view' == $context ) {
116
			return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this );
117
		}
118
119
		return $allow_quantities;
120
121
	}
122
123
	/**
124
	 * Returns whether or not the item is required.
125
	 *
126
	 * @since 1.0.19
127
	 * @param  string $context View or edit context.
128
	 * @return bool
129
	 */
130
	public function get_is_required( $context = 'view' ) {
131
		$is_required = (bool) $this->is_required;
132
133
		if ( 'view' == $context ) {
134
			return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this );
135
		}
136
137
		return $is_required;
138
139
	}
140
141
	/**
142
	 * Prepares form data for use.
143
	 *
144
	 * @since 1.0.19
145
	 * @param  string $context View or edit context.
146
	 * @return array
147
	 */
148
	public function prepare_data_for_use() {
149
150
		return array(
151
			'title'            => sanitize_text_field( $this->get_name() ),
152
			'id'               => $this->get_id(),
153
			'price'            => $this->get_price(),
154
			'recurring'        => $this->is_recurring(),
155
			'description'      => $this->get_description(),
156
			'allow_quantities' => $this->get_allow_quantities(),
157
        );
158
	}
159
160
    /*
161
	|--------------------------------------------------------------------------
162
	| Setters
163
	|--------------------------------------------------------------------------
164
	|
165
	| Functions for setting order data. These should not update anything in the
166
	| database itself and should only change what is stored in the class
167
	| object.
168
    */
169
170
	/**
171
	 * Set the item qantity.
172
	 *
173
	 * @since 1.0.19
174
	 * @param  int $quantity The item quantity.
175
	 */
176
	public function set_quantity( $quantity ) {
177
178
		if ( empty( $quantity ) || ! is_numeric( $quantity ) ) {
179
			$quantity = 1;
180
		}
181
182
		$this->quantity = $quantity;
183
184
	}
185
186
	/**
187
	 * Set whether or not the quantities are allowed.
188
	 *
189
	 * @since 1.0.19
190
	 * @param  bool $allow_quantities
191
	 */
192
	public function set_allow_quantities( $allow_quantities ) {
193
		$this->allow_quantities = (bool) $allow_quantities;
0 ignored issues
show
Documentation Bug introduced by
The property $allow_quantities was declared of type integer, but (bool)$allow_quantities is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
194
	}
195
196
	/**
197
	 * Set whether or not the item is required.
198
	 *
199
	 * @since 1.0.19
200
	 * @param  bool $is_required
201
	 */
202
	public function set_is_required( $is_required ) {
203
		$this->is_required = (bool) $is_required;
0 ignored issues
show
Documentation Bug introduced by
The property $is_required was declared of type integer, but (bool)$is_required is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
204
	}
205
206
	/**
207
	 * Sets the custom item description.
208
	 *
209
	 * @since 1.0.19
210
	 * @param  string $description
211
	 */
212
	public function set_custom_description( $description ) {
213
		$this->custom_description = $description;
214
	}
215
216
    /**
217
     * We do not want to save items to the database.
218
     * 
219
	 * @return int item id
220
     */
221
    public function save( $data = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

221
    public function save( /** @scrutinizer ignore-unused */ $data = array() ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
222
        return $this->get_id();
223
	}
224
225
    /*
226
	|--------------------------------------------------------------------------
227
	| Conditionals
228
	|--------------------------------------------------------------------------
229
	|
230
	| Checks if a condition is true or false.
231
	|
232
	*/
233
234
    /**
235
	 * Checks whether the item has enabled dynamic pricing.
236
	 *
237
	 * @since 1.0.19
238
	 * @return bool
239
	 */
240
	public function is_required() {
241
        return (bool) $this->get_is_required();
242
	}
243
244
}
245