Completed
Pull Request — master (#11889)
by Mike
12:12
created

WC_Item_Tax   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 180
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 2

16 Methods

Rating   Name   Duplication   Size   Complexity  
A is_compound() 0 3 1
B __get() 0 16 7
A set_rate_code() 0 3 1
A set_label() 0 3 1
A set_rate_id() 0 3 1
A set_tax_total() 0 3 1
A set_shipping_tax_total() 0 3 1
A set_compound() 0 3 1
A set_rate() 0 6 1
A get_type() 0 3 1
A get_rate_code() 0 3 1
A get_label() 0 3 2
A get_rate_id() 0 3 1
A get_tax_total() 0 3 1
A get_shipping_tax_total() 0 3 1
A get_compound() 0 3 1
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Line Item (tax).
8
 *
9
 * @version     2.7.0
10
 * @since       2.7.0
11
 * @package     WooCommerce/Classes
12
 * @author      WooThemes
13
 */
14
class WC_Item_Tax extends WC_Item {
15
16
	/**
17
	 * Data array.
18
	 * @since 2.7.0
19
	 * @var array
20
	 */
21
	protected $data = array(
22
		'rate_code'          => '',
23
		'rate_id'            => 0,
24
		'label'              => '',
25
		'compound'           => false,
26
		'tax_total'          => 0,
27
		'shipping_tax_total' => 0,
28
	);
29
30
	/**
31
	 * Is this a compound tax rate?
32
	 * @return boolean
33
	 */
34
	public function is_compound() {
35
		return $this->get_compound();
36
	}
37
38
	/**
39
	 * Backwards compatibility for directly accessed props.
40
	 * @param string $key
41
	 * @return mixed
42
	 */
43
	public function __get( $key ) {
44
		switch ( $key ) {
45
			case 'id' :
46
				return $this->get_id();
47
			case 'rate_id' :
48
				return $this->get_rate_id();
49
			case 'is_compound' :
50
				return $this->is_compound();
51
			case 'label' :
52
				return $this->get_label();
53
			case 'amount' :
54
				return wc_round_tax_total( $this->get_tax_total() + $this->get_shipping_tax_total() );
55
			case 'formatted_amount' :
56
				return wc_price( wc_round_tax_total( $this->get_tax_total() + $this->get_shipping_tax_total() ) );
57
		}
58
	}
59
60
	/*
61
	|--------------------------------------------------------------------------
62
	| Setters
63
	|--------------------------------------------------------------------------
64
	*/
65
66
	/**
67
	 * Set item name.
68
	 * @param string $value
69
	 * @throws WC_Data_Exception
70
	 */
71
	public function set_rate_code( $value ) {
72
		$this->data['rate_code'] = wc_clean( $value );
73
	}
74
75
	/**
76
	 * Set item name.
77
	 * @param string $value
78
	 * @throws WC_Data_Exception
79
	 */
80
	public function set_label( $value ) {
81
		$this->data['label'] = wc_clean( $value );
82
	}
83
84
	/**
85
	 * Set tax rate id.
86
	 * @param int $value
87
	 * @throws WC_Data_Exception
88
	 */
89
	public function set_rate_id( $value ) {
90
		$this->data['rate_id'] = absint( $value );
91
	}
92
93
	/**
94
	 * Set tax total.
95
	 * @param string $value
96
	 * @throws WC_Data_Exception
97
	 */
98
	public function set_tax_total( $value ) {
99
		$this->data['tax_total'] = wc_format_decimal( $value );
100
	}
101
102
	/**
103
	 * Set shipping_tax_total
104
	 * @param string $value
105
	 * @throws WC_Data_Exception
106
	 */
107
	public function set_shipping_tax_total( $value ) {
108
		$this->data['shipping_tax_total'] = wc_format_decimal( $value );
109
	}
110
111
	/**
112
	 * Set compound
113
	 * @param bool $value
114
	 * @throws WC_Data_Exception
115
	 */
116
	public function set_compound( $value ) {
117
		$this->data['compound'] = (bool) $value;
118
	}
119
120
	/**
121
	 * Set properties based on passed in tax rate by ID.
122
	 * @param int $tax_rate_id
123
	 * @throws WC_Data_Exception
124
	 */
125
	public function set_rate( $tax_rate_id ) {
126
		$this->set_rate_id( $tax_rate_id );
127
		$this->set_rate_code( WC_Tax::get_rate_code( $tax_rate_id ) );
128
		$this->set_label( WC_Tax::get_rate_label( $tax_rate_id ) );
129
		$this->set_compound( WC_Tax::is_compound( $tax_rate_id ) );
130
	}
131
132
	/*
133
	|--------------------------------------------------------------------------
134
	| Getters
135
	|--------------------------------------------------------------------------
136
	*/
137
138
	/**
139
	 * Get order item type.
140
	 * @return string
141
	 */
142
	public function get_type() {
143
		return 'tax';
144
	}
145
146
	/**
147
	 * Get rate code/name.
148
	 * @return string
149
	 */
150
	public function get_rate_code() {
151
		return $this->data['rate_code'];
152
	}
153
154
	/**
155
	 * Get label.
156
	 * @return string
157
	 */
158
	public function get_label() {
159
		return $this->data['label'] ? $this->data['label'] : __( 'Tax', 'woocommerce');
160
	}
161
162
	/**
163
	 * Get tax rate ID.
164
	 * @return int
165
	 */
166
	public function get_rate_id() {
167
		return absint( $this->data['rate_id'] );
168
	}
169
170
	/**
171
	 * Get tax_total
172
	 * @return string
173
	 */
174
	public function get_tax_total() {
175
		return wc_format_decimal( $this->data['tax_total'] );
176
	}
177
178
	/**
179
	 * Get shipping_tax_total
180
	 * @return string
181
	 */
182
	public function get_shipping_tax_total() {
183
		return wc_format_decimal( $this->data['shipping_tax_total'] );
184
	}
185
186
	/**
187
	 * Get compound.
188
	 * @return bool
189
	 */
190
	public function get_compound() {
191
		return (bool) $this->data['compound'];
192
	}
193
}
194