WC_Stripe_Email_Failed_Preorder_Authentication   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 21.28 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 20
loc 94
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 20 20 2
A trigger() 0 9 4
A send_email() 0 10 2
A get_default_subject() 0 3 1
A get_default_heading() 0 3 1
A get_custom_message() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit; // Exit if accessed directly.
4
}
5
6
/**
7
 * Failed Renewal/Pre-Order Authentication Notification
8
 *
9
 * @extends WC_Stripe_Email_Failed_Authentication
10
 */
11
class WC_Stripe_Email_Failed_Preorder_Authentication extends WC_Stripe_Email_Failed_Authentication {
12
	/**
13
	 * Holds the message, which is entered by admins when sending the email.
14
	 *
15
	 * @var string
16
	 */
17
	protected $custom_message;
18
19
	/**
20
	 * Constructor.
21
	 *
22
	 * @param WC_Email[] $email_classes All existing instances of WooCommerce emails.
23
	 */
24 View Code Duplication
	public function __construct( $email_classes = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
25
		$this->id             = 'failed_preorder_sca_authentication';
26
		$this->title          = __( 'Pre-order Payment Action Needed', 'woocommerce-gateway-stripe' );
27
		$this->description    = __( 'This is an order notification sent to the customer once a pre-order is complete, but additional payment steps are required.', 'woocommerce-gateway-stripe' );
28
		$this->customer_email = true;
29
30
		$this->template_html  = 'emails/failed-preorder-authentication.php';
31
		$this->template_plain = 'emails/plain/failed-preorder-authentication.php';
32
		$this->template_base  = plugin_dir_path( WC_STRIPE_MAIN_FILE ) . 'templates/';
33
34
		// Use the "authentication required" hook to add the correct, later hook.
35
		add_action( 'wc_gateway_stripe_process_payment_authentication_required', array( $this, 'trigger' ) );
36
37
		if ( isset( $email_classes['WC_Pre_Orders_Email_Pre_Order_Available'] ) ) {
38
			$this->original_email = $email_classes['WC_Pre_Orders_Email_Pre_Order_Available'];
39
		}
40
41
		// We want all the parent's methods, with none of its properties, so call its parent's constructor, rather than my parent constructor.
42
		parent::__construct();
43
	}
44
45
	/**
46
	 * When autnentication is required, this adds another action to `wc_pre_orders_pre_order_completed`
47
	 * in order to send the authentication required email when the custom pre-orders message is available.
48
	 *
49
	 * @param WC_Order $order The order whose payment is failing.
50
	 */
51
	public function trigger( $order ) {
52
		if ( class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order->get_id() ) ) {
53
			if ( isset( $this->original_email ) ) {
54
				remove_action( 'wc_pre_order_status_completed_notification', array( $this->original_email, 'trigger' ), 10, 2 );
55
			}
56
57
			add_action( 'wc_pre_orders_pre_order_completed', array( $this, 'send_email' ), 10, 2 );
58
		}
59
	}
60
61
	/**
62
	 * Triggers the email while also disconnecting the original Pre-Orders email.
63
	 *
64
	 * @param WC_Order $order The order that is being paid.
65
	 * @param string   $message The message, which should be added to the email.
66
	 */
67
	public function send_email( $order, $message ) {
68
		$this->custom_message = $message;
69
70
		parent::trigger( $order );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (trigger() instead of send_email()). Are you sure this is correct? If so, you might want to change this to $this->trigger().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
71
72
		// Restore the action of the original email for other bulk actions.
73
		if ( isset( $this->original_email ) ) {
74
			add_action( 'wc_pre_order_status_completed_notification', array( $this->original_email, 'trigger' ), 10, 2 );
75
		}
76
	}
77
78
	/**
79
	 * Returns the default subject of the email (modifyable in settings).
80
	 *
81
	 * @return string
82
	 */
83
	public function get_default_subject() {
84
		return __( 'Payment authorization needed for pre-order {order_number}', 'woocommerce-gateway-stripe' );
85
	}
86
87
	/**
88
	 * Returns the default heading of the email (modifyable in settings).
89
	 *
90
	 * @return string
91
	 */
92
	public function get_default_heading() {
93
		return __( 'Payment authorization needed for pre-order {order_number}', 'woocommerce-gateway-stripe' );
94
	}
95
96
	/**
97
	 * Returns the custom message, entered by the admin.
98
	 *
99
	 * @return string
100
	 */
101
	public function get_custom_message() {
102
		return $this->custom_message;
103
	}
104
}
105