| Total Complexity | 45 |
| Total Lines | 254 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GetPaid_Payment_Form_Submission_Taxes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GetPaid_Payment_Form_Submission_Taxes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GetPaid_Payment_Form_Submission_Taxes { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Submission taxes. |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | public $taxes = array(); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Whether or not we should skip the taxes. |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $skip_taxes = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Class constructor |
||
| 29 | * |
||
| 30 | * @param GetPaid_Payment_Form_Submission $submission |
||
| 31 | */ |
||
| 32 | public function __construct( $submission ) { |
||
| 33 | // Validate VAT number. |
||
| 34 | $this->validate_vat( $submission ); |
||
| 35 | |||
| 36 | if ( $this->skip_taxes ) { |
||
| 37 | return; |
||
| 38 | } |
||
| 39 | |||
| 40 | foreach ( $submission->get_items() as $item ) { |
||
| 41 | $this->process_item_tax( $item, $submission ); |
||
| 42 | } |
||
| 43 | |||
| 44 | // Process any existing invoice taxes. |
||
| 45 | if ( $submission->has_invoice() ) { |
||
| 46 | $invoice = $submission->get_invoice(); |
||
| 47 | $invoice = $this->refresh_totals( $invoice, $submission ); |
||
| 48 | |||
| 49 | $this->taxes = array_replace( $invoice->get_taxes(), $this->taxes ); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Maybe process tax. |
||
| 55 | * |
||
| 56 | * @since 1.0.19 |
||
| 57 | * @param GetPaid_Form_Item $item |
||
| 58 | * @param GetPaid_Payment_Form_Submission $submission |
||
| 59 | */ |
||
| 60 | public function process_item_tax( $item, $submission ) { |
||
| 61 | |||
| 62 | $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
||
| 63 | $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
||
| 64 | $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
||
| 65 | $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
||
| 66 | |||
| 67 | foreach ( $taxes as $name => $amount ) { |
||
| 68 | $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
||
| 69 | $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
||
| 70 | |||
| 71 | $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
||
| 72 | |||
| 73 | if ( ! isset( $this->taxes[ $name ] ) ) { |
||
| 74 | $this->taxes[ $name ] = $tax; |
||
| 75 | continue; |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
||
| 79 | $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
||
| 80 | |||
| 81 | } |
||
| 82 | |||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Checks if the submission has a digital item. |
||
| 87 | * |
||
| 88 | * @param GetPaid_Payment_Form_Submission $submission |
||
| 89 | * @since 1.0.19 |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function has_digital_item( $submission ) { |
||
| 93 | |||
| 94 | foreach ( $submission->get_items() as $item ) { |
||
| 95 | |||
| 96 | if ( 'digital' == $item->get_vat_rule() ) { |
||
| 97 | return true; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | return false; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Checks if this is an eu store. |
||
| 106 | * |
||
| 107 | * @since 1.0.19 |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public static function is_eu_store() { |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Checks if this is an eu country. |
||
| 116 | * |
||
| 117 | * @param string $country |
||
| 118 | * @since 1.0.19 |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public static function is_eu_country( $country ) { |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Checks if this is an eu purchase. |
||
| 127 | * |
||
| 128 | * @param string $customer_country |
||
| 129 | * @since 1.0.19 |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public static function is_eu_transaction( $customer_country ) { |
||
| 133 | return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Retrieves the vat number. |
||
| 138 | * |
||
| 139 | * @param GetPaid_Payment_Form_Submission $submission |
||
| 140 | * @since 1.0.19 |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function get_vat_number( $submission ) { |
||
| 144 | |||
| 145 | // Retrieve from the posted number. |
||
| 146 | $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
||
| 147 | if ( ! is_null( $vat_number ) ) { |
||
| 148 | return wpinv_clean( $vat_number ); |
||
| 149 | } |
||
| 150 | |||
| 151 | return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Retrieves the company. |
||
| 156 | * |
||
| 157 | * @param GetPaid_Payment_Form_Submission $submission |
||
| 158 | * @since 1.0.19 |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function get_company( $submission ) { |
||
| 162 | |||
| 163 | // Retrieve from the posted data. |
||
| 164 | $company = $submission->get_field( 'wpinv_company', 'billing' ); |
||
| 165 | if ( ! empty( $company ) ) { |
||
| 166 | return wpinv_clean( $company ); |
||
| 167 | } |
||
| 168 | |||
| 169 | // Retrieve from the invoice. |
||
| 170 | return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Checks if we require a VAT number. |
||
| 175 | * |
||
| 176 | * @param bool $ip_in_eu Whether the customer IP is from the EU |
||
| 177 | * @param bool $country_in_eu Whether the customer country is from the EU |
||
| 178 | * @since 1.0.19 |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function requires_vat( $ip_in_eu, $country_in_eu ) { |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Validate VAT data. |
||
| 192 | * |
||
| 193 | * @param GetPaid_Payment_Form_Submission $submission |
||
| 194 | * @since 1.0.19 |
||
| 195 | */ |
||
| 196 | public function validate_vat( $submission ) { |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Refresh totals if country or region changed in payment form. |
||
| 241 | * |
||
| 242 | * @since 2.8.8 |
||
| 243 | * |
||
| 244 | * @param object $invoice Invoice object. |
||
| 245 | * @param GetPaid_Payment_Form_Submission $submission Payment form submission object. |
||
| 246 | * @return object Invoice object. |
||
| 247 | */ |
||
| 248 | public function refresh_totals( $invoice, $submission ) { |
||
| 269 |