Completed
Push — master ( b43b5b...dcfda7 )
by J.D.
02:58
created

WordPoints_Hook_Firer_Reverse::get_hits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 9.2
cc 2
eloc 13
nc 2
nop 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
		$hits = $this->get_hits( $event_slug, $event_args );
25
		$reverse_hit_ids = array();
26
27
		foreach ( $hits as $hit ) {
28
29
			/** @var WordPoints_Hook_Reactor $reactor */
30
			$reactor = $hooks->reactors->get( $hit->reactor );
31
32
			if ( ! $reactor instanceof WordPoints_Hook_Reactor_ReverseI ) {
33
				continue;
34
			}
35
36
			$reactions = $reactor->get_reaction_store( $hit->reaction_store );
37
38
			if (
39
				! $reactions
40
				|| wp_json_encode( $reactions->get_context_id() ) !== $hit->reaction_context_id
41
			) {
42
				continue;
43
			}
44
45
			$reaction = $reactions->get_reaction( $hit->reaction_id );
46
47
			if ( ! $reaction ) {
48
				continue;
49
			}
50
51
			$fire = new WordPoints_Hook_Fire( $this, $event_args, $reaction, $hit );
0 ignored issues
show
Unused Code introduced by
The call to WordPoints_Hook_Fire::__construct() has too many arguments starting with $hit.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
52
53
			$reverse_hit_ids[ $hit->id ] = $fire->hit();
54
55
			$reactor->reverse_hit( $fire );
56
57
			/** @var WordPoints_Hook_Extension $extension */
58
			foreach ( $hooks->extensions->get_all() as $extension ) {
59
				$extension->after_reverse( $fire );
60
			}
61
		}
62
63
		// Set the reversed_by meta key for all hits so that we know that they have
64
		// been reverse fired, even if they didn't hit.
65
		foreach ( $hits as $hit ) {
66
67
			if ( isset( $reverse_hit_ids[ $hit->id ] ) ) {
68
				$reversed_by = $reverse_hit_ids[ $hit->id ];
69
			} else {
70
				$reversed_by = 0;
71
			}
72
73
			add_metadata(
74
				'wordpoints_hook_hit'
75
				, $hit->id
76
				, 'reversed_by'
77
				, $reversed_by
78
				, true
79
			);
80
		}
81
	}
82
83
	/**
84
	 * Retrieves a list of all hits matching this event that have not been reversed.
85
	 *
86
	 * @since 1.0.0
87
	 *
88
	 * @param string                     $event_slug The slug of the event.
89
	 * @param WordPoints_Hook_Event_Args $event_args The args for the event.
90
	 *
91
	 * @return object[] The data for each hit from the hit logs database table.
92
	 */
93
	protected function get_hits( $event_slug, WordPoints_Hook_Event_Args $event_args ) {
94
95
		$query = new WordPoints_Hook_Hit_Query(
96
			array(
97
				'firer' => 'fire',
98
				'primary_arg_guid' => wordpoints_hooks_get_event_primary_arg_guid_json(
99
					$event_args
100
				),
101
				'event' => $event_slug,
102
				'meta_key' => 'reversed_by',
103
				'meta_compare' => 'NOT EXISTS',
104
			)
105
		);
106
107
		$hits = $query->get();
108
109
		if ( ! is_array( $hits ) ) {
110
			return array();
111
		}
112
113
		return $hits;
114
	}
115
}
116
117
// EOF
118