| Total Complexity | 56 |
| Total Lines | 568 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SubscriptionsModule 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 SubscriptionsModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class SubscriptionsModule { |
||
| 35 | /** |
||
| 36 | * Plugin. |
||
| 37 | * |
||
| 38 | * @var Plugin $plugin |
||
| 39 | */ |
||
| 40 | public $plugin; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Construct and initialize a subscriptions module object. |
||
| 44 | * |
||
| 45 | * @param Plugin $plugin The plugin. |
||
| 46 | */ |
||
| 47 | public function __construct( Plugin $plugin ) { |
||
| 48 | $this->plugin = $plugin; |
||
| 49 | |||
| 50 | // Actions. |
||
| 51 | add_action( 'wp_loaded', array( $this, 'handle_subscription' ) ); |
||
| 52 | |||
| 53 | add_action( 'plugins_loaded', array( $this, 'maybe_schedule_subscription_payments' ), 5 ); |
||
| 54 | |||
| 55 | // Exclude payment and subscription notes. |
||
| 56 | add_filter( 'comments_clauses', array( $this, 'exclude_subscription_comment_notes' ), 10, 2 ); |
||
| 57 | |||
| 58 | add_action( 'pronamic_pay_new_payment', array( $this, 'maybe_create_subscription' ) ); |
||
| 59 | |||
| 60 | // The 'pronamic_pay_update_subscription_payments' hook adds subscription payments and sends renewal notices. |
||
| 61 | add_action( 'pronamic_pay_update_subscription_payments', array( $this, 'update_subscription_payments' ) ); |
||
| 62 | |||
| 63 | // Listen to payment status changes so we can update related subscriptions. |
||
| 64 | add_action( 'pronamic_payment_status_update', array( $this, 'payment_status_update' ) ); |
||
| 65 | |||
| 66 | // WordPress CLI. |
||
| 67 | // @see https://github.com/woocommerce/woocommerce/blob/3.3.1/includes/class-woocommerce.php#L365-L369. |
||
| 68 | // @see https://github.com/woocommerce/woocommerce/blob/3.3.1/includes/class-wc-cli.php. |
||
| 69 | // @see https://make.wordpress.org/cli/handbook/commands-cookbook/. |
||
| 70 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
| 71 | WP_CLI::add_command( 'pay subscriptions test', array( $this, 'cli_subscriptions_test' ) ); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Handle subscription actions. |
||
| 77 | * |
||
| 78 | * Extensions like Gravity Forms can send action links in for example |
||
| 79 | * email notifications so users can cancel or renew their subscription. |
||
| 80 | */ |
||
| 81 | public function handle_subscription() { |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Start a recurring payment at the specified gateway for the specified subscription. |
||
| 145 | * |
||
| 146 | * @param Subscription $subscription The subscription to start a recurring payment for. |
||
| 147 | * @param Gateway $gateway The gateway to start the recurring payment at. |
||
| 148 | * @param boolean $renewal Flag for renewal payment. |
||
| 149 | */ |
||
| 150 | public function start_recurring( Subscription $subscription, Gateway $gateway, $renewal = false ) { |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Update the specified subscription and redirect if allowed. |
||
| 215 | * |
||
| 216 | * @param Subscription $subscription The updated subscription. |
||
| 217 | * @param boolean $can_redirect Flag to redirect or not. |
||
| 218 | */ |
||
| 219 | public function update_subscription( $subscription = null, $can_redirect = true ) { |
||
| 220 | if ( empty( $subscription ) ) { |
||
| 221 | return; |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->plugin->subscriptions_data_store->update( $subscription ); |
||
| 225 | |||
| 226 | if ( defined( 'DOING_CRON' ) && empty( $subscription->status ) ) { |
||
| 227 | $can_redirect = false; |
||
| 228 | } |
||
| 229 | |||
| 230 | if ( $can_redirect ) { |
||
| 231 | wp_redirect( home_url() ); |
||
| 232 | |||
| 233 | exit; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Comments clauses. |
||
| 239 | * |
||
| 240 | * @param array $clauses The database query clauses. |
||
| 241 | * @param WP_Comment_Query $query The WordPress comment query object. |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public function exclude_subscription_comment_notes( $clauses, $query ) { |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Maybe schedule subscription payments. |
||
| 257 | */ |
||
| 258 | public function maybe_schedule_subscription_payments() { |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Maybe create subscription for the specified payment. |
||
| 268 | * |
||
| 269 | * @param Payment $payment The new payment. |
||
| 270 | */ |
||
| 271 | public function maybe_create_subscription( $payment ) { |
||
| 272 | // Check if there is already subscription attached to the payment. |
||
| 273 | $subscription_id = $payment->get_subscription_id(); |
||
| 274 | |||
| 275 | if ( ! empty( $subscription_id ) ) { |
||
| 276 | // Subscription already created. |
||
| 277 | return; |
||
| 278 | } |
||
| 279 | |||
| 280 | // Check if there is a subscription object attached to the payment. |
||
| 281 | $subscription_data = $payment->subscription; |
||
| 282 | |||
| 283 | if ( empty( $subscription_data ) ) { |
||
| 284 | return; |
||
| 285 | } |
||
| 286 | |||
| 287 | // New subscription. |
||
| 288 | $subscription = new Subscription(); |
||
| 289 | |||
| 290 | $subscription->config_id = $payment->config_id; |
||
| 291 | $subscription->user_id = $payment->user_id; |
||
| 292 | $subscription->title = sprintf( __( 'Subscription for %s', 'pronamic_ideal' ), $payment->title ); |
||
| 293 | $subscription->frequency = $subscription_data->get_frequency(); |
||
| 294 | $subscription->interval = $subscription_data->get_interval(); |
||
| 295 | $subscription->interval_period = $subscription_data->get_interval_period(); |
||
| 296 | $subscription->currency = $subscription_data->get_currency(); |
||
| 297 | $subscription->amount = $subscription_data->get_amount(); |
||
| 298 | $subscription->key = uniqid( 'subscr_' ); |
||
| 299 | $subscription->source = $payment->source; |
||
| 300 | $subscription->source_id = $payment->source_id; |
||
| 301 | $subscription->description = $payment->description; |
||
| 302 | $subscription->email = $payment->email; |
||
| 303 | $subscription->customer_name = $payment->customer_name; |
||
| 304 | $subscription->payment_method = $payment->method; |
||
| 305 | $subscription->first_payment = $payment->date; |
||
| 306 | |||
| 307 | // @todo |
||
| 308 | // Calculate dates |
||
| 309 | // @see https://github.com/pronamic/wp-pronamic-ideal/blob/4.7.0/classes/Pronamic/WP/Pay/Plugin.php#L883-L964 |
||
| 310 | $interval = $subscription->get_date_interval(); |
||
| 311 | |||
| 312 | $start_date = clone $payment->date; |
||
| 313 | $expiry_date = clone $start_date; |
||
| 314 | |||
| 315 | $next_date = clone $start_date; |
||
| 316 | $next_date->add( $interval ); |
||
| 317 | |||
| 318 | $end_date = null; |
||
| 319 | |||
| 320 | if ( $subscription_data->frequency ) { |
||
| 321 | // @see https://stackoverflow.com/a/10818981/6411283 |
||
| 322 | $period = new DatePeriod( $start_date, $interval, $subscription_data->frequency ); |
||
| 323 | |||
| 324 | $dates = iterator_to_array( $period ); |
||
| 325 | |||
| 326 | $end_date = end( $dates ); |
||
| 327 | } |
||
| 328 | |||
| 329 | $subscription->start_date = $start_date; |
||
| 330 | $subscription->end_date = $end_date; |
||
| 331 | $subscription->expiry_date = $expiry_date; |
||
| 332 | $subscription->next_payment = $next_date; |
||
| 333 | |||
| 334 | // Create. |
||
| 335 | $result = $this->plugin->subscriptions_data_store->create( $subscription ); |
||
| 336 | |||
| 337 | if ( $result ) { |
||
| 338 | $payment->subscription_id = $subscription->get_id(); |
||
| 339 | |||
| 340 | $payment->recurring_type = 'first'; |
||
| 341 | $payment->start_date = $start_date; |
||
| 342 | $payment->end_date = $next_date; |
||
| 343 | |||
| 344 | $this->plugin->payments_data_store->update( $payment ); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get expiring subscriptions. |
||
| 350 | * |
||
| 351 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L715-L746 |
||
| 352 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L652-L712 |
||
| 353 | * |
||
| 354 | * @param DateTime $start_date The start date of the period to check for expiring subscriptions. |
||
| 355 | * @param DateTime $end_date The end date of the period to check for expiring subscriptions. |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function get_expiring_subscription_posts( DateTime $start_date, DateTime $end_date ) { |
||
| 359 | $args = array( |
||
| 360 | 'post_type' => 'pronamic_pay_subscr', |
||
| 361 | 'nopaging' => true, |
||
| 362 | 'orderby' => 'post_date', |
||
| 363 | 'order' => 'ASC', |
||
| 364 | 'post_status' => array( |
||
| 365 | 'subscr_pending', |
||
| 366 | 'subscr_expired', |
||
| 367 | 'subscr_failed', |
||
| 368 | 'subscr_active', |
||
| 369 | ), |
||
| 370 | 'meta_query' => array( |
||
| 371 | array( |
||
| 372 | 'key' => '_pronamic_subscription_expiry_date', |
||
| 373 | 'value' => array( |
||
| 374 | $start_date->format( DateTime::MYSQL ), |
||
| 375 | $end_date->format( DateTime::MYSQL ), |
||
| 376 | ), |
||
| 377 | 'compare' => 'BETWEEN', |
||
| 378 | 'type' => 'DATETIME', |
||
| 379 | ), |
||
| 380 | ), |
||
| 381 | ); |
||
| 382 | |||
| 383 | $query = new WP_Query( $args ); |
||
| 384 | |||
| 385 | return $query->posts; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Update subscription payments. |
||
| 390 | */ |
||
| 391 | public function update_subscription_payments() { |
||
| 436 | } |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Send renewal notices. |
||
| 442 | * |
||
| 443 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L652-L712 |
||
| 444 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L715-L746 |
||
| 445 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/classes/class-sl-emails.php#L41-L126 |
||
| 446 | */ |
||
| 447 | public function send_subscription_renewal_notices() { |
||
| 448 | $interval = new DateInterval( 'P1W' ); // 1 week |
||
| 449 | |||
| 450 | $start_date = new DateTime( 'midnight', new DateTimeZone( 'UTC' ) ); |
||
| 451 | |||
| 452 | $end_date = clone $start_date; |
||
| 453 | $end_date->add( $interval ); |
||
| 454 | |||
| 455 | $expiring_subscription_posts = $this->get_expiring_subscription_posts( $start_date, $end_date ); |
||
| 456 | |||
| 457 | foreach ( $expiring_subscription_posts as $post ) { |
||
| 458 | $subscription = new Subscription( $post->ID ); |
||
| 459 | |||
| 460 | $expiry_date = $subscription->get_expiry_date(); |
||
| 461 | |||
| 462 | $sent_date_string = get_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week', true ); |
||
| 463 | |||
| 464 | if ( $sent_date_string ) { |
||
| 465 | $first_date = clone $expiry_date; |
||
| 466 | $first_date->sub( $interval ); |
||
| 467 | |||
| 468 | $sent_date = new DateTime( $sent_date_string, new DateTimeZone( 'UTC' ) ); |
||
| 469 | |||
| 470 | if ( $sent_date > $first_date ) { |
||
| 471 | // Prevent renewal notices from being sent more than once. |
||
| 472 | continue; |
||
| 473 | } |
||
| 474 | |||
| 475 | delete_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week' ); |
||
| 476 | } |
||
| 477 | |||
| 478 | do_action( 'pronamic_subscription_renewal_notice_' . $subscription->get_source(), $subscription ); |
||
| 479 | |||
| 480 | update_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week', $start_date->format( DateTime::MYSQL ) ); |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Payment status update. |
||
| 486 | * |
||
| 487 | * @param Payment $payment The status updated payment. |
||
| 488 | */ |
||
| 489 | public function payment_status_update( $payment ) { |
||
| 490 | // Check if the payment is connected to a subscription. |
||
| 491 | $subscription = $payment->get_subscription(); |
||
| 492 | |||
| 493 | if ( empty( $subscription ) ) { |
||
| 494 | // Payment not connected to a subscription, nothing to do. |
||
| 495 | return; |
||
| 496 | } |
||
| 497 | |||
| 498 | if ( empty( $payment->start_date ) ) { |
||
| 499 | return; |
||
| 500 | } |
||
| 501 | |||
| 502 | if ( empty( $payment->end_date ) ) { |
||
| 503 | return; |
||
| 504 | } |
||
| 505 | |||
| 506 | if ( Statuses::CANCELLED === $subscription->get_status() ) { |
||
| 507 | // If subscritpion is cancelled never change the subscription status. |
||
| 508 | return; |
||
| 509 | } |
||
| 510 | |||
| 511 | // Status. |
||
| 512 | $status_before = $subscription->get_status(); |
||
| 513 | |||
| 514 | switch ( $payment->get_status() ) { |
||
| 515 | case Statuses::OPEN: |
||
| 516 | // @todo |
||
| 517 | break; |
||
| 518 | case Statuses::SUCCESS: |
||
| 519 | $subscription->set_status( Statuses::ACTIVE ); |
||
| 520 | |||
| 521 | if ( $subscription->expiry_date < $payment->end_date ) { |
||
| 522 | $subscription->expiry_date = $payment->end_date; |
||
| 523 | } |
||
| 524 | |||
| 525 | break; |
||
| 526 | case Statuses::FAILURE: |
||
| 527 | $subscription->set_status( Statuses::CANCELLED ); |
||
| 528 | |||
| 529 | break; |
||
| 530 | case Statuses::CANCELLED: |
||
| 531 | $subscription->set_status( Statuses::CANCELLED ); |
||
| 532 | |||
| 533 | break; |
||
| 534 | case Statuses::EXPIRED: |
||
| 535 | $subscription->set_status( Statuses::CANCELLED ); |
||
| 536 | |||
| 537 | break; |
||
| 538 | case Statuses::COMPLETED: |
||
| 539 | $subscription->set_status( Statuses::COMPLETED ); |
||
| 540 | |||
| 541 | break; |
||
| 542 | } |
||
| 543 | |||
| 544 | $status_after = $subscription->get_status(); |
||
| 545 | |||
| 546 | if ( $status_before !== $status_after ) { |
||
| 547 | $subscription->add_note( sprintf( |
||
| 548 | __( 'Subscription status changed from "%1$s" to "%2$s".', 'pronamic_ideal' ), |
||
| 549 | esc_html( $this->plugin->subscriptions_data_store->get_meta_status_label( $status_before ) ), |
||
| 550 | esc_html( $this->plugin->subscriptions_data_store->get_meta_status_label( $status_after ) ) |
||
| 551 | ) ); |
||
| 552 | } |
||
| 553 | |||
| 554 | // Update. |
||
| 555 | $result = $this->plugin->subscriptions_data_store->update( $subscription ); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * CLI subscriptions test. |
||
| 560 | */ |
||
| 561 | public function cli_subscriptions_test() { |
||
| 602 | } |
||
| 603 | } |
||
| 604 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths