Completed
Push — master ( 071e73...82b9ae )
by J.D.
02:50
created

WordPoints_Hook_Reactor::__get()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 33
rs 5.2653
cc 11
eloc 18
nc 21
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Hook reactor class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Bootstrap for performing pre-scripted reactions when an event is fired.
12
 *
13
 * When a hook event fires, it is the job of the reactor to perform the action
14
 * specified for each reaction object. For most reactors this means that they must
15
 * "hit" a "target". For example, it might award points to a particular user.
16
 *
17
 * @since 1.0.0
18
 *
19
 * @property-read WordPoints_Hook_Reaction_StorageI|null $reactions
20
 *                Object for accessing hook reactions for this reactor based on the
21
 *                current network mode. If a reactor doesn't support network
22
 *                reactions and network mode is on, this property is not available.
23
 *
24
 * @property-read WordPoints_Hook_Reaction_StorageI|null $standard_reactions
25
 *                Object for accessing standard hook reactions for this reactor. Not
26
 *                available when network mode is on.
27
 *
28
 * @property-read WordPoints_Hook_Reaction_StorageI|null $network_reactions
29
 *                Object for accessing network hook reactions for this reactor. May
30
 *                not be available for all reactors.
31
 */
32
abstract class WordPoints_Hook_Reactor implements WordPoints_Hook_SettingsI {
33
34
	/**
35
	 * The unique slug identifying this hook reactor.
36
	 *
37
	 * @since 1.0.0
38
	 *
39
	 * @var string
40
	 */
41
	protected $slug;
42
43
	/**
44
	 * The types of args that this reactor can target.
45
	 *
46
	 * @since 1.0.0
47
	 *
48
	 * @var string|string[]
49
	 */
50
	protected $arg_types;
51
52
	/**
53
	 * The settings fields used by this reactor.
54
	 *
55
	 * @since 1.0.0
56
	 *
57
	 * @var array
58
	 */
59
	protected $settings_fields;
60
61
	/**
62
	 * The storage object for the standard reactions.
63
	 *
64
	 * @since 1.0.0
65
	 *
66
	 * @var WordPoints_Hook_Reaction_StorageI
67
	 */
68
	protected $standard_reactions;
69
70
	/**
71
	 * The storage object for the network-wide reactions.
72
	 *
73
	 * @since 1.0.0
74
	 *
75
	 * @var WordPoints_Hook_Reaction_StorageI
76
	 */
77
	protected $network_reactions;
78
79
	/**
80
	 * The reaction storage class this reactor uses.
81
	 *
82
	 * @since 1.0.0
83
	 *
84
	 * @var string
85
	 */
86
	protected $standard_reactions_class = 'WordPoints_Hook_Reaction_Storage_Options';
87
88
	/**
89
	 * The network reaction storage class this reactor uses.
90
	 *
91
	 * @since 1.0.0
92
	 *
93
	 * @var string
94
	 */
95
	protected $network_reactions_class = 'WordPoints_Hook_Reaction_Storage_Options_Network';
96
97
	/**
98
	 * @since 1.0.0
99
	 */
100
	public function __get( $var ) {
101
102
		$network_mode = wordpoints_hooks()->get_network_mode();
103
104
		switch ( $var ) {
105
			case 'reactions':
106
				$var = $network_mode ? 'network_reactions' : 'standard_reactions';
107
				// fall through
108
109
			case 'standard_reactions':
110
			case 'network_reactions':
111
				if ( $network_mode && 'standard_reactions' === $var ) {
112
					return null;
113
				}
114
115
				if ( 'network_reactions' === $var && ! $this->is_network_wide() ) {
116
					return null;
117
				}
118
119
				if ( ! isset( $this->$var ) ) {
120
					if ( isset( $this->{"{$var}_class"} ) ) {
121
						$this->$var = new $this->{"{$var}_class"}(
122
							$this->slug
123
							, ( 'network_reactions' === $var )
124
						);
125
					}
126
				}
127
128
				return $this->$var;
129
		}
130
131
		return null;
132
	}
133
134
	/**
135
	 * Get the slug of this reactor.
136
	 *
137
	 * @since 1.0.0
138
	 *
139
	 * @return string The reactor's slug.
140
	 */
141
	public function get_slug() {
142
		return $this->slug;
143
	}
144
145
	/**
146
	 * Get a list of the slugs of each type of arg that this reactor supports.
147
	 *
148
	 * @since 1.0.0
149
	 *
150
	 * @return string[] The slugs of the arg types this reactor supports.
151
	 */
152
	public function get_arg_types() {
153
		return (array) $this->arg_types;
154
	}
155
156
	/**
157
	 * Get the settings fields used by the reactor.
158
	 *
159
	 * @since 1.0.0
160
	 *
161
	 * @return string[] The meta keys used to store this reactor's settings.
162
	 */
163
	public function get_settings_fields() {
164
		return $this->settings_fields;
165
	}
166
167
	/**
168
	 * Check whether this reactor is network-wide.
169
	 *
170
	 * When a reactor is not network-wide, network reactions are not supported. For
171
	 * example, the points reactor is not network-wide when WordPoints isn't network-
172
	 * active, because the points types are created per-site. We default all reactors
173
	 * to being network wide only when WordPoints is network-active, but some may
174
	 * need to override this.
175
	 *
176
	 * @since 1.0.0
177
	 *
178
	 * @return bool Whether this reactor is network-wide.
179
	 */
180
	public function is_network_wide() {
181
		return is_wordpoints_network_active();
182
	}
183
184
	/**
185
	 * Get all reactions to a particular event for this reactor.
186
	 *
187
	 * On multisite it will return all reactions for the current site, both standard
188
	 * ones and any network-wide ones (if this reactor offers a network storage
189
	 * class). Or, if network mode is on, it will return only the network-wide ones.
190
	 *
191
	 * @since 1.0.0
192
	 *
193
	 * @param string $event_slug The event slug.
194
	 *
195
	 * @return WordPoints_Hook_ReactionI[] All of the reaction objects.
196
	 */
197 View Code Duplication
	public function get_all_reactions_to_event( $event_slug ) {
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...
198
199
		$reactions = array();
200
201
		foreach ( array( 'standard', 'network' ) as $store ) {
202
203
			$storage = $this->{"{$store}_reactions"};
204
205
			if ( ! $storage instanceof WordPoints_Hook_Reaction_StorageI ) {
206
				continue;
207
			}
208
209
			$reactions = array_merge(
210
				$reactions
211
				, $storage->get_reactions_to_event( $event_slug )
212
			);
213
		}
214
215
		return $reactions;
216
	}
217
218
	/**
219
	 * Get all reactions for this reactor.
220
	 *
221
	 * On multisite it will return all reactions for the current site, both standard
222
	 * ones and any network-wide ones (if this reactor offers a network storage
223
	 * class). Or, if network mode is on, it will return only the network-wide ones.
224
	 *
225
	 * @since 1.0.0
226
	 *
227
	 * @return WordPoints_Hook_ReactionI[] All of the reaction objects.
228
	 */
229 View Code Duplication
	public function get_all_reactions() {
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...
230
231
		$reactions = array();
232
233
		foreach ( array( 'standard', 'network' ) as $store ) {
234
235
			$storage = $this->{"{$store}_reactions"};
236
237
			if ( ! $storage instanceof WordPoints_Hook_Reaction_StorageI ) {
238
				continue;
239
			}
240
241
			$reactions = array_merge( $reactions, $storage->get_reactions() );
242
		}
243
244
		return $reactions;
245
	}
246
247
	/**
248
	 * @since 1.0.0
249
	 */
250
	public function validate_settings(
251
		array $settings,
252
		WordPoints_Hook_Reaction_Validator $validator,
253
		WordPoints_Hook_Event_Args $event_args
254
	) {
255
256
		if (
257
			empty( $settings['target'] )
258
			|| ! is_array( $settings['target'] )
259
		) {
260
261
			$validator->add_error( __( 'Invalid target.', 'wordpoints' ), 'target' );
262
263
		} else {
264
265
			$target = $event_args->get_from_hierarchy( $settings['target'] );
266
267
			if (
268
				! $target instanceof WordPoints_Entity
269
				|| ! in_array( $target->get_slug(), (array) $this->arg_types )
270
			) {
271
				$validator->add_error( __( 'Invalid target.', 'wordpoints' ), 'target' );
272
			}
273
		}
274
275
		return $settings;
276
	}
277
278
	/**
279
	 * @since 1.0.0
280
	 */
281
	public function update_settings( WordPoints_Hook_ReactionI $reaction, array $settings ) {
282
		$reaction->update_meta( 'target', $settings['target'] );
283
	}
284
285
	/**
286
	 * Perform an action when the reactor is hit by an event being fired.
287
	 *
288
	 * @since 1.0.0
289
	 *
290
	 * @param WordPoints_Hook_Event_Args         $event_args The event args.
291
	 * @param WordPoints_Hook_Reaction_Validator $reaction   The reaction.
292
	 */
293
	abstract public function hit( WordPoints_Hook_Event_Args $event_args, WordPoints_Hook_Reaction_Validator $reaction );
294
}
295
296
// EOF
297