Completed
Push — master ( 798024...d5c80d )
by J.D.
02:47
created

WordPoints_Hook_Reaction_Store   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4
Metric Value
wmc 15
lcom 2
cbo 4
dl 0
loc 173
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get_slug() 0 3 1
A get_reactor_slug() 0 3 1
A get_context_id() 0 3 1
A get_reaction() 0 8 2
A create_reaction() 0 3 1
A update_reaction() 0 3 1
C create_or_update_reaction() 0 48 7
_create_reaction() 0 1 ?
1
<?php
2
3
/**
4
 * Base hook reaction storage class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Bootstrap for hook reaction storage methods.
12
 *
13
 * This class provides a common bootstrap for creating, updated, and deleting
14
 * reactions. It also provides a bootstrap for retrieving a single hook reaction.
15
 *
16
 * @since 1.0.0
17
 */
18
abstract class WordPoints_Hook_Reaction_Store implements WordPoints_Hook_Reaction_StoreI {
19
20
	/**
21
	 * The slug of this store.
22
	 *
23
	 * @since 1.0.0
24
	 *
25
	 * @var string
26
	 */
27
	protected $slug;
28
29
	/**
30
	 * The reactor that these reactions belong to.
31
	 *
32
	 * @since 1.0.0
33
	 *
34
	 * @var WordPoints_Hook_Reactor
35
	 */
36
	protected $reactor;
37
38
	/**
39
	 * The slug of the contexts in which the reactions are stored.
40
	 *
41
	 * @since 1.0.0
42
	 *
43
	 * @see wordpoints_entities_get_current_context_id()
44
	 *
45
	 * @var string
46
	 */
47
	protected $context = 'site';
48
49
	/**
50
	 * The name of the class to use for reaction objects.
51
	 *
52
	 * The class must implement the WordPoints_Hook_ReactionI interface.
53
	 *
54
	 * @since 1.0.0
55
	 *
56
	 * @var string
57
	 */
58
	protected $reaction_class;
59
60
	/**
61
	 * @since 1.0.0
62
	 */
63
	public function __construct( $slug, WordPoints_Hook_Reactor $reactor ) {
64
65
		$this->slug = $slug;
66
		$this->reactor = $reactor;
67
	}
68
69
	/**
70
	 * @since 1.0.0
71
	 */
72
	public function get_slug() {
73
		return $this->slug;
74
	}
75
76
	/**
77
	 * @since 1.0.0
78
	 */
79
	public function get_reactor_slug() {
80
		return $this->reactor->get_slug();
81
	}
82
83
	/**
84
	 * @since 1.0.0
85
	 */
86
	public function get_context_id() {
87
		return wordpoints_entities_get_current_context_id( $this->context );
88
	}
89
90
	/**
91
	 * @since 1.0.0
92
	 */
93
	public function get_reaction( $id ) {
94
95
		if ( ! $this->reaction_exists( $id ) ) {
96
			return false;
97
		}
98
99
		return new $this->reaction_class( $id, $this );
100
	}
101
102
	/**
103
	 * @since 1.0.0
104
	 */
105
	public function create_reaction( array $settings ) {
106
		return $this->create_or_update_reaction( $settings );
107
	}
108
109
	/**
110
	 * @since 1.0.0
111
	 */
112
	public function update_reaction( $id, array $settings ) {
113
		return $this->create_or_update_reaction( $settings, $id );
114
	}
115
116
	/**
117
	 * Create or update a reaction.
118
	 *
119
	 * @since 1.0.0
120
	 *
121
	 * @param array $settings The settings for the reaction.
122
	 * @param int   $id       The ID of the reaction to update, if updating.
123
	 *
124
	 * @return WordPoints_Hook_ReactionI|false|WordPoints_Hook_Reaction_Validator
125
	 *         The reaction object if created/updated successfully. False or a
126
	 *         validator instance if not.
127
	 */
128
	protected function create_or_update_reaction( array $settings, $id = null ) {
129
130
		$is_new = ! isset( $id );
131
132
		if ( ! $is_new && ! $this->reaction_exists( $id ) ) {
133
			return false;
134
		}
135
136
		$validator = new WordPoints_Hook_Reaction_Validator( $settings, $this->reactor );
137
		$settings = $validator->validate();
138
139
		if ( $validator->had_errors() ) {
140
			return $validator;
141
		}
142
143
		if ( $is_new ) {
144
145
			$id = $this->_create_reaction( $settings['event'] );
146
147
			if ( ! $id ) {
148
				return false;
149
			}
150
		}
151
152
		$reaction = $this->get_reaction( $id );
153
154
		$reaction->update_event_slug( $settings['event'] );
155
156
		unset( $settings['event'] );
157
158
		$this->reactor->update_settings( $reaction, $settings );
0 ignored issues
show
Documentation introduced by
$reaction is of type false|object, but the function expects a object<WordPoints_Hook_ReactionI>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
160
		/** @var WordPoints_Hook_Extension $extension */
161
		foreach ( wordpoints_hooks()->extensions->get_all() as $extension ) {
162
			$extension->update_settings( $reaction, $settings );
163
		}
164
165
		/**
166
		 * A hook reaction is being saved.
167
		 *
168
		 * @param WordPoints_Hook_ReactionI $reaction The reaction object.
169
		 * @param array                     $settings The new settings for the reaction.
170
		 * @param bool                      $is_new   Whether the reaction was just now created.
171
		 */
172
		do_action( 'wordpoints_hook_reaction_save', $reaction, $settings, $is_new );
173
174
		return $reaction;
175
	}
176
177
	/**
178
	 * Create a reaction.
179
	 *
180
	 * The event slug is provided in case it is needed (for some storage methods it
181
	 * is).
182
	 *
183
	 * @since 1.0.0
184
	 *
185
	 * @param string $event_slug The slug of the event this reaction is for.
186
	 *
187
	 * @return int|false The reaction ID, or false if not created.
188
	 */
189
	abstract protected function _create_reaction( $event_slug );
190
}
191
192
// EOF
193