Completed
Branch FET-9046-messages-queue (d125c6)
by
unknown
486:55 queued 472:32
created

EE_Message_Resource_Manager   D

Complexity

Total Complexity 80

Size/Duplication

Total Lines 727
Duplicated Lines 5.91 %

Coupling/Cohesion

Components 1
Dependencies 9
Metric Value
wmc 80
lcom 1
cbo 9
dl 43
loc 727
rs 4.4444

34 Methods

Rating   Name   Duplication   Size   Complexity  
A update_active_messengers_option() 0 3 1
A get_has_activated_messengers_option() 0 3 1
A update_has_activated_messengers_option() 0 3 1
A __construct() 0 14 1
A messenger_collection() 0 3 1
A active_messengers() 0 3 1
A get_messenger() 0 3 1
A get_active_messenger() 0 3 2
A installed_messengers() 0 11 3
A valid_messenger() 12 12 2
A message_type_collection() 0 3 1
A active_message_types() 0 3 1
A get_message_type() 0 3 1
A get_active_message_type_for_messenger() 0 5 2
A get_active_message_types_for_messenger() 0 14 4
A list_of_active_message_types() 7 11 4
A get_active_message_type_objects() 7 12 4
A installed_message_types() 0 10 3
A valid_message_type() 12 12 2
A valid_message_type_for_messenger() 0 17 2
A get_active_messengers_option() 0 3 1
A reset_active_messengers_and_message_types() 0 3 1
B _set_active_messengers_and_message_types() 0 33 5
A ensure_messenger_is_active() 0 7 2
A ensure_message_type_is_active() 0 10 2
B activate_messenger() 0 22 4
A _activate_message_types() 0 21 3
A _add_settings_for_message_type() 0 13 3
A _set_messenger_has_activated_message_type() 0 11 3
A _add_settings_for_messenger() 5 8 3
A _deactivate_messenger() 0 6 1
A _deactivate_message_type() 0 9 2
A is_generating_messenger_and_active() 0 12 4
B get_all_contexts() 0 20 8

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 EE_Message_Resource_Manager 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 EE_Message_Resource_Manager, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
4
	exit( 'No direct script access allowed' );
5
}
6
7
8
9
/**
10
 * Class EE_Message_Resource_Manager
11
 *
12
 * Description
13
 *
14
 * @package       Event Espresso
15
 * @subpackage    core
16
 * @author        Brent Christensen
17
 * @since         $VID:$
18
 *
19
 */
20
class EE_Message_Resource_Manager {
21
22
	/**
23
	 * @type EE_Messenger_Collection $_messenger_collection_loader
24
	 */
25
	protected $_messenger_collection_loader;
26
27
	/**
28
	 * @type EE_Message_Type_Collection $_message_type_collection_loader
29
	 */
30
	protected $_message_type_collection_loader;
31
32
	/**
33
	 * @type EEM_Message_Template_Group $_message_template_group_model
34
	 */
35
	protected $_message_template_group_model;
36
37
	/**
38
	 * @type EE_Messenger[]
39
	 */
40
	protected $_installed_messengers = array();
41
42
	/**
43
	 * @type EE_message_type[]
44
	 */
45
	protected $_installed_message_types = array();
46
47
	/**
48
	 * Array of active messengers.
49
	 * Format is this:
50
	 * array(
51
	 *      'messenger_name' => EE_messenger
52
	 * )
53
	 *
54
	 * @type EE_Messenger[]
55
	 */
56
	protected $_active_messengers = array();
57
58
	/**
59
	 * Formatted array of active message types grouped per messenger.
60
	 * Format is this:
61
	 * array(
62
	 *      'messenger_name' => array(
63
	 *          'settings' => array(
64
	 *              '{messenger_name}-message_types' => array(
65
	 *                  'message_type_name' => array() //variable array of settings corresponding to message type.
66
	 *              )
67
	 *          )
68
	 *      )
69
	 * )
70
	 *
71
	 * @type array
72
	 */
73
	protected $_active_message_types = array();
74
75
	/**
76
	 * An array of unique message type contexts across all active message types.
77
	 *
78
	 * The array will be indexed by either 'slugs' or 'all'.
79
	 * The slugs index contains an array indexed by unique context slugs with the latest label representation for that slug.
80
	 * array(
81
	 *      'context_slug' => 'localized label for context obtained from latest message type in the loop'.
82
	 * );
83
	 *
84
	 * The all index returns an array in this format:
85
	 * array(
86
	 *      'message_type_name' => array(
87
	 *          'context_slug' => array(
88
	 *              'label' => 'localized label for context',
89
	 *              'description' => 'localized description for context'
90
	 *          )
91
	 *      )
92
	 * );
93
	 *
94
	 * @type array
95
	 */
96
	protected $_contexts = array();
97
98
99
100
	/**
101
	 * EE_Message_Resource_Manager constructor.
102
	 *
103
	 * @param \EE_Messenger_Collection_Loader    $Messenger_Collection_Loader
104
	 * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader
105
	 * @param \EEM_Message_Template_Group        $Message_Template_Group_Model
106
	 */
107
	function __construct(
108
		EE_Messenger_Collection_Loader $Messenger_Collection_Loader,
109
		EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader,
110
		EEM_Message_Template_Group $Message_Template_Group_Model
111
	) {
112
		$this->_messenger_collection_loader = $Messenger_Collection_Loader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $Messenger_Collection_Loader of type object<EE_Messenger_Collection_Loader> is incompatible with the declared type object<EE_Messenger_Collection> of property $_messenger_collection_loader.

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...
113
		$this->_messenger_collection_loader->load_messengers_from_folder();
114
		$this->_message_type_collection_loader = $Message_Type_Collection_Loader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $Message_Type_Collection_Loader of type object<EE_Message_Type_Collection_Loader> is incompatible with the declared type object<EE_Message_Type_Collection> of property $_message_type_collection_loader.

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...
115
		$this->_message_type_collection_loader->load_message_types_from_folder();
116
		$this->_message_template_group_model = $Message_Template_Group_Model;
117
		$this->_set_active_messengers_and_message_types();
118
		//$this->messenger_collection()->show_collection_classes();
119
		//$this->message_type_collection()->show_collection_classes();
120
	}
121
122
123
124
	/**
125
	 * @return EE_Messenger_Collection
126
	 */
127
	public function messenger_collection() {
128
		return $this->_messenger_collection_loader->messenger_collection();
0 ignored issues
show
Bug introduced by
The method messenger_collection() does not seem to exist on object<EE_Messenger_Collection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
	}
130
131
132
133
	/**
134
	 * @return EE_Messenger[]
135
	 */
136
	public function active_messengers() {
137
		return $this->_active_messengers;
138
	}
139
140
141
142
	/**
143
	 * @param string $messenger_name
144
	 * @return \EE_Messenger
145
	 */
146
	public function get_messenger( $messenger_name ) {
147
		return $this->messenger_collection()->get_by_info( $messenger_name );
148
	}
149
150
151
152
	/**
153
	 * This returns the corresponding EE_Messenger object for the given string if it is active.
154
	 *
155
	 * @param string $messenger
156
	 * @return EE_Messenger | null
157
	 */
158
	public function get_active_messenger( $messenger ) {
159
		return ! empty( $this->_active_messengers[ $messenger ] ) ? $this->_active_messengers[ $messenger ] : null;
160
	}
161
162
163
164
	/**
165
	 * @return \EE_Messenger[]
166
	 */
167
	public function installed_messengers() {
168
		if ( empty( $this->_installed_messengers ) ) {
169
			$this->_installed_messengers = array();
170
			$this->messenger_collection()->rewind();
171
			while ( $this->messenger_collection()->valid() ) {
172
				$this->_installed_messengers[ $this->messenger_collection()->current()->name ] = $this->messenger_collection()->current();
173
				$this->messenger_collection()->next();
174
			}
175
		}
176
		return $this->_installed_messengers;
177
	}
178
179
180
181
	/**
182
	 * @param string $messenger_name
183
	 * @return \EE_Messenger
184
	 * @throws \EE_Error
185
	 */
186 View Code Duplication
	public function valid_messenger( $messenger_name ) {
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
		$messenger = $this->get_messenger( $messenger_name );
188
		if ( $messenger instanceof EE_Messenger ) {
189
			return $messenger;
190
		}
191
		throw new EE_Error(
192
			sprintf(
193
				__( 'The "%1$s" messenger is either invalid or not installed', 'event_espresso' ),
194
				$messenger_name
195
			)
196
		);
197
	}
198
199
200
201
	/**
202
	 * @return EE_Message_Type_Collection
203
	 */
204
	public function message_type_collection() {
205
		return $this->_message_type_collection_loader->message_type_collection();
0 ignored issues
show
Bug introduced by
The method message_type_collection() does not seem to exist on object<EE_Message_Type_Collection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
206
	}
207
208
209
210
	/**
211
	 * @return array
212
	 */
213
	public function active_message_types() {
214
		return $this->_active_message_types;
215
	}
216
217
218
219
	/**
220
	 * @param string $message_type_name
221
	 * @return \EE_Message_Type
222
	 */
223
	public function get_message_type( $message_type_name ) {
224
		return $this->message_type_collection()->get_by_info( $message_type_name );
225
	}
226
227
228
229
	/**
230
	 * This returns the EE_message_type from the active message types array ( if present );
231
	 *
232
	 * @param string $messenger_name
233
	 * @param string $message_type_name
234
	 * @return \EE_Message_Type|null
235
	 */
236
	public function get_active_message_type_for_messenger( $messenger_name, $message_type_name ) {
237
		return ! empty( $this->_active_message_types[ $messenger_name ][ $message_type_name ] )
238
			? $this->get_message_type( $message_type_name )
239
			: null;
240
	}
241
242
243
244
	/**
245
	 * This checks the _active_message_types property for any active message types
246
	 * that are present for the given messenger and returns them.
247
	 *
248
	 * @since 4.9.0
249
	 * @param string $messenger_name The messenger being checked
250
	 * @return EE_message_type[]    (or empty array if none present)
251
	 */
252
	public function get_active_message_types_for_messenger( $messenger_name ) {
253
		if ( empty( $this->_active_message_types[ $messenger_name ] ) ) {
254
			return array();
255
		}
256
		$message_types = array();
257
		$active_message_types = $this->_active_message_types[ $messenger_name ];
258
		$installed_message_types = $this->installed_message_types();
259
		foreach ( $active_message_types as $message_type_name => $settings ) {
260
			if ( ! empty( $installed_message_types[ $message_type_name ] ) ) {
261
				$message_types[] = $installed_message_types[ $message_type_name ];
262
			}
263
		}
264
		return $message_types;
265
	}
266
267
268
269
	/**
270
	 * This does NOT return the _active_message_types property but
271
	 * simply returns an array of active message type names from that property.
272
	 * (The _active_message_types property is indexed by messenger and active message_types per messenger).
273
	 *
274
	 * @return array message_type references (string)
275
	 */
276
	public function list_of_active_message_types() {
277
		$message_types = array();
278 View Code Duplication
		foreach ( $this->_active_message_types as $messenger => $message_type_data ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
279
			foreach ( $message_type_data as $message_type => $config ) {
280
				if ( ! in_array( $message_type, $message_types ) ) {
281
					$message_types[] = $message_type;
282
				}
283
			}
284
		}
285
		return $message_types;
286
	}
287
288
289
290
	/**
291
	 * Same as list_of_active_message_types() except this returns actual EE_message_type objects
292
	 *
293
	 * @since 4.9.0
294
	 * @return \EE_message_type[]
295
	 */
296
	public function get_active_message_type_objects() {
297
		$message_types = array();
298
		$installed_message_types = $this->installed_message_types();
299 View Code Duplication
		foreach ( $this->_active_message_types as $messenger => $message_type_data ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
300
			foreach ( $message_type_data as $message_type => $config ) {
301
				if ( ! isset( $message_type, $message_types ) ) {
302
					$message_types[ $message_type ] = $installed_message_types[ $message_type ];
303
				}
304
			}
305
		}
306
		return $message_types;
307
	}
308
309
310
311
	/**
312
	 * @return \EE_Message_Type[]
313
	 */
314
	public function installed_message_types() {
315
		if ( empty( $this->_installed_message_types ) ) {
316
			$this->message_type_collection()->rewind();
317
			while ( $this->message_type_collection()->valid() ) {
318
				$this->_installed_message_types[ $this->message_type_collection()->current()->name ] = $this->message_type_collection()->current();
319
				$this->message_type_collection()->next();
320
			}
321
		}
322
		return $this->_installed_message_types;
323
	}
324
325
326
	/**
327
	 * @param string $message_type_name
328
	 * @return \EE_message_type
329
	 * @throws \EE_Error
330
	 */
331 View Code Duplication
	public function valid_message_type( $message_type_name ) {
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...
332
		$message_type = $this->get_message_type( $message_type_name );
333
		if ( $message_type instanceof EE_message_type ) {
334
			return $message_type;
335
		}
336
		throw new EE_Error(
337
			sprintf(
338
				__( 'The "%1$s" message type is either invalid or not installed', 'event_espresso' ),
339
				$message_type_name
340
			)
341
		);
342
	}
343
344
345
346
	/**
347
	 * valid_message_type_for_messenger
348
	 *
349
	 * @param EE_Messenger $messenger
350
	 * @param string $message_type_name
351
	 * @return array
352
	 * @throws \EE_Error
353
	 */
354
	public function valid_message_type_for_messenger( EE_Messenger $messenger, $message_type_name ) {
355
		$valid_message_types = $messenger->get_valid_message_types();
356
		if ( ! in_array( $message_type_name, $valid_message_types ) ) {
357
			throw new EE_Error(
358
				sprintf(
359
					__(
360
						'The message type (%1$s) sent to %2$s is not valid for the %3$s messenger.  Double-check the spelling and verify that message type has been registered as a valid type with the messenger.',
361
						'event_espresso'
362
					),
363
					$message_type_name,
364
					__METHOD__,
365
					$messenger
366
				)
367
			);
368
		}
369
		return true;
370
	}
371
372
373
	/**
374
	 * Used to return active messengers array stored in the wp options table.
375
	 * If no value is present in the option then an empty array is returned.
376
	 *
377
	 * @return array
378
	 */
379
	public function get_active_messengers_option() {
380
		return get_option( 'ee_active_messengers', array() );
381
	}
382
383
384
385
	/**
386
	 * Used to update the active messengers array stored in the wp options table.
387
	 *
388
	 * @param array $active_messengers Incoming data to save.
389
	 * @return bool FALSE if not updated, TRUE if updated.
390
	 */
391
	public function update_active_messengers_option( $active_messengers ) {
392
		return update_option( 'ee_active_messengers', $active_messengers );
393
	}
394
395
396
397
	/**
398
	 * Used to return active messengers array stored in the wp options table.
399
	 * If no value is present in the option then an empty array is returned.
400
	 *
401
	 * @return array
402
	 */
403
	public function get_has_activated_messengers_option() {
404
			get_option( 'ee_has_activated_messenger', array() );
405
	}
406
407
408
409
	/**
410
	 * Used to update the active messengers array stored in the wp options table.
411
	 *
412
	 * @param array $has_activated_messengers Incoming data to save.
413
	 * @return bool FALSE if not updated, TRUE if updated.
414
	 */
415
	public function update_has_activated_messengers_option( $has_activated_messengers ) {
416
		return update_option( 'ee_has_activated_messenger', $has_activated_messengers );
417
	}
418
419
420
421
	/**
422
	 * wrapper for _set_active_messengers_and_message_types()
423
	 */
424
	public function reset_active_messengers_and_message_types() {
425
		$this->_set_active_messengers_and_message_types();
426
	}
427
428
429
430
	/**
431
	 * Generate list of active messengers and message types from collection.
432
	 * This sets up the active messengers from what is present in the database.
433
	 */
434
	protected function _set_active_messengers_and_message_types() {
435
		// list of activated messengers as set via the admin
436
		$active_messengers = $this->get_active_messengers_option();
437
		$active_messengers = is_array( $active_messengers ) ? $active_messengers : array( $active_messengers );
438
		$not_installed = array();
439
		foreach ( $active_messengers as $active_messenger => $data ) {
440
			// check if supposedly active messenger is actually installed by looking in our collection
441
			if ( $this->messenger_collection()->has_by_name( $active_messenger ) ) {
442
				$this->activate_messenger( $active_messenger, array(), false );
443
				// grab installed messenger object
444
				//$this->_active_messengers[ $active_messenger ] = $this->messenger_collection()->get_by_info(
445
				//	$active_messenger
446
				//);
447
				//
448
				//$this->_active_message_types[ $active_messenger ] = ! empty( $data[ 'settings' ][ $active_messenger . '-message_types' ] )
449
				//	? $data[ 'settings' ][ $active_messenger . '-message_types' ]
450
				//	: array();
451
			} else {
452
				$not_installed[] = $active_messenger;
453
				$this->_deactivate_messenger( $active_messenger );
454
			}
455
		}
456
		if ( ! empty( $not_installed ) ) {
457
			EE_Error::add_error(
458
				sprintf(
459
					__( 'The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso' ),
460
					'<br />',
461
					implode( ', ', $not_installed )
462
				),
463
				__FILE__, __FUNCTION__, __LINE__
464
			);
465
		}
466
	}
467
468
469
470
	/**
471
	 * Ensures that the specified messenger is currently active.
472
	 * If not, activates it and its default message types.
473
	 *
474
	 * @param string $messenger_name
475
	 * @return boolean TRUE if it was PREVIOUSLY active, and FALSE if it was previously inactive
476
	 */
477
	public function ensure_messenger_is_active( $messenger_name ) {
478
		if ( ! isset( $this->_active_messengers[ $messenger_name ] ) ) {
479
			$this->activate_messenger( $messenger_name );
480
			return false;
481
		}
482
		return true;
483
	}
484
485
486
487
	/**
488
	 * Ensures that the specified message type for the given messenger is currently active, if not activates it.
489
	 * This ALSO ensures that the given messenger is active as well!
490
	 *
491
	 * @param string $message_type message type name
492
	 * @param        $messenger_name
493
	 * @return array
494
	 * @throws \EE_Error
495
	 */
496
	public function ensure_message_type_is_active( $message_type, $messenger_name ) {
497
		// grab the messenger to work with.
498
		$messenger = $this->valid_messenger( $messenger_name );
499
		if ( $this->valid_message_type_for_messenger( $messenger, $message_type ) ) {
500
			//all is good so let's just get it active
501
			$this->_activate_message_types( $messenger, array( $message_type ) );
502
			$this->update_active_messengers_option( $this->_active_message_types );
503
		}
504
		return $this->active_message_types();
505
	}
506
507
508
509
	/**
510
	 * Activates the specified messenger.
511
	 *
512
	 * @param string $messenger_name
513
	 * @param array  $message_type_names        An array of message type names to activate with this messenger.
514
	 *                                          If included we do NOT setup the default message types
515
	 *                                          (assuming they are already setup.)
516
	 * @param bool   $update_active_messengers_option
517
	 * @return array of generated templates
518
	 * @throws \EE_Error
519
	 */
520
	public function activate_messenger(
521
		$messenger_name,
522
		$message_type_names = array(),
523
		$update_active_messengers_option = true
524
	) {
525
		$templates = array();
526
		// grab the messenger to work with.
527
		$messenger = $this->messenger_collection()->get_by_info( $messenger_name );
528
		// it's inactive. Activate it.
529
		if ( $messenger instanceof EE_Messenger ) {
530
			$this->_active_messengers[ $messenger->name ] = $messenger;
531
			$message_type_names = $this->_activate_message_types( $messenger, $message_type_names );
532
			if ( $update_active_messengers_option ) {
533
				$this->update_active_messengers_option( $this->_active_message_types );
534
			}
535
			// might need to generate new templates
536
			if ( ! empty( $message_type_names ) ) {
537
				$templates = EEH_MSG_Template::generate_new_templates( $messenger->name, $message_type_names, 0, true );
538
			}
539
		}
540
		return $templates;
541
	}
542
543
544
	/**
545
	 * Activates given message types for the given EE_Messenger object.
546
	 *
547
	 * @param \EE_Messenger $messenger
548
	 * @param  array        $message_type_names
549
	 *
550
	 * @return array
551
	 */
552
	protected function _activate_message_types( EE_Messenger $messenger, $message_type_names = array() ) {
553
		// get has_active so we can be sure its kept up to date.
554
		$has_activated = $this->get_has_activated_messengers_option();
555
		// use incoming list of message types or if that's empty, then get defaults
556
		$message_type_names = ! empty( $message_type_names )
557
			? $message_type_names
558
			: $messenger->get_default_message_types();
559
		// cycle thru message types
560
		foreach ( $message_type_names as $message_type_name ) {
561
			$this->_add_settings_for_message_type( $messenger, $message_type_name );
562
			$has_activated = $this->_set_messenger_has_activated_message_type(
563
				$messenger,
564
				$has_activated,
0 ignored issues
show
Bug introduced by
It seems like $has_activated can also be of type null; however, EE_Message_Resource_Mana...ctivated_message_type() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
565
				$message_type_name
566
			);
567
		}
568
		// setup any initial settings for the messenger
569
		$this->_add_settings_for_messenger( $messenger );
570
		$this->update_has_activated_messengers_option( $has_activated );
0 ignored issues
show
Bug introduced by
It seems like $has_activated defined by $this->get_has_activated_messengers_option() on line 554 can also be of type null; however, EE_Message_Resource_Mana...ted_messengers_option() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
571
		return $message_type_names;
572
	}
573
574
575
576
	/**
577
	 * _get_settings_for_message_type
578
	 *
579
	 * @access protected
580
	 * @param \EE_Messenger $messenger
581
	 * @param  string       $message_type_name
582
	 */
583
	protected function _add_settings_for_message_type( EE_Messenger $messenger, $message_type_name ) {
584
		$settings = array();
585
		// get installed message type from collection
586
		$message_type = $this->message_type_collection()->get_by_info( $message_type_name );
587
		//we need to setup any initial settings for message types
588
		if ( $message_type instanceof EE_Message_Type ) {
589
			$settings_fields = $message_type->get_admin_settings_fields();
590
			foreach ( $settings_fields as $field => $values ) {
591
				$settings[ $field ] = $values[ 'default' ];
592
			}
593
		}
594
		$this->_active_message_types[ $messenger->name ][ 'settings' ][ $messenger->name . '-message_types' ][ $message_type_name ][ 'settings' ] = $settings;
595
	}
596
597
598
599
	/**
600
	 * _set_messenger_has_activated_message_type
601
	 *
602
	 * @access protected
603
	 * @param \EE_Messenger $messenger
604
	 * @param array         $has_activated
605
	 * @param string        $message_type_name
606
	 * @return array
607
	 */
608
	protected function _set_messenger_has_activated_message_type( EE_Messenger $messenger, $has_activated, $message_type_name ) {
609
		// make sure this messenger has a record in the has_activated array
610
		if ( ! isset( $has_activated[ $messenger->name ] ) ) {
611
			$has_activated[ $messenger->name ] = array();
612
		}
613
		// check if message type has already been added
614
		if ( ! in_array( $message_type_name, $has_activated[ $messenger->name ] ) ) {
615
			$has_activated[ $messenger->name ][] = $message_type_name;
616
		}
617
		return $has_activated;
618
	}
619
620
621
622
	/**
623
	 * _add_settings_for_messenger
624
	 *
625
	 * @access protected
626
	 * @param \EE_Messenger $messenger
627
	 */
628
	protected function _add_settings_for_messenger( EE_Messenger $messenger ) {
629
		$msgr_settings = $messenger->get_admin_settings_fields();
630 View Code Duplication
		if ( ! empty( $msgr_settings ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
631
			foreach ( $msgr_settings as $field => $value ) {
632
				$this->_active_message_types[ $messenger->name ][ 'settings' ][ $field ] = $value;
633
			}
634
		}
635
	}
636
637
638
639
	/**
640
	 * _deactivate_messenger
641
	 *
642
	 * @access protected
643
	 * @param  string $messenger name of messenger
644
	 * @return void
645
	 */
646
	protected function _deactivate_messenger( $messenger ) {
647
		unset( $this->_active_messengers[ $messenger ] );
648
		unset( $this->_active_message_types[ $messenger ] );
649
		$this->_message_template_group_model->deactivate_message_template_groups_for( $messenger );
650
		$this->update_active_messengers_option( $this->_active_message_types );
651
	}
652
653
654
	/**
655
	 * Deactivates a message type (note this will deactivate across all messenger's it is active on.
656
	 *
657
	 * @param  string $message_type_name name of message type being deactivated
658
	 */
659
	protected function _deactivate_message_type( $message_type_name ) {
660
		foreach ( $this->_active_message_types as $messenger => $settings ) {
661
			unset(
662
				$this->_active_message_types[ $messenger ][ 'settings' ][ $messenger . '-message_types' ][ $message_type_name ]
663
			);
664
		}
665
		$this->_message_template_group_model->deactivate_message_template_groups_for( '', $message_type_name );
666
		$this->update_active_messengers_option( $this->_active_message_types );
667
	}
668
669
670
671
	/**
672
	 * Used to verify if a message can be sent for the given messenger and message type
673
	 * and that it is a generating messenger (used for generating message templates).
674
	 *
675
	 * @param EE_Messenger    $messenger    messenger used in trigger
676
	 * @param EE_message_type $message_type message type used in trigger
677
	 *
678
	 * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send.
679
	 */
680
	public function is_generating_messenger_and_active( EE_Messenger $messenger, EE_message_type $message_type ) {
681
		//get the $messengers the message type says it can be used with.
682
		foreach ( $message_type->with_messengers() as $generating_messenger => $secondary_messengers ) {
683
			if (
684
				$messenger->name == $generating_messenger
685
				&& isset( $this->_active_message_types[ $generating_messenger ][ $message_type->name ] )
686
			) {
687
				return true;
688
			}
689
		}
690
		return false;
691
	}
692
693
694
695
	/**
696
	 * This returns all the contexts that are registered by all message types.
697
	 *
698
	 * If $slugs_only is true, then just an array indexed by unique context slugs with the latest label representation for that slug.
699
	 * array(
700
	 *      'context_slug' => 'localized label for context obtained from latest message type in the loop'.
701
	 * );
702
	 *
703
	 * If $slugs_only is false, then the format is:
704
	 * array(
705
	 *      'message_type_name' => array(
706
	 *          'context_slug' => array(
707
	 *              'label' => 'localized label for context',
708
	 *              'description' => 'localized description for context'
709
	 *          )
710
	 *      )
711
	 * );
712
	 *
713
	 * Keep in mind that although different message types may share the same context slugs, it is possible that the context
714
	 * is described differently by the message type.
715
	 *
716
	 * >>>>>>>>>>>> 1 usage in \EE_Message_List_Table::_get_table_filters()
717
	 * >>>>>>>>>>>> 1 usage in \EE_Message::context_label()
718
	 * >>>>>>>>>>>> 1 usage in \EE_messages_Test::test_get_all_contexts()
719
	 *
720
	 * @since 4.9.0
721
	 * @param   bool $slugs_only Whether to return an array of just slugs and labels (true) or all contexts indexed by message type.
722
	 * @return array
723
	 */
724
	public function get_all_contexts( $slugs_only = true ) {
725
		$key = $slugs_only ? 'slugs' : 'all';
726
		// check if contexts has been setup yet.
727
		if ( empty( $this->_contexts[ $key ] ) ) {
728
			// So let's get all active message type objects and loop through to get all unique contexts
729
			foreach ( $this->get_active_message_type_objects() as $message_type ) {
730
				if ( $message_type instanceof EE_message_type ) {
731
					$message_type_contexts = $message_type->get_contexts();
732
					if ( $slugs_only ) {
733
						foreach ( $message_type_contexts as $context => $context_details ) {
734
							$this->_contexts[ $key ][ $context ] = $context_details[ 'label' ];
735
						}
736
					} else {
737
						$this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts;
738
					}
739
				}
740
			}
741
		}
742
		return ! empty( $this->_contexts[ $key ] ) ? $this->_contexts[ $key ] : array();
743
	}
744
745
746
}
747
// End of file EE_Message_Resource_Manager.lib.php
748
// Location: /EE_Message_Resource_Manager.lib.php