Test Setup Failed
Push — issue/3872 ( 690bc7 )
by Ravinder
08:20
created

Give_Cache_Setting   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 202
Duplicated Lines 16.83 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 34
loc 202
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_instance() 9 9 2
A setup() 0 7 1
A load_plugin_settings() 0 28 4
A __reload_plugin_settings() 0 9 2
A get_option() 0 13 3
A get_give_option() 25 25 2
A get_settings() 0 7 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
 * Class for managing plugin setting cache
4
 * Note: only use for internal purpose.
5
 *
6
 * @package     Give
7
 * @subpackage  Classes/Give_Cache_Setting
8
 * @copyright   Copyright (c) 2018, GiveWP
9
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
10
 * @since       2.4.0
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
class Give_Cache_Setting {
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...
19
	/**
20
	 * Instance.
21
	 *
22
	 * @since  2.4.0
23
	 * @access private
24
	 * @var Give_Cache_Setting
25
	 */
26
	static private $instance;
27
28
	/**
29
	 * Instance.
30
	 *
31
	 * @since  2.4.0
32
	 * @access private
33
	 * @var array
34
	 */
35
	static private $settings;
36
37
	/**
38
	 * List of core options
39
	 * @since  2.4.0
40
	 * @access private
41
	 * @var array
42
	 */
43
	static private $options = array(
44
		'give_settings',
45
		'give_version',
46
		'give_completed_upgrades',
47
	);
48
49
	/**
50
	 * Singleton pattern.
51
	 *
52
	 * @since  2.4.0
53
	 * @access private
54
	 */
55
	private function __construct() {
56
	}
57
58
59
	/**
60
	 * Get instance.
61
	 *
62
	 * @since  2.4.0
63
	 * @access public
64
	 * @return Give_Cache_Setting
65
	 */
66 View Code Duplication
	public static function get_instance() {
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...
67
		if ( null === static::$instance ) {
68
			self::$instance = new static();
69
70
			self::$instance->setup();
71
		}
72
73
		return self::$instance;
74
	}
75
76
	/**
77
	 * Setup
78
	 *
79
	 * @since  2.4.0
80
	 * @access private
81
	 */
82
	private function setup() {
83
		$this->load_plugin_settings();
84
85
		add_action( 'added_option', array( $this, '__reload_plugin_settings' )  );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
86
		add_action( 'updated_option', array( $this, '__reload_plugin_settings' )  );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
87
		add_action( 'deleted_option', array( $this, '__reload_plugin_settings' )  );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
88
	}
89
90
	/**
91
	 * Load plugin settings
92
	 *
93
	 * @since  2.4.0
94
	 * @access private
95
	 */
96
	private function load_plugin_settings() {
97
		global $wpdb;
98
99
		$cache = wp_cache_get( 'giveAllOptions','options' );
100
101
		// Load options from cache.
102
		if( false !== $cache ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
103
			self::$settings = $cache;
104
			return;
105
		}
106
107
		$options = '\'' . implode( '\',\'', self::$options ) . '\'';
108
109
		$tmp     = array();
110
		$sql     = "SELECT option_name, option_value FROM $wpdb->options WHERE option_name IN ({$options}) ";
111
		$results = $wpdb->get_results( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
112
113
		if ( ! empty( $results ) ) {
114
115
			/* @var  stdClass $result */
116
			foreach ( $results as $result ) {
117
				$tmp[ $result->option_name ] = maybe_unserialize( $result->option_value );
118
			}
119
120
			self::$settings = $tmp;
121
			wp_cache_set( 'giveAllOptions', $tmp, 'options' );
122
		}
123
	}
124
125
	/**
126
	 * Reload option when add, update or delete
127
	 * Note: only for internal logic
128
	 *
129
	 * @since 2.4.0
130
	 *
131
	 * @param $option_name
132
	 */
133
	public function __reload_plugin_settings( $option_name ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cache_Setting::__reload_plugin_settings" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
134
		// Bailout.
135
		if ( ! in_array( $option_name, self::$options ) ) {
136
			return;
137
		}
138
139
		wp_cache_delete( 'giveAllOptions', 'options' );
140
		$this->$this->load_plugin_settings();
141
	}
142
143
144
	/**
145
	 * Get option
146
	 *
147
	 * @since  2.4.0
148
	 * @access public
149
	 *
150
	 * @param      $option_name
151
	 * @param bool $default
152
	 *
153
	 * @return mixed
154
	 */
155
	public static function get_option( $option_name, $default = false ) {
156
		if ( in_array( $option_name, self::$options ) ) {
157
			$value = ! empty( self::$settings[ $option_name ] )
158
				? self::$settings[ $option_name ]
159
				: $default;
160
161
		} else {
162
163
			$value = self::get_option( $option_name, $default );
164
		}
165
166
		return $value;
167
	}
168
169
	/**
170
	 * Get option
171
	 *
172
	 * @since  2.4.0
173
	 * @access public
174
	 *
175
	 * @param      $setting_name
176
	 * @param bool $default
177
	 *
178
	 * @return mixed
179
	 */
180 View Code Duplication
	public static function get_give_option( $setting_name, $default = false ) {
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
		$give_settings = self::$settings['give_settings'];
182
183
		$value = ! empty( $give_settings[ $setting_name ] )
184
			? self::$settings[ $setting_name ]
185
			: $default;
186
187
		/**
188
		 * Filter the option
189
		 *
190
		 * @since 2.4.0
191
		 */
192
		$value = apply_filters( 'give_get_option', $value, $setting_name, $default );
193
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
194
195
		/**
196
		 * filter the specific option
197
		 *
198
		 * @since 2.4.0
199
		 */
200
		$value = apply_filters( "give_get_option_{$setting_name}", $value, $setting_name, $default );
201
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
202
203
		return $value;
204
	}
205
206
	/**
207
	 * Get plugin settings
208
	 *
209
	 * @since  2.4.0
210
	 * @access public
211
	 */
212
	public static function get_settings() {
213
214
		/**
215
		 * Filter the plugin setting
216
		 */
217
		return (array) apply_filters( 'give_get_settings', self::$settings['give_settings'] );
218
	}
219
}
220
221
Give_Cache_Setting::get_instance();
222