Completed
Push — master ( ed9362...6730cd )
by J.D.
03:52
created

WordPoints_Hook_Event_Args   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 19
lcom 1
cbo 3
dl 0
loc 162
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 18 5
A is_event_repeatable() 0 3 1
A set_validator() 0 3 1
C descend() 0 45 8
A ascend() 0 10 3
A get_from_hierarchy() 0 8 1
1
<?php
2
3
/**
4
 * Hook event args class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since   1.0.0
8
 */
9
10
/**
11
 * Represents the args associated with a hook event.
12
 *
13
 * This is similar to the regular entity hierarchy, except that it handles errors
14
 * with the reaction validators, and accepts an array of hook args instead of
15
 * requiring entity objects to be passed directly.
16
 *
17
 * @since 1.0.0
18
 */
19
class WordPoints_Hook_Event_Args extends WordPoints_Entity_Hierarchy {
20
21
	/**
22
	 * Whether the event is repeatable.
23
	 *
24
	 * @since 1.0.0
25
	 *
26
	 * @var bool
27
	 */
28
	protected $is_repeatable = true;
29
30
	/**
31
	 * The validator associated with the current hook reaction.
32
	 *
33
	 * @since 1.0.0
34
	 *
35
	 * @var WordPoints_Hook_Reaction_Validator
36
	 */
37
	protected $validator;
38
39
	/**
40
	 * Whether to push fields onto the validator when we descend into the hierarchy.
41
	 *
42
	 * This will usually be true, however, when we are getting a value from the
43
	 * hierarchy, we aren't actually descending into a sub-field, but descending
44
	 * down the arg hierarchy stored within a field.
45
	 *
46
	 * @since 1.0.0
47
	 *
48
	 * @var bool
49
	 */
50
	protected $push_on_descend = true;
51
52
	/**
53
	 * Construct the object with the arg objects.
54
	 *
55
	 * @param WordPoints_Hook_Arg[] $args The hook args.
56
	 */
57
	public function __construct( array $args ) {
58
59
		parent::__construct();
60
61
		foreach ( $args as $arg ) {
62
63
			$entity = $arg->get_entity();
64
65
			if ( $entity instanceof WordPoints_Entity ) {
66
				$this->entities[ $arg->get_slug() ] = $entity;
67
			}
68
69
			// If any of the args aren't stateful the event isn't repeatable.
70
			if ( $this->is_repeatable && ! $arg->is_stateful() ) {
71
				$this->is_repeatable = false;
72
			}
73
		}
74
	}
75
76
	/**
77
	 * Whether the event is repeatable.
78
	 *
79
	 * An event is repeatable if none of its args have their status toggled by the
80
	 * event. In that case, it is possible for the event to occur with those same args
81
	 * multiple times in a row without being reversed in between.
82
	 *
83
	 * An arg that has its status modified is called a signature arg. One that does
84
	 * not is called a stateful arg. An event is repeatable if it has no signature
85
	 * args, only stateful ones.
86
	 *
87
	 * @since 1.0.0
88
	 *
89
	 * @return bool Whether the event is repeatable.
90
	 */
91
	public function is_event_repeatable() {
92
		return $this->is_repeatable;
93
	}
94
95
	/**
96
	 * Set the validator for the current reaction.
97
	 *
98
	 * @since 1.0.0
99
	 *
100
	 * @param WordPoints_Hook_Reaction_Validator $validator The validator.
101
	 */
102
	public function set_validator( WordPoints_Hook_Reaction_Validator $validator ) {
103
		$this->validator = $validator;
104
	}
105
106
	/**
107
	 * @since 1.0.0
108
	 */
109
	public function descend( $child_slug ) {
110
111
		$result = parent::descend( $child_slug );
112
113
		// Just in case no validator has been set.
114
		if ( ! $this->validator ) {
115
			return $result;
116
		}
117
118
		if ( ! $result ) {
119
120
			if ( ! isset( $this->current ) ) {
121
122
				$this->validator->add_error(
123
					sprintf(
124
						__( 'The %s arg is not registered for this event.', 'wordpoints' ) // TODO message
125
						, $child_slug
126
					)
127
				);
128
129
			} elseif ( ! ( $this->current instanceof WordPoints_Entity_ParentI ) ) {
130
131
				$this->validator->add_error(
132
					__( 'Cannot get descendant of %s: not a parent.', 'wordpoints' ) // TODO message
133
				);
134
135
			} else {
136
137
				$child_arg = $this->current->get_child( $child_slug );
138
139
				if ( ! $child_arg ) {
140
					$this->validator->add_error(
141
						__( '%s does not have a child "%s".', 'wordpoints' ) // TODO message
142
						, $this->push_on_descend ? $child_slug : null
143
					);
144
				}
145
			}
146
147
		} elseif ( $this->push_on_descend ) {
148
149
			$this->validator->push_field( $child_slug );
150
		}
151
152
		return $result;
153
	}
154
155
	/**
156
	 * @since 1.0.0
157
	 */
158
	public function ascend() {
159
160
		$ascended = parent::ascend();
161
162
		if ( $ascended && $this->validator ) {
163
			$this->validator->pop_field();
164
		}
165
166
		return $ascended;
167
	}
168
169
	/**
170
	 * @since 1.0.0
171
	 */
172
	public function get_from_hierarchy( array $hierarchy ) {
173
174
		$this->push_on_descend = false;
175
		$entityish = parent::get_from_hierarchy( $hierarchy );
176
		$this->push_on_descend = true;
177
178
		return $entityish;
179
	}
180
}
181
182
// EOF
183