Test Failed
Push — issues/370 ( 90279e )
by Ravinder
05:35
created

Give_Email_Notification_Util   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 260
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 26
loc 260
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_instance() 0 7 2
A has_preview() 0 3 1
A has_recipient_field() 0 3 1
A is_notification_status_editable() 0 3 1
A is_content_type_editable() 0 3 1
A is_email_preview_has_header() 0 3 1
A is_email_preview() 0 3 1
A is_show_on_emails_setting_page() 0 3 1
A is_email_notification_active() 0 11 3
A get_formatted_email_type() 0 8 1
B get_value() 0 24 5
A can_preview_email() 13 13 4
A can_send_preview_email() 13 13 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
/**
3
 * Email Notification Util
4
 *
5
 * This class contains helper functions for  email notification.
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Emails
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       2.0
12
 */
13
14
// Exit if access directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
20
class Give_Email_Notification_Util {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
21
	/**
22
	 * Instance.
23
	 *
24
	 * @since  2.0
25
	 * @access static
26
	 * @var
27
	 */
28
	static private $instance;
29
30
	/**
31
	 * Singleton pattern.
32
	 *
33
	 * @since  2.0
34
	 * @access private
35
	 * Give_Email_Notification_Util constructor.
36
	 */
37
	private function __construct() {
38
	}
39
40
41
	/**
42
	 * Get instance.
43
	 *
44
	 * @since  2.0
45
	 * @access static
46
	 * @return static
47
	 */
48
	static function get_instance() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
		if ( null === static::$instance ) {
50
			self::$instance = new static();
51
		}
52
53
		return self::$instance;
54
	}
55
56
57
	/**
58
	 * Check if notification has preview field or not.
59
	 *
60
	 * @since  2.0
61
	 * @access public
62
	 *
63
	 * @param Give_Email_Notification $email
64
	 *
65
	 * @return bool
66
	 */
67
	public static function has_preview( Give_Email_Notification $email ) {
68
		return $email->config['has_preview'];
69
	}
70
71
	/**
72
	 * Check if notification has recipient field or not.
73
	 *
74
	 * @since  2.0
75
	 * @access public
76
	 *
77
	 * @param Give_Email_Notification $email
78
	 *
79
	 * @return bool
80
	 */
81
	public static function has_recipient_field( Give_Email_Notification $email ) {
82
		return $email->config['has_recipient_field'];
83
	}
84
85
	/**
86
	 * Check if admin can edit notification status or not.
87
	 *
88
	 * @since  2.0
89
	 * @access public
90
	 *
91
	 * @param Give_Email_Notification $email
92
	 *
93
	 * @return bool
94
	 */
95
	public static function is_notification_status_editable( Give_Email_Notification $email ) {
96
		return $email->config['notification_status_editable'];
97
	}
98
99
	/**
100
	 * Check if admin can edit notification status or not.
101
	 *
102
	 * @since  2.0
103
	 * @access public
104
	 *
105
	 * @param Give_Email_Notification $email
106
	 *
107
	 * @return bool
108
	 */
109
	public static function is_content_type_editable( Give_Email_Notification $email ) {
110
		return $email->config['content_type_editable'];
111
	}
112
113
	/**
114
	 * Check email preview header active or not.
115
	 *
116
	 * @since  2.0
117
	 * @access public
118
	 *
119
	 * @param Give_Email_Notification $email
120
	 *
121
	 * @return bool
122
	 */
123
	public static function is_email_preview_has_header( Give_Email_Notification $email ) {
124
		return $email->config['has_preview_header'];
125
	}
126
127
	/**
128
	 * Check email preview header active or not.
129
	 *
130
	 * @since  2.0
131
	 * @access public
132
	 *
133
	 * @param Give_Email_Notification $email
134
	 *
135
	 * @return bool
136
	 */
137
	public static function is_email_preview( Give_Email_Notification $email ) {
138
		return $email->config['has_preview'];
139
	}
140
141
	/**
142
	 * Check if email notification setting appear on emails setting page or not.
143
	 *
144
	 * @since  2.0
145
	 * @access public
146
	 *
147
	 * @param Give_Email_Notification $email
148
	 *
149
	 * @return bool
150
	 */
151
	public static function is_show_on_emails_setting_page( Give_Email_Notification $email ){
152
		return $email->config['show_on_emails_setting_page'];
153
	}
154
155
156
	/**
157
	 * Check email active or not.
158
	 *
159
	 * @since  2.0
160
	 * @access public
161
	 *
162
	 * @param Give_Email_Notification $email
163
	 * @param int $form_id
164
	 *
165
	 * @return string
166
	 */
167
	public static function is_email_notification_active( Give_Email_Notification $email, $form_id = null ) {
168
		$notification_status = $email->get_notification_status( $form_id );
169
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
170
171
		$notification_status = empty( $form_id )
172
			? give_is_setting_enabled( $notification_status )
0 ignored issues
show
Documentation introduced by
$notification_status is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
173
			: give_is_setting_enabled( give_get_option( "{$email->config['id']}_notification", $email->config['notification_status'] ) ) && give_is_setting_enabled( $notification_status, array( 'enabled', 'global' ) );
0 ignored issues
show
Documentation introduced by
$notification_status is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array('enabled', 'global') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
174
			// To check if email notification active or not on per form basis, email notification must be globally active other it will consider as disable.
175
176
		return $notification_status;
177
	}
178
179
	/**
180
	 * Check if admin preview email or not
181
	 *
182
	 * @since  2.0
183
	 * @access public
184
	 * @return bool   $is_preview
185
	 */
186 View Code Duplication
	public static function can_preview_email() {
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...
187
		$is_preview = false;
188
189
		if (
190
			current_user_can( 'manage_give_settings' )
191
			&& ! empty( $_GET['give_action'] )
192
			&& 'preview_email' === $_GET['give_action']
193
		) {
194
			$is_preview = true;
195
		}
196
197
		return $is_preview;
198
	}
199
200
	/**
201
	 * Check if admin preview email or not
202
	 *
203
	 * @since  2.0
204
	 * @access public
205
	 * @return bool   $is_preview
206
	 */
207 View Code Duplication
	public static function can_send_preview_email() {
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...
208
		$is_preview = false;
209
210
		if (
211
			current_user_can( 'manage_give_settings' )
212
			&& ! empty( $_GET['give_action'] )
213
			&& 'send_preview_email' === $_GET['give_action']
214
		) {
215
			$is_preview = true;
216
		}
217
218
		return $is_preview;
219
	}
220
221
222
	/**
223
	 * Get formatted text for email content type.
224
	 *
225
	 * @since  2.0
226
	 * @access public
227
	 *
228
	 * @param string $content_type
229
	 *
230
	 * @return string
231
	 */
232
	public static function get_formatted_email_type( $content_type ) {
233
		$email_contents = array(
234
			'text/html'  => __( 'HTML', 'give' ),
235
			'text/plain' => __( 'Plain', 'give' ),
236
		);
237
238
		return $email_contents[ $content_type ];
239
	}
240
241
242
	/**
243
	 * Get email notification option value.
244
	 *
245
	 * @since  2.0
246
	 * @access public
247
	 *
248
	 * @param Give_Email_Notification $email
249
	 * @param string                   $option_name
250
	 * @param int                      $form_id
251
	 * @param mixed                    $default
252
	 *
253
	 * @return mixed
254
	 */
255
	public static function get_value( Give_Email_Notification $email, $option_name, $form_id = null, $default = false ) {
256
		// If form id set then option name can be contain _give_ prefix which is only used for meta key,
257
		// So make sure you are using correct option name.
258
		$global_option_name = ( 0 === strpos( $option_name, '_give_' )
259
			? str_replace( '_give_', '', $option_name )
260
			: $option_name );
261
		$option_value = give_get_option( $global_option_name, $default );
262
263
		if (
264
			! empty( $form_id )
265
			&& give_is_setting_enabled( give_get_meta( $form_id, Give_Email_Setting_Field::get_prefix( $email, $form_id ) . 'notification', true, 'global' ) )
0 ignored issues
show
Documentation introduced by
'global' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
266
		) {
267
			$option_value = get_post_meta( $form_id, $option_name, true );
268
		}
269
270
		$option_value = empty( $option_value ) ? $default : $option_value;
271
272
		/**
273
		 * Filter the setting value
274
		 *
275
		 * @since 2.0
276
		 */
277
		return apply_filters( 'give_email_setting_value', $option_value, $option_name, $email, $form_id, $default );
278
	}
279
}
280