Completed
Pull Request — master (#10259)
by Mike
08:16
created

WC_Order_Item_Fee::offsetExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Order Line Item (fee).
8
 *
9
 * @version     2.6.0
10
 * @since       2.6.0
11
 * @package     WooCommerce/Classes
12
 * @author      WooThemes
13
 */
14
class WC_Order_Item_Fee extends WC_Order_Item {
15
16
	/**
17
	 * Data properties of this order item object.
18
	 * @since 2.6.0
19
	 * @var array
20
	 */
21
	protected $_data = array(
22
		'order_id'      => 0,
23
		'order_item_id' => 0,
24
		'name'          => '',
25
		'tax_class'     => '',
26
		'tax_status'    => 'taxable',
27
		'total'         => '',
28
		'total_tax'     => '',
29
		'taxes'         => array(
30
			'total' => array()
31
		)
32
	);
33
34
	/**
35
	 * offsetGet for ArrayAccess/Backwards compatibility.
36
	 * @deprecated Add deprecation notices in future release.
37
	 * @param string $offset
38
	 * @return mixed
39
	 */
40
	public function offsetGet( $offset ) {
41
		if ( 'line_total' === $offset ) {
42
			$offset = 'total';
43
		} elseif ( 'line_tax' === $offset ) {
44
			$offset = 'total_tax';
45
		} elseif ( 'line_tax_data' === $offset ) {
46
			$offset = 'taxes';
47
		}
48
		return parent::offsetGet( $offset );
49
	}
50
51
	/**
52
	 * offsetExists for ArrayAccess
53
	 * @param string $offset
54
	 * @return bool
55
	 */
56
	public function offsetExists( $offset ) {
57
		if ( in_array( $offset, array( 'line_total', 'line_tax', 'line_tax_data' ) ) ) {
58
			return true;
59
		}
60
		return parent::offsetExists( $offset );
61
	}
62
63
	/**
64
	 * Read/populate data properties specific to this order item.
65
	 */
66
	public function read( $id ) {
67
		parent::read( $id );
68
		if ( $this->get_id() ) {
69
			$this->set_tax_class( get_metadata( 'order_item', $this->get_id(), '_tax_class', true ) );
70
			$this->set_tax_status( get_metadata( 'order_item', $this->get_id(), '_tax_status', true ) );
71
			$this->set_total( get_metadata( 'order_item', $this->get_id(), '_line_total', true ) );
72
			$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), '_line_tax', true ) );
73
			$this->set_taxes( get_metadata( 'order_item', $this->get_id(), '_line_tax_data', true ) );
74
		}
75
	}
76
77
	/**
78
	 * Save properties specific to this order item.
79
	 * @return int Item ID
80
	 */
81
	public function save() {
82
		parent::save();
83
		if ( $this->get_id() ) {
84
			wc_update_order_item_meta( $this->get_id(), '_tax_class', $this->get_tax_class() );
85
			wc_update_order_item_meta( $this->get_id(), '_tax_status', $this->get_tax_status() );
86
			wc_update_order_item_meta( $this->get_id(), '_line_total', $this->get_total() );
87
			wc_update_order_item_meta( $this->get_id(), '_line_tax', $this->get_total_tax() );
88
			wc_update_order_item_meta( $this->get_id(), '_line_tax_data', $this->get_taxes() );
89
		}
90
91
		return $this->get_id();
92
	}
93
94
	/*
95
	|--------------------------------------------------------------------------
96
	| Setters
97
	|--------------------------------------------------------------------------
98
	*/
99
100
	/**
101
	 * Set tax class.
102
	 * @param string $value
103
	 */
104
	public function set_tax_class( $value ) {
105
		$this->_data['tax_class'] = $value;
106
	}
107
108
	/**
109
	 * Set tax_status.
110
	 * @param string $value
111
	 */
112
	public function set_tax_status( $value ) {
113
		if ( in_array( $value, array( 'taxable', 'none' ) ) ) {
114
			$this->_data['tax_status'] = $value;
115
		} else {
116
			$this->_data['tax_status'] = 'taxable';
117
		}
118
	}
119
120
	/**
121
	 * Set total.
122
	 * @param string $value
123
	 */
124
	public function set_total( $value ) {
125
		$this->_data['total'] = wc_format_decimal( $value );
126
	}
127
128
	/**
129
	 * Set total tax.
130
	 * @param string $value
131
	 */
132
	public function set_total_tax( $value ) {
133
		$this->_data['total_tax'] = wc_format_decimal( $value );
134
	}
135
136
	/**
137
	 * Set taxes.
138
	 *
139
	 * This is an array of tax ID keys with total amount values.
140
	 * @param array $raw_tax_data
141
	 */
142 View Code Duplication
	public function set_taxes( $raw_tax_data ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
		$raw_tax_data = maybe_unserialize( $raw_tax_data );
144
		$tax_data     = array(
145
			'total' => array(),
146
		);
147
		if ( ! empty( $raw_tax_data['total'] ) ) {
148
			$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
149
		}
150
		$this->_data['taxes'] = $tax_data;
151
	}
152
153
	/*
154
	|--------------------------------------------------------------------------
155
	| Getters
156
	|--------------------------------------------------------------------------
157
	*/
158
159
	/**
160
	 * Get order item name.
161
	 * @return string
162
	 */
163
	public function get_name() {
164
		return $this->_data['name'] ? $this->_data['name'] : __( 'Fee', 'woocommerce' );
165
	}
166
167
	/**
168
	 * Get order item type.
169
	 * @return string
170
	 */
171
	public function get_type() {
172
		return 'fee';
173
	}
174
175
	/**
176
	 * Get tax class.
177
	 * @return string
178
	 */
179
	public function get_tax_class() {
180
		return $this->_data['tax_class'];
181
	}
182
183
	/**
184
	 * Get tax status.
185
	 * @return string
186
	 */
187
	public function get_tax_status() {
188
		return $this->_data['tax_status'];
189
	}
190
191
	/**
192
	 * Get total fee.
193
	 * @return string
194
	 */
195
	public function get_total() {
196
		return wc_format_decimal( $this->_data['total'] );
197
	}
198
199
	/**
200
	 * Get total tax.
201
	 * @return string
202
	 */
203
	public function get_total_tax() {
204
		return wc_format_decimal( $this->_data['total_tax'] );
205
	}
206
207
	/**
208
	 * Get fee taxes.
209
	 * @return array
210
	 */
211
	public function get_taxes() {
212
		return $this->_data['taxes'];
213
	}
214
}
215