WC_Stripe_Email_Failed_Authentication   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 29.31 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 34
loc 116
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_content_html() 17 17 1
A get_content_plain() 17 17 1
A get_authorization_url() 0 3 1
A init_form_fields() 0 17 1
A trigger() 0 25 4

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
 * Base for Failed Renewal/Pre-Order Authentication Notifications.
8
 *
9
 * @extends WC_Email
10
 */
11
abstract class WC_Stripe_Email_Failed_Authentication extends WC_Email {
12
	/**
13
	 * An instance of the email, which would normally be sent after a failed payment.
14
	 *
15
	 * @var WC_Email
16
	 */
17
	public $original_email;
18
19
	/**
20
	 * Generates the HTML for the email while keeping the `template_base` in mind.
21
	 *
22
	 * @return string
23
	 */
24 View Code Duplication
	public function get_content_html() {
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
		ob_start();
26
		wc_get_template(
27
			$this->template_html,
28
			array(
29
				'order'             => $this->object,
30
				'email_heading'     => $this->get_heading(),
31
				'sent_to_admin'     => false,
32
				'plain_text'        => false,
33
				'authorization_url' => $this->get_authorization_url( $this->object ),
34
				'email'             => $this,
35
			),
36
			'',
37
			$this->template_base
38
		);
39
		return ob_get_clean();
40
	}
41
42
	/**
43
	 * Generates the plain text for the email while keeping the `template_base` in mind.
44
	 *
45
	 * @return string
46
	 */
47 View Code Duplication
	public function get_content_plain() {
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...
48
		ob_start();
49
		wc_get_template(
50
			$this->template_plain,
51
			array(
52
				'order'             => $this->object,
53
				'email_heading'     => $this->get_heading(),
54
				'sent_to_admin'     => false,
55
				'plain_text'        => true,
56
				'authorization_url' => $this->get_authorization_url( $this->object ),
57
				'email'             => $this,
58
			),
59
			'',
60
			$this->template_base
61
		);
62
		return ob_get_clean();
63
	}
64
65
	/**
66
	 * Generates the URL, which will be used to authenticate the payment.
67
	 *
68
	 * @param WC_Order $order The order whose payment needs authentication.
69
	 * @return string
70
	 */
71
	public function get_authorization_url( $order ) {
72
		return add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) );
73
	}
74
75
	/**
76
	 * Uses specific fields from `WC_Email_Customer_Invoice` for this email.
77
	 */
78
	public function init_form_fields() {
79
		parent::init_form_fields();
80
		$base_fields = $this->form_fields;
81
82
		$this->form_fields = array(
83
			'enabled'    => array(
84
				'title'   => _x( 'Enable/Disable', 'an email notification', 'woocommerce-gateway-stripe' ),
85
				'type'    => 'checkbox',
86
				'label'   => __( 'Enable this email notification', 'woocommerce-gateway-stripe' ),
87
				'default' => 'yes',
88
			),
89
90
			'subject'    => $base_fields['subject'],
91
			'heading'    => $base_fields['heading'],
92
			'email_type' => $base_fields['email_type'],
93
		);
94
	}
95
96
	/**
97
	 * Triggers the email.
98
	 *
99
	 * @param WC_Order $order The renewal order whose payment failed.
100
	 */
101
	public function trigger( $order ) {
102
		if ( ! $this->is_enabled() ) {
103
			return;
104
		}
105
106
		$this->object = $order;
107
108
		if ( method_exists( $order, 'get_billing_email' ) ) {
109
			$this->recipient = $order->get_billing_email();
110
		} else {
111
			$this->recipient = $order->billing_email;
112
		}
113
114
		$this->find['order_date'] = '{order_date}';
115
		if ( function_exists( 'wc_format_datetime' ) ) { // WC 3.0+
116
			$this->replace['order_date'] = wc_format_datetime( $order->get_date_created() );
117
		} else { // WC < 3.0
118
			$this->replace['order_date'] = $order->date_created->date_i18n( wc_date_format() );
119
		}
120
121
		$this->find['order_number']    = '{order_number}';
122
		$this->replace['order_number'] = $order->get_order_number();
123
124
		$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
125
	}
126
}
127