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 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 |
||
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; |
||
|
|||
113 | $this->_messenger_collection_loader->load_messengers_from_folder(); |
||
114 | $this->_message_type_collection_loader = $Message_Type_Collection_Loader; |
||
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(); |
||
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 ) { |
|
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(); |
||
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 ) { |
|
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 ) { |
|
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 ) { |
|
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 ) { |
||
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() { |
||
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 ) { |
||
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 ); |
||
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 ) { |
||
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 ) { |
||
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( |
||
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() ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
744 | |||
745 | |||
746 | } |
||
747 | // End of file EE_Message_Resource_Manager.lib.php |
||
748 | // Location: /EE_Message_Resource_Manager.lib.php |
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..