| Conditions | 15 |
| Paths | 88 |
| Total Lines | 85 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 142 | public static function status_update( Payment $payment ) { |
||
| 143 | $transaction_id = $payment->get_source_id(); |
||
| 144 | |||
| 145 | $transaction = new MeprTransaction( $transaction_id ); |
||
| 146 | |||
| 147 | if ( $payment->get_recurring() ) { |
||
| 148 | $subscription = $transaction->subscription(); |
||
| 149 | |||
| 150 | if ( empty( $subscription ) || empty( $subscription->id ) ) { |
||
| 151 | $subscription_id = $payment->get_subscription()->get_source_id(); |
||
| 152 | |||
| 153 | $subscription = new MeprSubscription( $subscription_id ); |
||
| 154 | } |
||
| 155 | |||
| 156 | // Same source ID and first transaction ID for recurring payment means we need to add a new transaction. |
||
| 157 | if ( $payment->get_source_id() === $subscription->id ) { |
||
| 158 | // First transaction. |
||
| 159 | $first_txn = $subscription->first_txn(); |
||
| 160 | |||
| 161 | if ( false === $first_txn || ! ( $first_txn instanceof MeprTransaction ) ) { |
||
| 162 | $first_txn = new MeprTransaction(); |
||
| 163 | $first_txn->user_id = $subscription->user_id; |
||
| 164 | $first_txn->product_id = $subscription->product_id; |
||
| 165 | $first_txn->coupon_id = $subscription->coupon_id; |
||
| 166 | $first_txn->gateway = null; |
||
|
1 ignored issue
–
show
|
|||
| 167 | } |
||
| 168 | |||
| 169 | // Transaction number. |
||
| 170 | $trans_num = $payment->get_transaction_id(); |
||
| 171 | |||
| 172 | if ( empty( $trans_num ) ) { |
||
| 173 | $trans_num = uniqid(); |
||
| 174 | } |
||
| 175 | |||
| 176 | // New transaction. |
||
| 177 | $transaction = new MeprTransaction(); |
||
| 178 | $transaction->created_at = $payment->post->post_date_gmt; |
||
|
1 ignored issue
–
show
|
|||
| 179 | $transaction->user_id = $first_txn->user_id; |
||
| 180 | $transaction->product_id = $first_txn->product_id; |
||
| 181 | $transaction->coupon_id = $first_txn->coupon_id; |
||
| 182 | $transaction->gateway = $first_txn->gateway; |
||
| 183 | $transaction->trans_num = $trans_num; |
||
|
1 ignored issue
–
show
|
|||
| 184 | $transaction->txn_type = MeprTransaction::$payment_str; |
||
|
1 ignored issue
–
show
|
|||
| 185 | $transaction->status = MeprTransaction::$pending_str; |
||
|
1 ignored issue
–
show
|
|||
| 186 | $transaction->subscription_id = $subscription->id; |
||
|
1 ignored issue
–
show
|
|||
| 187 | |||
| 188 | $transaction->set_gross( $payment->get_amount()->get_amount() ); |
||
| 189 | |||
| 190 | $transaction->store(); |
||
| 191 | |||
| 192 | // Set source ID. |
||
| 193 | $payment->set_meta( 'source_id', $transaction->id ); |
||
| 194 | |||
| 195 | $payment->source_id = $transaction->id; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | $should_update = ! MemberPress::transaction_has_status( $transaction, array( |
||
| 200 | MeprTransaction::$failed_str, |
||
| 201 | MeprTransaction::$complete_str, |
||
| 202 | ) ); |
||
| 203 | |||
| 204 | if ( $should_update ) { |
||
| 205 | $gateway = new Gateway(); |
||
| 206 | |||
| 207 | $gateway->mp_txn = $transaction; |
||
| 208 | |||
| 209 | switch ( $payment->get_status() ) { |
||
| 210 | case Statuses::CANCELLED: |
||
| 211 | case Statuses::EXPIRED: |
||
| 212 | case Statuses::FAILURE: |
||
| 213 | $gateway->record_payment_failure(); |
||
| 214 | |||
| 215 | break; |
||
| 216 | case Statuses::SUCCESS: |
||
| 217 | if ( $payment->get_recurring() ) { |
||
| 218 | $gateway->record_subscription_payment(); |
||
| 219 | } else { |
||
| 220 | $gateway->record_payment(); |
||
| 221 | } |
||
| 222 | |||
| 223 | break; |
||
| 224 | case Statuses::OPEN: |
||
| 225 | default: |
||
| 226 | break; |
||
| 227 | } |
||
| 377 |