Passed
Pull Request — master (#375)
by Brian
87:27
created

GetPaid_Form_Item::get_description()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
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
	|--------------------------------------------------------------------------
143
	| Setters
144
	|--------------------------------------------------------------------------
145
	|
146
	| Functions for setting order data. These should not update anything in the
147
	| database itself and should only change what is stored in the class
148
	| object.
149
    */
150
151
	/**
152
	 * Set the item qantity.
153
	 *
154
	 * @since 1.0.19
155
	 * @param  int $quantity The item quantity.
156
	 */
157
	public function set_quantity( $quantity ) {
158
159
		if ( empty( $quantity ) || ! is_numeric( $quantity ) ) {
160
			$quantity = 1;
161
		}
162
163
		$this->quantity = $quantity;
164
165
	}
166
167
	/**
168
	 * Set whether or not the quantities are allowed.
169
	 *
170
	 * @since 1.0.19
171
	 * @param  bool $allow_quantities
172
	 */
173
	public function set_allow_quantities( $allow_quantities ) {
174
		$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...
175
	}
176
177
	/**
178
	 * Set whether or not the item is required.
179
	 *
180
	 * @since 1.0.19
181
	 * @param  bool $is_required
182
	 */
183
	public function set_is_required( $is_required ) {
184
		$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...
185
	}
186
187
	/**
188
	 * Sets the custom item description.
189
	 *
190
	 * @since 1.0.19
191
	 * @param  string $description
192
	 */
193
	public function set_custom_description( $description ) {
194
		$this->custom_description = $description;
195
	}
196
197
    /**
198
     * We do not want to save items to the database.
199
     * 
200
	 * @return int item id
201
     */
202
    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

202
    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...
203
        return $this->get_id();
204
	}
205
206
    /*
207
	|--------------------------------------------------------------------------
208
	| Conditionals
209
	|--------------------------------------------------------------------------
210
	|
211
	| Checks if a condition is true or false.
212
	|
213
	*/
214
215
    /**
216
	 * Checks whether the item has enabled dynamic pricing.
217
	 *
218
	 * @since 1.0.19
219
	 * @return bool
220
	 */
221
	public function is_required() {
222
        return (bool) $this->get_is_required();
223
	}
224
225
}
226