Completed
Push — master ( e73d65...d818c3 )
by J.D.
03:52
created

WordPoints_Hook_Extension_Reversals   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 84
rs 10
wmc 9
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A should_hit() 0 10 2
A after_hit() 0 10 3
A after_miss() 0 3 1
B get_hits_to_be_reversed() 0 29 3
1
<?php
2
3
/**
4
 * Reversals hook extension class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Makes actions of one type behave as reversals of actions of another type.
12
 *
13
 * Use this if your reactor has multiple responses, and the one tied to one action
14
 * type should only fire if the last fire of the action type tied to another response
15
 * produced a hit. In other words, you only want the last fire reversed if it was a
16
 * hit; you only want the last hit reversed if it was from the last fire. This will
17
 * prevent you from reversing a hit if reversal of it has already reverse-fired and
18
 * the fire was blocked for some reason.
19
 *
20
 * @since 1.0.0
21
 */
22
class WordPoints_Hook_Extension_Reversals
23
	extends WordPoints_Hook_Extension
24
	implements WordPoints_Hook_Extension_Hit_ListenerI,
25
		WordPoints_Hook_Extension_Miss_ListenerI {
26
27
	/**
28
	 * @since 1.0.0
29
	 */
30
	protected $slug = 'reversals';
31
32
	/**
33
	 * @since 1.0.0
34
	 */
35
	public function should_hit( WordPoints_Hook_Fire $fire ) {
36
37
		if ( ! $this->get_settings_from_fire( $fire ) ) {
38
			return true;
39
		}
40
41
		$ids = $this->get_hits_to_be_reversed( $fire );
42
43
		return count( $ids ) > 0;
44
	}
45
46
	/**
47
	 * @since 1.0.0
48
	 */
49
	public function after_hit( WordPoints_Hook_Fire $fire ) {
50
51
		if ( ! $this->get_settings_from_fire( $fire ) ) {
52
			return;
53
		}
54
55
		foreach ( $this->get_hits_to_be_reversed( $fire ) as $id ) {
56
			add_metadata( 'wordpoints_hook_hit', $id, 'reverse_fired', true );
57
		}
58
	}
59
60
	/**
61
	 * @since 1.0.0
62
	 */
63
	public function after_miss( WordPoints_Hook_Fire $fire ) {
64
		$this->after_hit( $fire );
65
	}
66
67
	/**
68
	 * Get the IDs of the hits that should be reversed by this fire.
69
	 *
70
	 * @since 1.0.0
71
	 *
72
	 * @param WordPoints_Hook_Fire $fire The fire object.
73
	 *
74
	 * @return array The ids the hits to be reversed.
75
	 */
76
	protected function get_hits_to_be_reversed( WordPoints_Hook_Fire $fire ) {
77
78
		// We cache these so that we don't run the query both before and after the
79
		// fire.
80
		if ( isset( $fire->data[ $this->slug ]['hit_ids'] ) ) {
81
			return $fire->data[ $this->slug ]['hit_ids'];
82
		}
83
84
		$query = $fire->get_matching_hits_query();
85
86
		$query->set_args(
87
			array(
88
				'fields'       => 'id',
89
				'action_type'  => $this->get_settings_from_fire( $fire ),
90
				'meta_key'     => 'reverse_fired',
91
				'meta_compare' => 'NOT EXISTS',
92
			)
93
		);
94
95
		$ids = $query->get( 'col' );
96
97
		if ( ! $ids ) {
98
			$ids = array();
99
		}
100
101
		$fire->data[ $this->slug ]['hit_ids'] = $ids;
102
103
		return $ids;
104
	}
105
}
106
107
// EOF
108