WC_Email_Customer_Refunded_Order   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 227
Duplicated Lines 9.69 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 22
loc 227
rs 10
wmc 15
lcom 2
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B set_email_strings() 0 25 2
A trigger_full() 0 3 1
A trigger_partial() 0 3 1
B trigger() 0 27 5
A get_subject() 0 3 1
A get_heading() 0 3 1
A get_content_html() 11 11 1
A get_content_plain() 11 11 1
A init_form_fields() 0 51 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
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
if ( ! class_exists( 'WC_Email_Customer_Refunded_Order' ) ) :
8
9
/**
10
 * Customer Refunded Order Email.
11
 *
12
 * Order refunded emails are sent to the customer when the order is marked refunded.
13
 *
14
 * @class    WC_Email_Customer_Refunded_Order
15
 * @version  2.4.0
16
 * @package  WooCommerce/Classes/Emails
17
 * @author   WooThemes
18
 * @extends  WC_Email
19
 */
20
class WC_Email_Customer_Refunded_Order extends WC_Email {
21
22
	/**
23
	 * Refund order.
24
	 *
25
	 * @var WC_Order|bool
26
	 */
27
	public $refund;
28
29
	/**
30
	 * Is the order partial refunded?
31
	 *
32
	 * @var bool
33
	 */
34
	public $partial_refund;
35
36
	/**
37
	 * Constructor.
38
	 */
39
	public function __construct() {
40
		$this->set_email_strings();
41
		$this->customer_email = true;
42
43
		// Triggers for this email
44
		add_action( 'woocommerce_order_fully_refunded_notification', array( $this, 'trigger_full' ), 10, 2 );
45
		add_action( 'woocommerce_order_partially_refunded_notification', array( $this, 'trigger_partial' ), 10, 2 );
46
47
		// Call parent constuctor
48
		parent::__construct();
49
	}
50
51
	/**
52
	 * Set email strings.
53
	 *
54
	 * @param bool $partial_refund
55
	 */
56
	public function set_email_strings( $partial_refund = false ) {
57
		$this->subject_partial     = $this->get_option( 'subject_partial', __( 'Your {site_title} order from {order_date} has been partially refunded', 'woocommerce' ) );
58
		$this->subject_full        = $this->get_option( 'subject_full', __( 'Your {site_title} order from {order_date} has been refunded', 'woocommerce' ) );
59
60
		$this->heading_full        = $this->get_option( 'heading_full', __( 'Your order has been fully refunded', 'woocommerce' ) );
61
		$this->heading_partial     = $this->get_option( 'heading_partial', __( 'Your order has been partially refunded', 'woocommerce' ) );
62
63
		$this->template_html  = 'emails/customer-refunded-order.php';
64
		$this->template_plain = 'emails/plain/customer-refunded-order.php';
65
66
		if ( $partial_refund ) {
67
			$this->id             = 'customer_partially_refunded_order';
68
			$this->title          = __( 'Partially Refunded order', 'woocommerce' );
69
			$this->description    = __( 'Order partially refunded emails are sent to customers when their orders are partially refunded.', 'woocommerce' );
70
			$this->heading        = $this->heading_partial;
71
			$this->subject        = $this->subject_partial;
72
		}
73
		else {
74
			$this->id             = 'customer_refunded_order';
75
			$this->title          = __( 'Refunded order', 'woocommerce' );
76
			$this->description    = __( 'Order refunded emails are sent to customers when their orders are marked refunded.', 'woocommerce' );
77
			$this->heading        = $this->heading_full;
78
			$this->subject        = $this->subject_full;
79
		}
80
	}
81
82
	/**
83
	 * Full refund notification.
84
	 *
85
	 * @param int $order_id
86
	 * @param int $refund_id
87
	 */
88
	public function trigger_full( $order_id, $refund_id = null ) {
89
		$this->trigger( $order_id, false, $refund_id );
90
	}
91
92
	/**
93
	 * Partial refund notification.
94
	 *
95
	 * @param int $order_id
96
	 * @param int $refund_id
97
	 */
98
	public function trigger_partial( $order_id, $refund_id = null ) {
99
		$this->trigger( $order_id, true, $refund_id );
100
	}
101
102
	/**
103
	 * Trigger.
104
	 *
105
	 * @param int $order_id
106
	 * @param bool $partial_refund
107
	 * @param int $refund_id
108
	 */
109
	public function trigger( $order_id, $partial_refund = false, $refund_id = null ) {
110
		$this->partial_refund = $partial_refund;
111
		$this->set_email_strings( $partial_refund );
112
113
		if ( $order_id ) {
114
			$this->object    = wc_get_order( $order_id );
0 ignored issues
show
Documentation Bug introduced by
It seems like wc_get_order($order_id) can also be of type false. However, the property $object is declared as type object. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
115
			$this->recipient = $this->object->billing_email;
116
117
			$this->find['order-date']      = '{order_date}';
118
			$this->find['order-number']    = '{order_number}';
119
120
			$this->replace['order-date']   = date_i18n( wc_date_format(), strtotime( $this->object->order_date ) );
121
			$this->replace['order-number'] = $this->object->get_order_number();
122
		}
123
124
		if ( ! empty( $refund_id ) ) {
125
			$this->refund = wc_get_order( $refund_id );
126
		} else {
127
			$this->refund = false;
128
		}
129
130
		if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
131
			return;
132
		}
133
134
		$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
135
	}
136
137
	/**
138
	 * Get email subject.
139
	 *
140
	 * @access public
141
	 * @return string
142
	 */
143
	public function get_subject() {
144
		return apply_filters( 'woocommerce_email_subject_customer_refunded_order', $this->format_string( $this->subject ), $this->object );
145
	}
146
147
	/**
148
	 * Get email heading.
149
	 *
150
	 * @access public
151
	 * @return string
152
	 */
153
	public function get_heading() {
154
		return apply_filters( 'woocommerce_email_heading_customer_refunded_order', $this->format_string( $this->heading ), $this->object );
155
	}
156
157
	/**
158
	 * Get content html.
159
	 *
160
	 * @access public
161
	 * @return string
162
	 */
163 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...
164
		return wc_get_template_html( $this->template_html, array(
165
			'order'          => $this->object,
166
			'refund'		 => $this->refund,
167
			'partial_refund' => $this->partial_refund,
168
			'email_heading'  => $this->get_heading(),
169
			'sent_to_admin'  => false,
170
			'plain_text'     => false,
171
			'email'			 => $this
172
		) );
173
	}
174
175
	/**
176
	 * Get content plain.
177
	 *
178
	 * @return string
179
	 */
180 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...
181
		return wc_get_template_html( $this->template_plain, array(
182
			'order'          => $this->object,
183
			'refund'		 => $this->refund,
184
			'partial_refund' => $this->partial_refund,
185
			'email_heading'  => $this->get_heading(),
186
			'sent_to_admin'  => false,
187
			'plain_text'     => true,
188
			'email'			 => $this
189
		) );
190
	}
191
192
	/**
193
	 * Initialise settings form fields.
194
	 */
195
	public function init_form_fields() {
196
		$this->form_fields = array(
197
			'enabled' => array(
198
				'title'   => __( 'Enable/Disable', 'woocommerce' ),
199
				'type'    => 'checkbox',
200
				'label'   => __( 'Enable this email notification', 'woocommerce' ),
201
				'default' => 'yes'
202
			),
203
			'subject_full' => array(
204
				'title'       => __( 'Full Refund Subject', 'woocommerce' ),
205
				'type'        => 'text',
206
				'description' => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->subject_full ),
207
				'placeholder' => '',
208
				'default'     => $this->subject_full,
209
				'desc_tip'      => true
210
			),
211
			'subject_partial' => array(
212
				'title'       => __( 'Partial Refund Subject', 'woocommerce' ),
213
				'type'        => 'text',
214
				'description' => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->subject_partial ),
215
				'placeholder' => '',
216
				'default'     => $this->subject_partial,
217
				'desc_tip'      => true
218
			),
219
			'heading_full' => array(
220
				'title'       => __( 'Full Refund Email Heading', 'woocommerce' ),
221
				'type'        => 'text',
222
				'description' => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->heading_full ),
223
				'placeholder' => '',
224
				'default'     => $this->heading_full,
225
				'desc_tip'      => true
226
			),
227
			'heading_partial' => array(
228
				'title'       => __( 'Partial Refund Email Heading', 'woocommerce' ),
229
				'type'        => 'text',
230
				'description' => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->heading_partial ),
231
				'placeholder' => '',
232
				'default'     => $this->heading_partial,
233
				'desc_tip'      => true
234
			),
235
			'email_type' => array(
236
				'title'       => __( 'Email type', 'woocommerce' ),
237
				'type'        => 'select',
238
				'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
239
				'default'     => 'html',
240
				'class'       => 'email_type wc-enhanced-select',
241
				'options'     => $this->get_email_type_options(),
242
				'desc_tip'      => true
243
			)
244
		);
245
	}
246
}
247
248
endif;
249
250
return new WC_Email_Customer_Refunded_Order();
251