Completed
Push — master ( 0691eb...d055d1 )
by Stephanie
03:44 queued 01:07
created

FrmInbox   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 225
Duplicated Lines 12.89 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 29
loc 225
rs 7.44
c 0
b 0
f 0
wmc 52
lcom 2
cbo 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set_cache_key() 0 3 1
A api_url() 0 3 1
A get_messages() 0 3 1
A set_messages() 0 13 2
A add_api_messages() 0 10 3
A add_message() 0 25 4
A fill_message() 0 12 1
C clean_messages() 0 16 14
A mark_read() 12 12 3
A mark_unread() 0 7 4
A dismiss() 17 17 4
A dismiss_all() 0 13 4
A unread() 0 10 4
A unread_html() 0 8 2
A remove() 0 6 2
A update_list() 0 3 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like FrmInbox often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FrmInbox, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @since 4.05
4
 */
5
class FrmInbox extends FrmFormApi {
6
7
	protected $cache_key;
8
9
	private $option = 'frm_inbox';
10
11
	private $messages = false;
12
13
	public function __construct( $for_parent = null ) {
14
		$this->set_cache_key();
15
		$this->set_messages();
16
	}
17
18
	/**
19
	 * @since 4.05
20
	 */
21
	protected function set_cache_key() {
22
		$this->cache_key = 'frm_inbox_cache';
23
	}
24
25
	/**
26
	 * @since 4.05
27
	 */
28
	protected function api_url() {
29
		return 'https://formidableforms.com/wp-json/inbox/v1/message/';
30
	}
31
32
	/**
33
	 * @since 4.05
34
	 */
35
	public function get_messages() {
36
		return $this->messages;
37
	}
38
39
	/**
40
	 * @since 4.05
41
	 */
42
	public function set_messages() {
43
		$this->messages = get_option( $this->option );
44
		if ( empty( $this->messages ) ) {
45
			$this->messages = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type boolean of property $messages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
		}
47
48
		$this->add_api_messages();
49
50
		/**
51
		 * Messages are in an array.
52
		 */
53
		$this->messages = apply_filters( 'frm_inbox', $this->messages );
54
	}
55
56
	/**
57
	 * @since 4.05
58
	 */
59
	private function add_api_messages() {
60
		$api = $this->get_api_info();
61
		if ( empty( $api ) ) {
62
			return;
63
		}
64
65
		foreach ( $api as $message ) {
66
			$this->add_message( $message );
67
		}
68
	}
69
70
	/**
71
	 * @param array $message
72
	 */
73
	public function add_message( $message ) {
74
		if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) {
75
			// Don't replace messages unless required.
76
			return;
77
		}
78
79
		if ( isset( $this->messages[ $message['key'] ] ) ) {
80
			// Move up and mark as new.
81
			unset( $this->messages[ $message['key'] ] );
82
		}
83
84
		$message = $this->fill_message( $message );
85
		$this->messages[ $message['key'] ] = array(
86
			'created' => $message['time'],
87
			'message' => $message['message'],
88
			'subject' => $message['subject'],
89
			'icon'    => $message['icon'],
90
			'cta'     => $message['cta'],
91
			'expires' => $message['expires'],
92
		);
93
94
		$this->update_list();
95
96
		$this->clean_messages();
97
	}
98
99
	private function fill_message( $message ) {
100
		$defaults = array(
101
			'time'    => time(),
102
			'message' => '',
103
			'subject' => '',
104
			'icon'    => 'frm_tooltip_icon',
105
			'cta'     => '',
106
			'expires' => false,
107
		);
108
109
		return array_merge( $defaults, $message );
110
	}
111
112
	private function clean_messages() {
113
		$removed  = false;
114
		foreach ( $this->messages as $t => $message ) {
0 ignored issues
show
Bug introduced by
The expression $this->messages of type boolean is not traversable.
Loading history...
115
			$read    = isset( $message['read'] ) && ! empty( $message['read'] ) && isset( $message['read'][ get_current_user_id() ] ) && $message['read'][ get_current_user_id() ] < strtotime( '-1 month' );
116
			$dismissed = isset( $message['dismissed'] ) && ! empty( $message['dismissed'] ) && isset( $message['dismissed'][ get_current_user_id() ] ) && $message['dismissed'][ get_current_user_id() ] < strtotime( '-1 week' );
117
			$expired = isset( $message['expires'] ) && ! empty( $message['expires'] ) && $message['expires'] < time();
118
			if ( $read || $expired || $dismissed ) {
119
				unset( $this->messages[ $t ] );
120
				$removed = true;
121
			}
122
		}
123
124
		if ( $removed ) {
125
			$this->update_list();
126
		}
127
	}
128
129
	/**
130
	 * @param string $key
131
	 */
132 View Code Duplication
	public function mark_read( $key ) {
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...
133
		if ( ! isset( $this->messages[ $key ] ) ) {
134
			return;
135
		}
136
137
		if ( ! isset( $this->messages[ $key ]['read'] ) ) {
138
			$this->messages[ $key ]['read'] = array();
139
		}
140
		$this->messages[ $key ]['read'][ get_current_user_id() ] = time();
141
142
		$this->update_list();
143
	}
144
145
	/**
146
	 * @param string $key
147
	 *
148
	 * @since 4.05.02
149
	 */
150
	public function mark_unread( $key ) {
151
		$is_read = isset( $this->messages[ $key ] ) && isset( $this->messages[ $key ]['read'] ) && isset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
152
		if ( $is_read ) {
153
			unset( $this->messages[ $key ]['read'][ get_current_user_id() ] );
154
			$this->update_list();
155
		}
156
	}
157
158
	/**
159
	 * @param string $key
160
	 */
161 View Code Duplication
	public function dismiss( $key ) {
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...
162
		if ( $key === 'all' ) {
163
			$this->dismiss_all();
164
			return;
165
		}
166
167
		if ( ! isset( $this->messages[ $key ] ) ) {
168
			return;
169
		}
170
171
		if ( ! isset( $this->messages[ $key ]['dismissed'] ) ) {
172
			$this->messages[ $key ]['dismissed'] = array();
173
		}
174
		$this->messages[ $key ]['dismissed'][ get_current_user_id() ] = time();
175
176
		$this->update_list();
177
	}
178
179
	/**
180
	 * @since 4.06
181
	 */
182
	private function dismiss_all() {
183
		$user_id = get_current_user_id();
184
		foreach ( $this->messages as $key => $message ) {
0 ignored issues
show
Bug introduced by
The expression $this->messages of type boolean is not traversable.
Loading history...
185
			if ( ! isset( $message['dismissed'] ) ) {
186
				$this->messages[ $key ]['dismissed'] = array();
187
			}
188
189
			if ( ! isset( $message['dismissed'][ $user_id ] ) ) {
190
				$this->messages[ $key ]['dismissed'][ $user_id ] = time();
191
			}
192
		}
193
		$this->update_list();
194
	}
195
196
	public function unread() {
197
		$messages = $this->get_messages();
198
		$user_id  = get_current_user_id();
199
		foreach ( $messages as $t => $message ) {
0 ignored issues
show
Bug introduced by
The expression $messages of type boolean is not traversable.
Loading history...
200
			if ( isset( $message['read'] ) && isset( $message['read'][ $user_id ] ) ) {
201
				unset( $messages[ $t ] );
202
			}
203
		}
204
		return $messages;
205
	}
206
207
	public function unread_html() {
208
		$html = '';
209
		$count = count( $this->unread() );
210
		if ( $count ) {
211
			$html = ' <span class="update-plugins frm_inbox_count"><span class="plugin-count">' . absint( $count ) . '</span></span>';
212
		}
213
		return $html;
214
	}
215
216
	/**
217
	 * @since 4.05.02
218
	 */
219
	public function remove( $key ) {
220
		if ( isset( $this->messages[ $key ] ) ) {
221
			unset( $this->messages[ $key ] );
222
			$this->update_list();
223
		}
224
	}
225
226
	private function update_list() {
227
		update_option( $this->option, $this->messages, 'no' );
228
	}
229
}
230