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

WordPoints_Hook_Hit_Logger::log_hit()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 39
rs 8.8571
nc 3
cc 3
eloc 25
nop 0
1
<?php
2
3
/**
4
 * Hook hit logger class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since   1.0.0
8
 */
9
10
/**
11
 * Logs hook hits.
12
 *
13
 * @since 1.0.0
14
 */
15
class WordPoints_Hook_Hit_Logger {
16
17
	/**
18
	 * The fire for which a hit might occur.
19
	 *
20
	 * @since 1.0.0
21
	 *
22
	 * @var WordPoints_Hook_Fire
23
	 */
24
	protected $fire;
25
26
	/**
27
	 * @param WordPoints_Hook_Fire $fire The fire that might be logged as a hit.
28
	 */
29
	public function __construct( WordPoints_Hook_Fire $fire ) {
30
31
		$this->fire = $fire;
32
	}
33
34
	/**
35
	 * Logs a hit for this fire.
36
	 *
37
	 * @since 1.0.0
38
	 *
39
	 * @return int|false The hit ID, or false if logging the hit failed.
40
	 */
41
	public function log_hit() {
42
43
		global $wpdb;
44
45
		$signature = wordpoints_hooks_get_event_signature( $this->fire->event_args );
46
47
		$inserted = $wpdb->insert(
48
			$wpdb->wordpoints_hook_hits
49
			, array(
50
				'fire_type' => $this->fire->firer->get_slug(),
51
				'signature' => $signature,
52
				'event' => $this->fire->reaction->get_event_slug(),
53
				'reactor' => $this->fire->reaction->get_reactor_slug(),
54
				'reaction_type' => $this->fire->reaction->get_storage_group_slug(),
55
				'reaction_id' => $this->fire->reaction->ID,
0 ignored issues
show
Bug introduced by
Accessing ID on the interface WordPoints_Hook_ReactionI suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56
				'date' => current_time( 'mysql' ),
57
			)
58
		);
59
60
		if ( ! $inserted ) {
61
			return false;
62
		}
63
64
		$hit_id = $wpdb->insert_id;
65
66
		$supersedes = $this->fire->get_superseded_hit();
67
68
		if ( $supersedes ) {
69
			$wpdb->update(
70
				$wpdb->wordpoints_hook_hits
71
				, array( 'superseded_by' => $hit_id )
72
				, array( 'id' => $supersedes->id )
73
				, array( '%d' )
74
				, array( '%d' )
75
			);
76
		}
77
78
		return $hit_id;
79
	}
80
}
81
82
// EOF
83