Completed
Push — master ( 33372a...46be78 )
by J.D.
03:04
created

WordPoints_Hook_Firer_Reverse   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 78
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B do_event() 0 36 6
B get_hits() 0 25 2
1
<?php
2
3
/**
4
 * Reverse hook firer class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since   1.0.0
8
 */
9
10
/**
11
 * Fires a reverse action for a hook event.
12
 *
13
 * @since 1.0.0
14
 */
15
class WordPoints_Hook_Firer_Reverse extends WordPoints_Hook_Firer {
16
17
	/**
18
	 * @since 1.0.0
19
	 */
20
	public function do_event( $event_slug, WordPoints_Hook_Event_Args $event_args ) {
21
22
		$hooks = wordpoints_hooks();
23
24
		foreach ( $this->get_hits( $event_slug, $event_args ) as $hit ) {
25
26
			$reactor = $hooks->reactors->get( $hit->reactor );
27
28
			if ( ! $reactor instanceof WordPoints_Hook_Reactor ) {
29
				continue;
30
			}
31
32
			$reactions = $reactor->get_reaction_group( $hit->reaction_type );
33
34
			if ( ! $reactions ) {
35
				continue;
36
			}
37
38
			$reaction = $reactions->get_reaction( $hit->reaction_id );
39
40
			if ( ! $reaction ) {
41
				continue;
42
			}
43
44
			$fire = new WordPoints_Hook_Fire( $this, $event_args, $reaction, $hit );
45
46
			$fire->hit();
47
48
			$reactor->reverse_hit( $fire );
49
50
			/** @var WordPoints_Hook_Extension $extension */
51
			foreach ( $hooks->extensions->get_all() as $extension ) {
52
				$extension->after_reverse( $fire );
53
			}
54
		}
55
	}
56
57
	/**
58
	 * Retrieves a list of all hits matching this event that have not been reversed.
59
	 *
60
	 * @since 1.0.0
61
	 *
62
	 * @param string                     $event_slug The slug of the event.
63
	 * @param WordPoints_Hook_Event_Args $event_args The args for the event.
64
	 *
65
	 * @return object[] The data for each hit from the hit logs database table.
66
	 */
67
	protected function get_hits( $event_slug, WordPoints_Hook_Event_Args $event_args ) {
68
69
		global $wpdb;
70
71
		$hits = $wpdb->get_results(
72
			$wpdb->prepare(
73
				"
74
					SELECT *
75
					FROM `{$wpdb->wordpoints_hook_hits}`
76
					WHERE `fire_type` = 'fire'
77
					AND `signature` = %s
78
					AND `event` = %s
79
					AND `superseded_by` IS NULL
80
				"
81
				, wordpoints_hooks_get_event_signature( $event_args )
82
				, $event_slug
83
			)
84
		);
85
86
		if ( ! is_array( $hits ) ) {
87
			return array();
88
		}
89
90
		return $hits;
91
	}
92
}
93
94
// EOF
95