| Conditions | 12 |
| Paths | 43 |
| Total Lines | 88 |
| Lines | 25 |
| Ratio | 28.41 % |
| 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 |
||
| 112 | public function process( $event_json ) { |
||
| 113 | |||
| 114 | // Next, proceed with additional webhooks. |
||
| 115 | if ( isset( $event_json->id ) ) { |
||
| 116 | |||
| 117 | status_header( 200 ); |
||
| 118 | |||
| 119 | try { |
||
| 120 | |||
| 121 | $event = \Stripe\Event::retrieve( $event_json->id ); |
||
| 122 | |||
| 123 | // Update time of webhook received whenever the event is retrieved. |
||
| 124 | give_update_option( 'give_stripe_last_webhook_received_timestamp', current_time( 'timestamp', 1 ) ); |
||
| 125 | |||
| 126 | } catch ( \Stripe\Error\Authentication $e ) { |
||
| 127 | |||
| 128 | if ( strpos( $e->getMessage(), 'Platform access may have been revoked' ) !== false ) { |
||
| 129 | give_stripe_connect_delete_options(); |
||
| 130 | } |
||
| 131 | } catch ( Exception $e ) { |
||
| 132 | die( 'Invalid event ID' ); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Bailout, if event type doesn't exists. |
||
| 136 | if ( empty( $event->type ) ) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | switch ( $event->type ) { |
||
| 141 | |||
| 142 | View Code Duplication | case 'payment_intent.succeeded': |
|
|
|
|||
| 143 | $intent = $event->data->object; |
||
| 144 | |||
| 145 | if ( 'succeeded' === $intent->status ) { |
||
| 146 | $donation_id = give_get_purchase_id_by_transaction_id( $intent->id ); |
||
| 147 | |||
| 148 | // Update payment status to donation. |
||
| 149 | give_update_payment_status( $donation_id, 'publish' ); |
||
| 150 | |||
| 151 | // Insert donation note to inform admin that charge succeeded. |
||
| 152 | give_insert_payment_note( $donation_id, __( 'Charge succeeded in Stripe.', 'give' ) ); |
||
| 153 | } |
||
| 154 | |||
| 155 | break; |
||
| 156 | |||
| 157 | View Code Duplication | case 'payment_intent.payment_failed': |
|
| 158 | $intent = $event->data->object; |
||
| 159 | $donation_id = give_get_purchase_id_by_transaction_id( $intent->id ); |
||
| 160 | |||
| 161 | // Update payment status to donation. |
||
| 162 | give_update_payment_status( $donation_id, 'failed' ); |
||
| 163 | |||
| 164 | // Insert donation note to inform admin that charge succeeded. |
||
| 165 | give_insert_payment_note( $donation_id, __( 'Charge failed in Stripe.', 'give' ) ); |
||
| 166 | |||
| 167 | break; |
||
| 168 | |||
| 169 | case 'charge.refunded': |
||
| 170 | global $wpdb; |
||
| 171 | |||
| 172 | $charge = $event->data->object; |
||
| 173 | |||
| 174 | if ( $charge->refunded ) { |
||
| 175 | |||
| 176 | $payment_id = $wpdb->get_var( $wpdb->prepare( "SELECT donation_id FROM {$wpdb->donationmeta} WHERE meta_key = '_give_payment_transaction_id' AND meta_value = %s LIMIT 1", $charge->id ) ); |
||
| 177 | |||
| 178 | if ( $payment_id ) { |
||
| 179 | |||
| 180 | give_update_payment_status( $payment_id, 'refunded' ); |
||
| 181 | give_insert_payment_note( $payment_id, __( 'Charge refunded in Stripe.', 'give' ) ); |
||
| 182 | |||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | break; |
||
| 187 | } |
||
| 188 | |||
| 189 | do_action( 'give_stripe_event_' . $event->type, $event ); |
||
| 190 | |||
| 191 | return $event->type; |
||
| 192 | |||
| 193 | } else { |
||
| 194 | status_header( 500 ); |
||
| 195 | // Something went wrong outside of Stripe. |
||
| 196 | give_record_gateway_error( __( 'Stripe Error', 'give' ), sprintf( __( 'An error occurred while processing a webhook.', 'give' ) ) ); |
||
| 197 | die( '-1' ); // Failed. |
||
| 198 | } // End if(). |
||
| 199 | } |
||
| 200 | } |
||
| 204 |
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.