Plugin_Life_Cycle::get_events()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare( strict_types=1 );
4
5
/**
6
 * Interface for all classes which act on a plugin state change.
7
 * Update, Activation, Deactivation and Uninstalling.
8
 *
9
 * This interface should not be used directory, please see
10
 * those which extend. This is to offer a faux union type.
11
 *
12
 * @package PinkCrab\Plugin_Lifecycle
13
 * @author Glynn Quelch [email protected]
14
 * @since 1.0.0
15
 */
16
17
namespace PinkCrab\Plugin_Lifecycle;
18
19
use Exception;
20
use ReflectionClass;
21
use PinkCrab\Loader\Hook_Loader;
22
use PinkCrab\Perique\Application\Hooks;
23
use PinkCrab\Perique\Interfaces\Module;
24
use PinkCrab\Perique\Application\App_Config;
25
use PinkCrab\Perique\Interfaces\DI_Container;
26
use ReflectionMethod;
27
28
class Plugin_Life_Cycle implements Module {
29
30
	public const STATE_EVENTS  = 'PinkCrab\Plugin_Lifecycle\State_Events';
31
	public const PRE_FINALISE  = 'PinkCrab\Plugin_Lifecycle\Pre_Finalise';
32
	public const POST_FINALISE = 'PinkCrab\Plugin_Lifecycle\Post_Finalise';
33
	public const EVENT_LIST    = 'PinkCrab\Plugin_Lifecycle\Event_List';
34
35
	/** @var class-string<Plugin_State_Change>[] */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Plugin_State_Change>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Plugin_State_Change>[].
Loading history...
36
	private array $events                              = array();
37
	private ?string $plugin_base_file                  = null;
38
	private ?Plugin_State_Controller $state_controller = null;
39
40
	/**
41
	 * Adds an event to the event queue.
42
	 *
43
	 * @param class-string<Plugin_State_Change> $event
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Plugin_State_Change> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Plugin_State_Change>.
Loading history...
44
	 *
45
	 * @return self
46
	 */
47
	public function event( string $event ): self {
48
		// Ensure the event is a valid class.
49
		if ( ! class_exists( $event ) ) {
50
			throw Plugin_State_Exception::invalid_state_change_event_type( $event );
51
		}
52
		$this->events[] = $event;
53
54
		return $this;
55
	}
56
57
	/**
58
	 * Sets the plugin base file.
59
	 *
60
	 * @param string $plugin_base_file
61
	 *
62
	 * @return self
63
	 */
64
	public function plugin_base_file( string $plugin_base_file ): self {
65
		$this->plugin_base_file = $plugin_base_file;
66
67
		return $this;
68
	}
69
70
	/**
71
	 * Used to create the controller instance and register the hook call, to trigger.
72
	 *
73
	 * @pram App_Config $config
74
	 * @pram Hook_Loader $loader
75
	 * @pram DI_Container $di_container
76
	 * @return void
77
	 */
78
	public function pre_boot( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
79
80
		// Create the instance of the state controller.
81
		$this->state_controller = new Plugin_State_Controller( $di_container, $this->plugin_base_file );
82
83
		// Finalise the state controller.
84
		add_action( Hooks::APP_INIT_PRE_BOOT, array( $this, 'finalise' ), 999, 3 );
85
	}
86
87
	/**
88
	 * The callback for finalising the controller process.
89
	 *
90
	 * @pram App_Config $config
91
	 * @pram Hook_Loader $loader
92
	 * @pram DI_Container $di_container
93
	 * @return void
94
	 */
95
	public function finalise( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed
0 ignored issues
show
Unused Code introduced by
The parameter $loader is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
	public function finalise( App_Config $config, /** @scrutinizer ignore-unused */ Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
	public function finalise( /** @scrutinizer ignore-unused */ App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $di_container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
	public function finalise( App_Config $config, Hook_Loader $loader, /** @scrutinizer ignore-unused */ DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
97
		// Throw exception if controller not set.
98
		if ( null === $this->state_controller ) {
99
			throw Plugin_State_Exception::missing_controller();
100
		}
101
102
		// Trigger the pre action.
103
		do_action( self::PRE_FINALISE, $this );
104
105
		// Add events to the controller.
106
		foreach ( $this->get_events() as $event ) {
107
			$this->state_controller->event( $event );
108
		}
109
110
		// Register the state controller.
111
		$this->state_controller->finalise();
112
113
		// Trigger the post action.
114
		do_action( self::POST_FINALISE, $this );
115
	}
116
117
118
	/**
119
	 * Get all the events, allowing other modules to extend and add their own.
120
	 *
121
	 * @return class-string<Plugin_State_Change>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Plugin_State_Change>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Plugin_State_Change>[].
Loading history...
122
	 */
123
	public function get_events(): array {
124
		$events = apply_filters( self::STATE_EVENTS, $this->events );
125
		$events = array_unique( $events );
126
		$events = array_filter( $events, 'is_string' );
127
		$events = array_filter( $events, fn( $event ) => is_subclass_of( $event, Plugin_State_Change::class ) );
128
129
		return $events;
130
	}
131
132
	## Unused methods
133
134
	/** @inheritDoc */
135
	public function pre_register( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
136
137
	/** @inheritDoc */
138
	public function post_register( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
139
140
	/** @inheritDoc */
141
	public function get_middleware(): ?string {
142
		return null;
143
	}
144
}
145