| Total Complexity | 54 |
| Total Lines | 556 |
| 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 | // Listen to subscription status changes so we can log these in a note. |
||
| 67 | add_action( 'pronamic_subscription_status_update', array( $this, 'log_subscription_status_update' ), 10, 4 ); |
||
| 68 | |||
| 69 | // WordPress CLI. |
||
| 70 | // @see https://github.com/woocommerce/woocommerce/blob/3.3.1/includes/class-woocommerce.php#L365-L369. |
||
| 71 | // @see https://github.com/woocommerce/woocommerce/blob/3.3.1/includes/class-wc-cli.php. |
||
| 72 | // @see https://make.wordpress.org/cli/handbook/commands-cookbook/. |
||
| 73 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
| 74 | WP_CLI::add_command( 'pay subscriptions test', array( $this, 'cli_subscriptions_test' ) ); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Handle subscription actions. |
||
| 80 | * |
||
| 81 | * Extensions like Gravity Forms can send action links in for example |
||
| 82 | * email notifications so users can cancel or renew their subscription. |
||
| 83 | */ |
||
| 84 | public function handle_subscription() { |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Start a recurring payment at the specified gateway for the specified subscription. |
||
| 148 | * |
||
| 149 | * @param Subscription $subscription The subscription to start a recurring payment for. |
||
| 150 | * @param Gateway $gateway The gateway to start the recurring payment at. |
||
| 151 | * @param boolean $renewal Flag for renewal payment. |
||
| 152 | */ |
||
| 153 | public function start_recurring( Subscription $subscription, Gateway $gateway, $renewal = false ) { |
||
| 154 | // If next payment date is after the subscription end date unset the next payment date. |
||
| 155 | if ( isset( $subscription->end_date, $subscription->next_payment ) && $subscription->end_date <= $subscription->next_payment ) { |
||
| 156 | $subscription->next_payment = null; |
||
| 157 | } |
||
| 158 | |||
| 159 | // If there is no next payment date change the subscription status to completed. |
||
| 160 | if ( empty( $subscription->next_payment ) ) { |
||
| 161 | $subscription->status = Statuses::COMPLETED; |
||
| 162 | $subscription->expiry_date = $subscription->end_date; |
||
| 163 | |||
| 164 | $result = $this->plugin->subscriptions_data_store->update( $subscription ); |
||
| 165 | |||
| 166 | // @todo |
||
| 167 | return; |
||
| 168 | } |
||
| 169 | |||
| 170 | // Calculate payment start and end dates. |
||
| 171 | $start_date = clone $subscription->next_payment; |
||
| 172 | |||
| 173 | $end_date = clone $start_date; |
||
| 174 | $end_date->add( $subscription->get_date_interval() ); |
||
| 175 | |||
| 176 | $subscription->next_payment = $end_date; |
||
| 177 | |||
| 178 | // Create follow up payment. |
||
| 179 | $payment = new Payment(); |
||
| 180 | |||
| 181 | $payment->config_id = $subscription->config_id; |
||
| 182 | $payment->user_id = $subscription->user_id; |
||
| 183 | $payment->source = $subscription->source; |
||
| 184 | $payment->source_id = $subscription->source_id; |
||
| 185 | $payment->description = $subscription->description; |
||
| 186 | $payment->order_id = $subscription->order_id; |
||
| 187 | $payment->currency = $subscription->currency; |
||
| 188 | $payment->email = $subscription->email; |
||
| 189 | $payment->customer_name = $subscription->customer_name; |
||
| 190 | $payment->address = $subscription->address; |
||
| 191 | $payment->address = $subscription->address; |
||
| 192 | $payment->city = $subscription->city; |
||
| 193 | $payment->zip = $subscription->zip; |
||
| 194 | $payment->country = $subscription->country; |
||
| 195 | $payment->telephone_number = $subscription->telephone_number; |
||
| 196 | $payment->method = $subscription->payment_method; |
||
| 197 | $payment->subscription = $subscription; |
||
| 198 | $payment->subscription_id = $subscription->get_id(); |
||
| 199 | $payment->amount = $subscription->amount; |
||
| 200 | $payment->start_date = $start_date; |
||
| 201 | $payment->end_date = $end_date; |
||
| 202 | $payment->recurring_type = 'recurring'; |
||
| 203 | $payment->recurring = ! $renewal; |
||
| 204 | |||
| 205 | // Start payment. |
||
| 206 | $payment = Plugin::start_payment( $payment, $gateway ); |
||
| 207 | |||
| 208 | // Update subscription. |
||
| 209 | $result = $this->plugin->subscriptions_data_store->update( $subscription ); |
||
| 210 | |||
| 211 | return $payment; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Update the specified subscription and redirect if allowed. |
||
| 216 | * |
||
| 217 | * @param Subscription $subscription The updated subscription. |
||
| 218 | * @param boolean $can_redirect Flag to redirect or not. |
||
| 219 | */ |
||
| 220 | public function update_subscription( $subscription = null, $can_redirect = true ) { |
||
| 221 | if ( empty( $subscription ) ) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | |||
| 225 | $this->plugin->subscriptions_data_store->update( $subscription ); |
||
| 226 | |||
| 227 | if ( defined( 'DOING_CRON' ) && empty( $subscription->status ) ) { |
||
| 228 | $can_redirect = false; |
||
| 229 | } |
||
| 230 | |||
| 231 | if ( $can_redirect ) { |
||
| 232 | wp_redirect( home_url() ); |
||
| 233 | |||
| 234 | exit; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Comments clauses. |
||
| 240 | * |
||
| 241 | * @param array $clauses The database query clauses. |
||
| 242 | * @param WP_Comment_Query $query The WordPress comment query object. |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function exclude_subscription_comment_notes( $clauses, $query ) { |
||
| 246 | $type = $query->query_vars['type']; |
||
| 247 | |||
| 248 | // Ignore subscription notes comments if it's not specifically requested. |
||
| 249 | if ( 'subscription_note' !== $type ) { |
||
| 250 | $clauses['where'] .= " AND comment_type != 'subscription_note'"; |
||
| 251 | } |
||
| 252 | |||
| 253 | return $clauses; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Maybe schedule subscription payments. |
||
| 258 | */ |
||
| 259 | public function maybe_schedule_subscription_payments() { |
||
| 260 | if ( wp_next_scheduled( 'pronamic_pay_update_subscription_payments' ) ) { |
||
| 261 | return; |
||
| 262 | } |
||
| 263 | |||
| 264 | wp_schedule_event( time(), 'hourly', 'pronamic_pay_update_subscription_payments' ); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Maybe create subscription for the specified payment. |
||
| 269 | * |
||
| 270 | * @param Payment $payment The new payment. |
||
| 271 | */ |
||
| 272 | public function maybe_create_subscription( $payment ) { |
||
| 273 | // Check if there is already subscription attached to the payment. |
||
| 274 | $subscription_id = $payment->get_subscription_id(); |
||
| 275 | |||
| 276 | if ( ! empty( $subscription_id ) ) { |
||
| 277 | // Subscription already created. |
||
| 278 | return; |
||
| 279 | } |
||
| 280 | |||
| 281 | // Check if there is a subscription object attached to the payment. |
||
| 282 | $subscription_data = $payment->subscription; |
||
| 283 | |||
| 284 | if ( empty( $subscription_data ) ) { |
||
| 285 | return; |
||
| 286 | } |
||
| 287 | |||
| 288 | // New subscription. |
||
| 289 | $subscription = new Subscription(); |
||
| 290 | |||
| 291 | $subscription->config_id = $payment->config_id; |
||
| 292 | $subscription->user_id = $payment->user_id; |
||
| 293 | $subscription->title = sprintf( __( 'Subscription for %s', 'pronamic_ideal' ), $payment->title ); |
||
| 294 | $subscription->frequency = $subscription_data->get_frequency(); |
||
| 295 | $subscription->interval = $subscription_data->get_interval(); |
||
| 296 | $subscription->interval_period = $subscription_data->get_interval_period(); |
||
| 297 | $subscription->currency = $subscription_data->get_currency(); |
||
| 298 | $subscription->amount = $subscription_data->get_amount(); |
||
| 299 | $subscription->key = uniqid( 'subscr_' ); |
||
| 300 | $subscription->source = $payment->source; |
||
| 301 | $subscription->source_id = $payment->source_id; |
||
| 302 | $subscription->description = $payment->description; |
||
| 303 | $subscription->email = $payment->email; |
||
| 304 | $subscription->customer_name = $payment->customer_name; |
||
| 305 | $subscription->payment_method = $payment->method; |
||
| 306 | $subscription->first_payment = $payment->date; |
||
| 307 | $subscription->status = Statuses::OPEN; |
||
| 308 | |||
| 309 | // @todo |
||
| 310 | // Calculate dates |
||
| 311 | // @see https://github.com/pronamic/wp-pronamic-ideal/blob/4.7.0/classes/Pronamic/WP/Pay/Plugin.php#L883-L964 |
||
| 312 | $interval = $subscription->get_date_interval(); |
||
| 313 | |||
| 314 | $start_date = clone $payment->date; |
||
| 315 | $expiry_date = clone $start_date; |
||
| 316 | |||
| 317 | $next_date = clone $start_date; |
||
| 318 | $next_date->add( $interval ); |
||
| 319 | |||
| 320 | $end_date = null; |
||
| 321 | |||
| 322 | if ( $subscription_data->frequency ) { |
||
| 323 | // @see https://stackoverflow.com/a/10818981/6411283 |
||
| 324 | $period = new DatePeriod( $start_date, $interval, $subscription_data->frequency ); |
||
| 325 | |||
| 326 | $dates = iterator_to_array( $period ); |
||
| 327 | |||
| 328 | $end_date = end( $dates ); |
||
| 329 | } |
||
| 330 | |||
| 331 | $subscription->start_date = $start_date; |
||
| 332 | $subscription->end_date = $end_date; |
||
| 333 | $subscription->expiry_date = $expiry_date; |
||
| 334 | $subscription->next_payment = $next_date; |
||
| 335 | |||
| 336 | // Create. |
||
| 337 | $result = $this->plugin->subscriptions_data_store->create( $subscription ); |
||
| 338 | |||
| 339 | if ( $result ) { |
||
| 340 | $payment->subscription_id = $subscription->get_id(); |
||
| 341 | |||
| 342 | $payment->recurring_type = 'first'; |
||
| 343 | $payment->start_date = $start_date; |
||
| 344 | $payment->end_date = $next_date; |
||
| 345 | |||
| 346 | $this->plugin->payments_data_store->update( $payment ); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get expiring subscriptions. |
||
| 352 | * |
||
| 353 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L715-L746 |
||
| 354 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L652-L712 |
||
| 355 | * |
||
| 356 | * @param DateTime $start_date The start date of the period to check for expiring subscriptions. |
||
| 357 | * @param DateTime $end_date The end date of the period to check for expiring subscriptions. |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function get_expiring_subscription_posts( DateTime $start_date, DateTime $end_date ) { |
||
| 361 | $args = array( |
||
| 362 | 'post_type' => 'pronamic_pay_subscr', |
||
| 363 | 'nopaging' => true, |
||
| 364 | 'orderby' => 'post_date', |
||
| 365 | 'order' => 'ASC', |
||
| 366 | 'post_status' => array( |
||
| 367 | 'subscr_pending', |
||
| 368 | 'subscr_expired', |
||
| 369 | 'subscr_failed', |
||
| 370 | 'subscr_active', |
||
| 371 | ), |
||
| 372 | 'meta_query' => array( |
||
| 373 | array( |
||
| 374 | 'key' => '_pronamic_subscription_expiry_date', |
||
| 375 | 'value' => array( |
||
| 376 | $start_date->format( DateTime::MYSQL ), |
||
| 377 | $end_date->format( DateTime::MYSQL ), |
||
| 378 | ), |
||
| 379 | 'compare' => 'BETWEEN', |
||
| 380 | 'type' => 'DATETIME', |
||
| 381 | ), |
||
| 382 | ), |
||
| 383 | ); |
||
| 384 | |||
| 385 | $query = new WP_Query( $args ); |
||
| 386 | |||
| 387 | return $query->posts; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Update subscription payments. |
||
| 392 | */ |
||
| 393 | public function update_subscription_payments() { |
||
| 438 | } |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Send renewal notices. |
||
| 444 | * |
||
| 445 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L652-L712 |
||
| 446 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L715-L746 |
||
| 447 | * @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/classes/class-sl-emails.php#L41-L126 |
||
| 448 | */ |
||
| 449 | public function send_subscription_renewal_notices() { |
||
| 450 | $interval = new DateInterval( 'P1W' ); // 1 week |
||
| 451 | |||
| 452 | $start_date = new DateTime( 'midnight', new DateTimeZone( 'UTC' ) ); |
||
| 453 | |||
| 454 | $end_date = clone $start_date; |
||
| 455 | $end_date->add( $interval ); |
||
| 456 | |||
| 457 | $expiring_subscription_posts = $this->get_expiring_subscription_posts( $start_date, $end_date ); |
||
| 458 | |||
| 459 | foreach ( $expiring_subscription_posts as $post ) { |
||
| 460 | $subscription = new Subscription( $post->ID ); |
||
| 461 | |||
| 462 | $expiry_date = $subscription->get_expiry_date(); |
||
| 463 | |||
| 464 | $sent_date_string = get_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week', true ); |
||
| 465 | |||
| 466 | if ( $sent_date_string ) { |
||
| 467 | $first_date = clone $expiry_date; |
||
| 468 | $first_date->sub( $interval ); |
||
| 469 | |||
| 470 | $sent_date = new DateTime( $sent_date_string, new DateTimeZone( 'UTC' ) ); |
||
| 471 | |||
| 472 | if ( $sent_date > $first_date ) { |
||
| 473 | // Prevent renewal notices from being sent more than once. |
||
| 474 | continue; |
||
| 475 | } |
||
| 476 | |||
| 477 | delete_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week' ); |
||
| 478 | } |
||
| 479 | |||
| 480 | do_action( 'pronamic_subscription_renewal_notice_' . $subscription->get_source(), $subscription ); |
||
| 481 | |||
| 482 | update_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week', $start_date->format( DateTime::MYSQL ) ); |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Payment status update. |
||
| 488 | * |
||
| 489 | * @param Payment $payment The status updated payment. |
||
| 490 | */ |
||
| 491 | public function payment_status_update( $payment ) { |
||
| 492 | // Check if the payment is connected to a subscription. |
||
| 493 | $subscription = $payment->get_subscription(); |
||
| 494 | |||
| 495 | if ( empty( $subscription ) ) { |
||
| 496 | // Payment not connected to a subscription, nothing to do. |
||
| 497 | return; |
||
| 498 | } |
||
| 499 | |||
| 500 | // Status. |
||
| 501 | $status_before = $subscription->get_status(); |
||
| 502 | $status_update = $status_before; |
||
| 503 | |||
| 504 | switch ( $payment->get_status() ) { |
||
| 505 | case Statuses::OPEN: |
||
| 506 | // @todo |
||
| 507 | break; |
||
| 508 | case Statuses::SUCCESS: |
||
| 509 | $status_update = Statuses::ACTIVE; |
||
| 510 | |||
| 511 | if ( isset( $subscription->expiry_date, $payment->end_date ) && $subscription->expiry_date < $payment->end_date ) { |
||
| 512 | $subscription->expiry_date = clone $payment->end_date; |
||
| 513 | } |
||
| 514 | |||
| 515 | break; |
||
| 516 | case Statuses::FAILURE: |
||
| 517 | case Statuses::CANCELLED: |
||
| 518 | case Statuses::EXPIRED: |
||
| 519 | $status_update = Statuses::CANCELLED; |
||
| 520 | |||
| 521 | break; |
||
| 522 | } |
||
| 523 | |||
| 524 | // The status of canceled or completed subscriptions will not be changed automatically. |
||
| 525 | if ( ! in_array( $status_before, array( Statuses::CANCELLED, Statuses::COMPLETED ), true ) ) { |
||
| 526 | $subscription->set_status( $status_update ); |
||
| 527 | } |
||
| 528 | |||
| 529 | // Update. |
||
| 530 | $result = $this->plugin->subscriptions_data_store->update( $subscription ); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Subscription status update. |
||
| 535 | * |
||
| 536 | * @param Subscription $subscription The status updated subscirption. |
||
| 537 | */ |
||
| 538 | public function log_subscription_status_update( $subscription, $can_redirect, $old_status, $new_status ) { |
||
| 543 | ) ); |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * CLI subscriptions test. |
||
| 548 | */ |
||
| 549 | public function cli_subscriptions_test() { |
||
| 590 | } |
||
| 591 | } |
||
| 592 |
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