Completed
Branch FET-9046-messages-queue (acf360)
by
unknown
1260:33 queued 1235:57
created

EE_Message_Resource_Manager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6
Metric Value
wmc 12
lcom 2
cbo 6
dl 0
loc 194
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A messenger_collection() 0 3 1
A message_type_collection() 0 3 1
A get_messenger() 0 3 1
A get_message_type() 0 3 1
A get_active_messengers_option() 0 6 1
A update_active_messengers_option() 0 3 1
A _set_active_messengers_and_message_types() 0 13 2
A _deactivate_messenger() 0 6 1
A _deactivate_message_type() 0 10 2
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
24
	/**
25
	 * @type EE_Messenger_Collection $_messenger_collection_loader
26
	 */
27
	protected $_messenger_collection_loader;
28
29
30
	/**
31
	 * @type EE_Message_Type_Collection $_message_type_collection_loader
32
	 */
33
	protected $_message_type_collection_loader;
34
35
36
	/**
37
	 * @type EEM_Message_Template_Group $_message_template_group_model
38
	 */
39
	protected $_message_template_group_model;
40
41
	/**
42
	 * Array of active messengers.
43
	 * Format is this:
44
	 * array(
45
	 *      'messenger_name' => EE_messenger
46
	 * )
47
	 *
48
	 * @type EE_Messenger[]
49
	 */
50
	protected $_active_messengers = array();
51
52
	/**
53
	 * Formatted array of active message types grouped per messenger.
54
	 * Format is this:
55
	 * array(
56
	 *      'messenger_name' => array(
57
	 *          'settings' => array(
58
	 *              '{messenger_name}-message_types' => array(
59
	 *                  'message_type_name' => array() //variable array of settings corresponding to message type.
60
	 *              )
61
	 *          )
62
	 *      )
63
	 * )
64
	 *
65
	 * @type array
66
	 */
67
	protected $_active_message_types = array();
68
69
70
71
	/**
72
	 * EE_Message_Resource_Manager constructor.
73
	 *
74
	 * @param \EE_Messenger_Collection_Loader    $Messenger_Collection_Loader
75
	 * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader
76
	 * @param \EEM_Message_Template_Group        $Message_Template_Group_Model
77
	 */
78
	function __construct(
79
		EE_Messenger_Collection_Loader $Messenger_Collection_Loader,
80
		EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader,
81
		EEM_Message_Template_Group $Message_Template_Group_Model
82
	) {
83
		$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...
84
		$this->_messenger_collection_loader->load_messengers_from_folder();
85
		$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...
86
		$this->_message_type_collection_loader->load_message_types_from_folder();
87
		$this->_message_template_group_model = $Message_Template_Group_Model;
88
		$this->_set_active_messengers_and_message_types();
89
	}
90
91
92
93
	/**
94
	 * @return EE_Messenger_Collection
95
	 */
96
	public function messenger_collection() {
97
		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...
98
	}
99
100
101
102
	/**
103
	 * @return EE_Message_Type_Collection
104
	 */
105
	public function message_type_collection() {
106
		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...
107
	}
108
109
110
111
	/**
112
	 * @param string $messenger_classname
113
	 * @return \EE_Messenger
114
	 */
115
	public function get_messenger( $messenger_classname ) {
116
		return $this->messenger_collection()->get_by_info( $messenger_classname );
117
	}
118
119
120
121
	/**
122
	 * @param string $message_type_classname
123
	 * @return \EE_Message_Type
124
	 */
125
	public function get_message_type( $message_type_classname ) {
126
		return $this->message_type_collection()->get_by_info( $message_type_classname );
127
	}
128
129
130
131
	/**
132
	 * Used to return active messengers array stored in the wp options table.
133
	 * If no value is present in the option then an empty array is returned.
134
	 *
135
	 * @return array
136
	 */
137
	public static function get_active_messengers_option() {
138
		return apply_filters(
139
			'FHEE__EE_Messenger_And_Message_Type_Manager__get_active_messengers_option',
140
			get_option( 'ee_active_messengers', array() )
141
		);
142
	}
143
144
145
146
	/**
147
	 * Used to update the active messengers array stored in the wp options table.
148
	 *
149
	 * @param array $active_messengers Incoming data to save.
150
	 * @return bool FALSE if not updated, TRUE if updated.
151
	 */
152
	public static function update_active_messengers_option( $active_messengers ) {
153
		return update_option( 'ee_active_messengers', $active_messengers );
154
	}
155
156
157
158
	/**
159
	 * _set_active_messengers_and_message_types
160
	 * generate list of active messengers and message types from collection
161
	 */
162
	protected function _set_active_messengers_and_message_types() {
163
		$this->messenger_collection()->rewind();
164
		while ( $this->messenger_collection()->valid() ) {
165
			$this->_active_messengers[ $this->messenger_collection()->current()->name ] = $this->messenger_collection()->current();
166
			$this->messenger_collection()->next();
167
168
			//$this->_active_message_types[ $name ] = ! empty( $_actives[ $name ][ 'settings' ][ $name . '-message_types' ] )
169
			//	? $_actives[ $name ][ 'settings' ][ $name . '-message_types' ]
170
			//	: array();
171
172
		}
173
174
	}
175
176
177
178
	/**
179
	 * _deactivate_messenger
180
	 *
181
	 * @access protected
182
	 * @param  string $messenger name of messenger
183
	 * @return void
184
	 */
185
	protected function _deactivate_messenger( $messenger ) {
186
		unset( $this->_active_messengers[ $messenger ] );
187
		unset( $this->_active_message_types[ $messenger ] );
188
		$this->_message_template_group_model->deactivate_message_template_groups_for( $messenger );
189
		EE_Message_Resource_Manager::update_active_messengers_option( $this->_active_messengers );
190
	}
191
192
193
194
	/**
195
	 * _deactivate_message_type
196
	 *
197
	 * @access protected
198
	 * @param  string $message_type name of message type
199
	 * @return void
200
	 */
201
	protected function _deactivate_message_type( $message_type ) {
202
		foreach ( $this->_active_messengers as $messenger => $settings ) {
203
			unset(
204
				$this->_active_messengers[ $messenger ][ 'settings' ][ $messenger . '-message_types' ][ $message_type ]
205
			);
206
		}
207
		$this->_message_template_group_model->deactivate_message_template_groups_for( '', $message_type );
208
		unset( $this->_active_message_types[ $message_type ] );
209
		EE_Message_Resource_Manager::update_active_messengers_option( $this->_active_messengers );
210
	}
211
212
213
}
214
// End of file EE_Message_Resource_Manager.lib.php
215
// Location: /EE_Message_Resource_Manager.lib.php