State_Change_Queue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Queue and runner for a collection events
7
 *
8
 * @package PinkCrab\Plugin_Lifecycle
9
 * @author Glynn Quelch [email protected]
10
 * @since 0.0.1
11
 */
12
13
namespace PinkCrab\Plugin_Lifecycle;
14
15
use PinkCrab\Plugin_Lifecycle\Plugin_State_Change;
16
use PinkCrab\Plugin_Lifecycle\State_Event\Activation;
17
18
class State_Change_Queue {
19
20
	/**
21
	 * Events
22
	 *
23
	 * @var Plugin_State_Change[]
24
	 */
25
	protected $events;
26
27
	/** @param Plugin_State_Change ...$event */
28
	public function __construct( Plugin_State_Change ...$event ) {
29
		$this->events = $event;
30
	}
31
32
	/**
33
	 * Runs all of the queued events.
34
	 *
35
	 * @return void
36
	 */
37
	public function __invoke() {
38
		foreach ( $this->events as $event ) {
39
			try {
40
				$event->run();
41
			} catch ( \Throwable $th ) {
42
				// If caught on Activation, throw Plugin_State_Exception
43
				if ( is_a( $event, Activation::class ) ) {
44
					throw Plugin_State_Exception::error_running_state_change_event( $event, $th );
45
				}
46
47
				continue;
48
			}
49
		}
50
	}
51
}
52