Complex classes like WC_Stripe_Customer 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 WC_Stripe_Customer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class WC_Stripe_Customer { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Stripe customer ID |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $id = ''; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * WP User ID |
||
| 21 | * @var integer |
||
| 22 | */ |
||
| 23 | private $user_id = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Data from API |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $customer_data = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Constructor |
||
| 33 | * @param int $user_id The WP user ID |
||
| 34 | */ |
||
| 35 | public function __construct( $user_id = 0 ) { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get Stripe customer ID. |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function get_id() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set Stripe customer ID. |
||
| 52 | * @param [type] $id [description] |
||
|
|
|||
| 53 | */ |
||
| 54 | public function set_id( $id ) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * User ID in WordPress. |
||
| 67 | * @return int |
||
| 68 | */ |
||
| 69 | public function get_user_id() { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Set User ID used by WordPress. |
||
| 75 | * @param int $user_id |
||
| 76 | */ |
||
| 77 | public function set_user_id( $user_id ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get user object. |
||
| 83 | * @return WP_User |
||
| 84 | */ |
||
| 85 | protected function get_user() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Store data from the Stripe API about this customer |
||
| 91 | */ |
||
| 92 | public function set_customer_data( $data ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Generates the customer request, used for both creating and updating customers. |
||
| 98 | * |
||
| 99 | * @param array $args Additional arguments (optional). |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | protected function generate_customer_request( $args = array() ) { |
||
| 103 | $billing_email = isset( $_POST['billing_email'] ) ? filter_var( $_POST['billing_email'], FILTER_SANITIZE_EMAIL ) : ''; |
||
| 104 | $user = $this->get_user(); |
||
| 105 | |||
| 106 | if ( $user ) { |
||
| 107 | $billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true ); |
||
| 108 | $billing_last_name = get_user_meta( $user->ID, 'billing_last_name', true ); |
||
| 109 | |||
| 110 | // If billing first name does not exists try the user first name. |
||
| 111 | if ( empty( $billing_first_name ) ) { |
||
| 112 | $billing_first_name = get_user_meta( $user->ID, 'first_name', true ); |
||
| 113 | } |
||
| 114 | |||
| 115 | // If billing last name does not exists try the user last name. |
||
| 116 | if ( empty( $billing_last_name ) ) { |
||
| 117 | $billing_last_name = get_user_meta( $user->ID, 'last_name', true ); |
||
| 118 | } |
||
| 119 | |||
| 120 | // translators: %1$s First name, %2$s Second name, %3$s Username. |
||
| 121 | $description = sprintf( __( 'Name: %1$s %2$s, Username: %s', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name, $user->user_login ); |
||
| 122 | |||
| 123 | $defaults = array( |
||
| 124 | 'email' => $user->user_email, |
||
| 125 | 'description' => $description, |
||
| 126 | ); |
||
| 127 | } else { |
||
| 128 | $billing_first_name = isset( $_POST['billing_first_name'] ) ? filter_var( wp_unslash( $_POST['billing_first_name'] ), FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
||
| 129 | $billing_last_name = isset( $_POST['billing_last_name'] ) ? filter_var( wp_unslash( $_POST['billing_last_name'] ), FILTER_SANITIZE_STRING ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
||
| 130 | |||
| 131 | // translators: %1$s First name, %2$s Second name. |
||
| 132 | $description = sprintf( __( 'Name: %1$s %2$s, Guest', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name ); |
||
| 133 | |||
| 134 | $defaults = array( |
||
| 135 | 'email' => $billing_email, |
||
| 136 | 'description' => $description, |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | |||
| 140 | $metadata = array(); |
||
| 141 | $defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user ); |
||
| 142 | |||
| 143 | return wp_parse_args( $args, $defaults ); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Create a customer via API. |
||
| 148 | * @param array $args |
||
| 149 | * @return WP_Error|int |
||
| 150 | */ |
||
| 151 | public function create_customer( $args = array() ) { |
||
| 152 | $args = $this->generate_customer_request( $args ); |
||
| 153 | $response = WC_Stripe_API::request( apply_filters( 'wc_stripe_create_customer_args', $args ), 'customers' ); |
||
| 154 | |||
| 155 | if ( ! empty( $response->error ) ) { |
||
| 156 | throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
||
| 157 | } |
||
| 158 | |||
| 159 | $this->set_id( $response->id ); |
||
| 160 | $this->clear_cache(); |
||
| 161 | $this->set_customer_data( $response ); |
||
| 162 | |||
| 163 | if ( $this->get_user_id() ) { |
||
| 164 | update_user_meta( $this->get_user_id(), '_stripe_customer_id', $response->id ); |
||
| 165 | } |
||
| 166 | |||
| 167 | do_action( 'woocommerce_stripe_add_customer', $args, $response ); |
||
| 168 | |||
| 169 | return $response->id; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Updates the Stripe customer through the API. |
||
| 174 | * |
||
| 175 | * @param array $args Additional arguments for the request (optional). |
||
| 176 | */ |
||
| 177 | public function update_customer( $args = array() ) { |
||
| 178 | if ( empty( $this->id ) ) { |
||
| 179 | throw new WC_Stripe_Exception( 'id_required_to_update_user', __( 'Attempting to update a Stripe customer without a customer ID.', 'woocommerce-gateway-stripe' ) ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $args = $this->generate_customer_request( $args ); |
||
| 183 | $args = apply_filters( 'wc_stripe_update_customer_args', $args ); |
||
| 184 | $response = WC_Stripe_API::request( $args, 'customers/' . $this->id ); |
||
| 185 | |||
| 186 | if ( ! empty( $response->error ) ) { |
||
| 187 | throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
||
| 188 | } |
||
| 189 | |||
| 190 | $this->clear_cache(); |
||
| 191 | $this->set_customer_data( $response ); |
||
| 192 | |||
| 193 | do_action( 'woocommerce_stripe_update_customer', $args, $response ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Checks to see if error is of invalid request |
||
| 198 | * error and it is no such customer. |
||
| 199 | * |
||
| 200 | * @since 4.1.2 |
||
| 201 | * @param array $error |
||
| 202 | */ |
||
| 203 | public function is_no_such_customer_error( $error ) { |
||
| 204 | return ( |
||
| 205 | $error && |
||
| 206 | 'invalid_request_error' === $error->type && |
||
| 207 | preg_match( '/No such customer/i', $error->message ) |
||
| 208 | ); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Add a source for this stripe customer. |
||
| 213 | * @param string $source_id |
||
| 214 | * @return WP_Error|int |
||
| 215 | */ |
||
| 216 | public function add_source( $source_id ) { |
||
| 217 | if ( ! $this->get_id() ) { |
||
| 218 | $this->set_id( $this->create_customer() ); |
||
| 219 | } |
||
| 220 | |||
| 221 | $response = WC_Stripe_API::request( |
||
| 222 | array( |
||
| 223 | 'source' => $source_id, |
||
| 224 | ), |
||
| 225 | 'customers/' . $this->get_id() . '/sources' |
||
| 226 | ); |
||
| 227 | |||
| 228 | $wc_token = false; |
||
| 229 | |||
| 230 | if ( ! empty( $response->error ) ) { |
||
| 231 | // It is possible the WC user once was linked to a customer on Stripe |
||
| 232 | // but no longer exists. Instead of failing, lets try to create a |
||
| 233 | // new customer. |
||
| 234 | if ( $this->is_no_such_customer_error( $response->error ) ) { |
||
| 235 | delete_user_meta( $this->get_user_id(), '_stripe_customer_id' ); |
||
| 236 | $this->create_customer(); |
||
| 237 | return $this->add_source( $source_id ); |
||
| 238 | } else { |
||
| 239 | return $response; |
||
| 240 | } |
||
| 241 | } elseif ( empty( $response->id ) ) { |
||
| 242 | return new WP_Error( 'error', __( 'Unable to add payment source.', 'woocommerce-gateway-stripe' ) ); |
||
| 243 | } |
||
| 244 | |||
| 245 | // Add token to WooCommerce. |
||
| 246 | if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) { |
||
| 247 | if ( ! empty( $response->type ) ) { |
||
| 248 | switch ( $response->type ) { |
||
| 249 | case 'alipay': |
||
| 250 | break; |
||
| 251 | case 'sepa_debit': |
||
| 252 | $wc_token = new WC_Payment_Token_SEPA(); |
||
| 253 | $wc_token->set_token( $response->id ); |
||
| 254 | $wc_token->set_gateway_id( 'stripe_sepa' ); |
||
| 255 | $wc_token->set_last4( $response->sepa_debit->last4 ); |
||
| 256 | break; |
||
| 257 | default: |
||
| 258 | if ( 'source' === $response->object && 'card' === $response->type ) { |
||
| 259 | $wc_token = new WC_Payment_Token_CC(); |
||
| 260 | $wc_token->set_token( $response->id ); |
||
| 261 | $wc_token->set_gateway_id( 'stripe' ); |
||
| 262 | $wc_token->set_card_type( strtolower( $response->card->brand ) ); |
||
| 263 | $wc_token->set_last4( $response->card->last4 ); |
||
| 264 | $wc_token->set_expiry_month( $response->card->exp_month ); |
||
| 265 | $wc_token->set_expiry_year( $response->card->exp_year ); |
||
| 266 | } |
||
| 267 | break; |
||
| 268 | } |
||
| 269 | } else { |
||
| 270 | // Legacy. |
||
| 271 | $wc_token = new WC_Payment_Token_CC(); |
||
| 272 | $wc_token->set_token( $response->id ); |
||
| 273 | $wc_token->set_gateway_id( 'stripe' ); |
||
| 274 | $wc_token->set_card_type( strtolower( $response->brand ) ); |
||
| 275 | $wc_token->set_last4( $response->last4 ); |
||
| 276 | $wc_token->set_expiry_month( $response->exp_month ); |
||
| 277 | $wc_token->set_expiry_year( $response->exp_year ); |
||
| 278 | } |
||
| 279 | |||
| 280 | $wc_token->set_user_id( $this->get_user_id() ); |
||
| 281 | $wc_token->save(); |
||
| 282 | } |
||
| 283 | |||
| 284 | $this->clear_cache(); |
||
| 285 | |||
| 286 | do_action( 'woocommerce_stripe_add_source', $this->get_id(), $wc_token, $response, $source_id ); |
||
| 287 | |||
| 288 | return $response->id; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get a customers saved sources using their Stripe ID. |
||
| 293 | * |
||
| 294 | * @param string $customer_id |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function get_sources() { |
||
| 298 | if ( ! $this->get_id() ) { |
||
| 299 | return array(); |
||
| 300 | } |
||
| 301 | |||
| 302 | $sources = get_transient( 'stripe_sources_' . $this->get_id() ); |
||
| 303 | |||
| 304 | $response = WC_Stripe_API::request( |
||
| 305 | array( |
||
| 306 | 'limit' => 100, |
||
| 307 | ), |
||
| 308 | 'customers/' . $this->get_id() . '/sources', |
||
| 309 | 'GET' |
||
| 310 | ); |
||
| 311 | |||
| 312 | if ( ! empty( $response->error ) ) { |
||
| 313 | return array(); |
||
| 314 | } |
||
| 315 | |||
| 316 | if ( is_array( $response->data ) ) { |
||
| 317 | $sources = $response->data; |
||
| 318 | } |
||
| 319 | |||
| 320 | return empty( $sources ) ? array() : $sources; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Delete a source from stripe. |
||
| 325 | * @param string $source_id |
||
| 326 | */ |
||
| 327 | public function delete_source( $source_id ) { |
||
| 328 | if ( ! $this->get_id() ) { |
||
| 329 | return false; |
||
| 330 | } |
||
| 331 | |||
| 332 | $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $source_id ), 'DELETE' ); |
||
| 333 | |||
| 334 | $this->clear_cache(); |
||
| 335 | |||
| 336 | if ( empty( $response->error ) ) { |
||
| 337 | do_action( 'wc_stripe_delete_source', $this->get_id(), $response ); |
||
| 338 | |||
| 339 | return true; |
||
| 340 | } |
||
| 341 | |||
| 342 | return false; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Set default source in Stripe |
||
| 347 | * @param string $source_id |
||
| 348 | */ |
||
| 349 | public function set_default_source( $source_id ) { |
||
| 350 | $response = WC_Stripe_API::request( |
||
| 351 | array( |
||
| 352 | 'default_source' => sanitize_text_field( $source_id ), |
||
| 353 | ), |
||
| 354 | 'customers/' . $this->get_id(), |
||
| 355 | 'POST' |
||
| 356 | ); |
||
| 357 | |||
| 358 | $this->clear_cache(); |
||
| 359 | |||
| 360 | if ( empty( $response->error ) ) { |
||
| 361 | do_action( 'wc_stripe_set_default_source', $this->get_id(), $response ); |
||
| 362 | |||
| 363 | return true; |
||
| 364 | } |
||
| 365 | |||
| 366 | return false; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Deletes caches for this users cards. |
||
| 371 | */ |
||
| 372 | public function clear_cache() { |
||
| 377 | } |
||
| 378 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.