Passed
Pull Request — master (#375)
by Brian
63:08
created

GetPaid_Form_Item::get_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
	 * Returns the sub total.
85
	 *
86
	 * @since 1.0.19
87
	 * @param  string $context View or edit context.
88
	 * @return int
89
	 */
90
	public function get_sub_total( $context = 'view' ) {
91
		return $this->get_qantity( $context ) * $this->get_price( $context );
92
	}
93
94
	/**
95
	 * Get the item qantity.
96
	 *
97
	 * @since 1.0.19
98
	 * @param  string $context View or edit context.
99
	 * @return int
100
	 */
101
	public function get_qantity( $context = 'view' ) {
102
		$quantity = (int) $this->quantity;
103
104
		if ( empty( $quantity ) || 1 > $quantity ) {
105
			$quantity = 1;
106
		}
107
108
		if ( 'view' == $context ) {
109
			return apply_filters( 'getpaid_payment_form_item_quanity', $quantity, $this );
110
		}
111
112
		return $quantity;
113
114
	}
115
116
	/**
117
	 * Returns whether or not customers can update the item quantity.
118
	 *
119
	 * @since 1.0.19
120
	 * @param  string $context View or edit context.
121
	 * @return bool
122
	 */
123
	public function get_allow_quantities( $context = 'view' ) {
124
		$allow_quantities = (bool) $this->allow_quantities;
125
126
		if ( 'view' == $context ) {
127
			return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this );
128
		}
129
130
		return $allow_quantities;
131
132
	}
133
134
	/**
135
	 * Returns whether or not the item is required.
136
	 *
137
	 * @since 1.0.19
138
	 * @param  string $context View or edit context.
139
	 * @return bool
140
	 */
141
	public function get_is_required( $context = 'view' ) {
142
		$is_required = (bool) $this->is_required;
143
144
		if ( 'view' == $context ) {
145
			return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this );
146
		}
147
148
		return $is_required;
149
150
	}
151
152
	/**
153
	 * Prepares form data for use.
154
	 *
155
	 * @since 1.0.19
156
	 * @param  string $context View or edit context.
157
	 * @return array
158
	 */
159
	public function prepare_data_for_use() {
160
161
		return array(
162
			'title'            => sanitize_text_field( $this->get_name() ),
163
			'id'               => $this->get_id(),
164
			'price'            => $this->get_price(),
165
			'recurring'        => $this->is_recurring(),
166
			'description'      => $this->get_description(),
167
			'allow_quantities' => $this->allows_quantities(),
168
			'required'         => $this->is_required(),
169
        );
170
	}
171
172
    /*
173
	|--------------------------------------------------------------------------
174
	| Setters
175
	|--------------------------------------------------------------------------
176
	|
177
	| Functions for setting order data. These should not update anything in the
178
	| database itself and should only change what is stored in the class
179
	| object.
180
    */
181
182
	/**
183
	 * Set the item qantity.
184
	 *
185
	 * @since 1.0.19
186
	 * @param  int $quantity The item quantity.
187
	 */
188
	public function set_quantity( $quantity ) {
189
190
		if ( empty( $quantity ) || ! is_numeric( $quantity ) ) {
191
			$quantity = 1;
192
		}
193
194
		$this->quantity = $quantity;
195
196
	}
197
198
	/**
199
	 * Set whether or not the quantities are allowed.
200
	 *
201
	 * @since 1.0.19
202
	 * @param  bool $allow_quantities
203
	 */
204
	public function set_allow_quantities( $allow_quantities ) {
205
		$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...
206
	}
207
208
	/**
209
	 * Set whether or not the item is required.
210
	 *
211
	 * @since 1.0.19
212
	 * @param  bool $is_required
213
	 */
214
	public function set_is_required( $is_required ) {
215
		$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...
216
	}
217
218
	/**
219
	 * Sets the custom item description.
220
	 *
221
	 * @since 1.0.19
222
	 * @param  string $description
223
	 */
224
	public function set_custom_description( $description ) {
225
		$this->custom_description = $description;
226
	}
227
228
    /**
229
     * We do not want to save items to the database.
230
     * 
231
	 * @return int item id
232
     */
233
    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

233
    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...
234
        return $this->get_id();
235
	}
236
237
    /*
238
	|--------------------------------------------------------------------------
239
	| Conditionals
240
	|--------------------------------------------------------------------------
241
	|
242
	| Checks if a condition is true or false.
243
	|
244
	*/
245
246
    /**
247
	 * Checks whether the item has enabled dynamic pricing.
248
	 *
249
	 * @since 1.0.19
250
	 * @return bool
251
	 */
252
	public function is_required() {
253
        return (bool) $this->get_is_required();
254
	}
255
256
	/**
257
	 * Checks whether users can edit the quantities.
258
	 *
259
	 * @since 1.0.19
260
	 * @return bool
261
	 */
262
	public function allows_quantities() {
263
        return (bool) $this->get_allow_quantities();
264
	}
265
266
}
267