Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 8 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
Listener | |
0.00% |
0 / 8 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
register_hooks | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
push | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
get_class_name | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Data\Listeners; |
4 | |
5 | use NewfoldLabs\WP\Module\Data\EventManager; |
6 | use NewfoldLabs\WP\Module\Data\Event; |
7 | |
8 | /** |
9 | * Base class for listeners |
10 | */ |
11 | abstract class Listener { |
12 | |
13 | /** |
14 | * Event Manager instance |
15 | * |
16 | * @var EventManager |
17 | */ |
18 | protected $manager; |
19 | |
20 | /** |
21 | * Default constructor |
22 | * |
23 | * @param EventManager $manager Instance of the Event Manager the listener is registered to |
24 | */ |
25 | public function __construct( EventManager $manager ) { |
26 | $this->manager = $manager; |
27 | } |
28 | |
29 | /** |
30 | * Register all required hooks for the listener category |
31 | */ |
32 | public function register_hooks() {} |
33 | |
34 | /** |
35 | * Wrapper for pushing event data up to the Event Manager |
36 | * |
37 | * @param string $key Key representing the action that occurred |
38 | * @param array $data Optional array of additional data for the action |
39 | * @return void |
40 | */ |
41 | protected function push( $key, $data = array() ) { |
42 | $category = $this->get_class_name(); |
43 | $event = new Event( $category, $key, $data ); |
44 | $this->manager->push( $event ); |
45 | } |
46 | |
47 | /** |
48 | * Return the name of the current class without the namespace |
49 | * |
50 | * @return string Name of the class or false |
51 | */ |
52 | protected function get_class_name() { |
53 | $class = get_class( $this ); |
54 | $position = strrpos( $class, '\\' ); |
55 | |
56 | return substr( $class, $position + 1 ); |
57 | } |
58 | } |