Completed
Push — master ( a88d8a...dfa187 )
by Glynn
02:01 queued 01:58
created

Plugin_Life_Cycle   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 31
c 1
b 0
f 0
dl 0
loc 114
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get_events() 0 6 1
A finalise() 0 20 3
A pre_register() 0 1 1
A pre_boot() 0 11 2
A plugin_base_file() 0 3 1
A event() 0 7 2
A post_register() 0 1 1
A get_middleware() 0 2 1
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 PinkCrab\Loader\Hook_Loader;
20
use PinkCrab\Perique\Application\Hooks;
21
22
use PinkCrab\Perique\Application\App;
23
use PinkCrab\Perique\Interfaces\Module;
24
use PinkCrab\Perique\Application\App_Config;
25
use PinkCrab\Perique\Interfaces\DI_Container;
26
use PinkCrab\Plugin_Lifecycle\Plugin_State_Exception;
27
use PinkCrab\Plugin_Lifecycle\Plugin_State_Controller;
28
29
class Plugin_Life_Cycle implements Module {
30
31
	public const STATE_EVENTS  = 'PinkCrab\Plugin_Lifecycle\State_Events';
32
	public const PRE_FINALISE  = 'PinkCrab\Plugin_Lifecycle\Pre_Finalise';
33
	public const POST_FINALISE = 'PinkCrab\Plugin_Lifecycle\Post_Finalise';
34
	public const EVENT_LIST    = 'PinkCrab\Plugin_Lifecycle\Event_List';
35
36
	/** @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...
37
	private array $events                              = array();
38
	private ?string $plugin_base_file                  = null;
39
	private ?Plugin_State_Controller $state_controller = null;
40
41
	/**
42
	 * Adds an event to the event queue.
43
	 *
44
	 * @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...
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
		return $this;
54
	}
55
56
	/**
57
	 * Sets the plugin base file.
58
	 *
59
	 * @param string $plugin_base_file
60
	 * @return self
61
	 */
62
	public function plugin_base_file( string $plugin_base_file ): self {
63
		$this->plugin_base_file = $plugin_base_file;
64
		return $this;
65
	}
66
67
	/**
68
	 * Used to create the controller instance and register the hook call, to trigger.
69
	 *
70
	 * @pram App_Config $config
71
	 * @pram Hook_Loader $loader
72
	 * @pram DI_Container $di_container
73
	 * @return void
74
	 */
75
	public function pre_boot( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
76
77
		if ( null === $this->plugin_base_file ) {
78
			throw Plugin_State_Exception::invalid_plugin_base_file( $this->plugin_base_file );
79
		}
80
81
		// Create the instance of the state controller.
82
		$this->state_controller = new Plugin_State_Controller( $di_container, $this->plugin_base_file );
83
84
		// Finalise the state controller.
85
		add_action( Hooks::APP_INIT_PRE_BOOT, array( $this, 'finalise' ), 999, 3 );
86
	}
87
88
	/**
89
	 * The callback for finalising the controller process.
90
	 *
91
	 * @pram App_Config $config
92
	 * @pram Hook_Loader $loader
93
	 * @pram DI_Container $di_container
94
	 * @return void
95
	 */
96
	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

96
	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 $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

96
	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...
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

96
	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...
97
98
		// Throw exception if controller not set.
99
		if ( null === $this->state_controller ) {
100
			throw Plugin_State_Exception::missing_controller();
101
		}
102
103
		// Trigger the pre action.
104
		do_action( self::PRE_FINALISE, $this );
105
106
		// Add events to the controller.
107
		foreach ( $this->get_events() as $event ) {
108
			$this->state_controller->event( $event );
109
		}
110
111
		// Register the state controller.
112
		$this->state_controller->finalise();
113
114
		// Trigger the post action.
115
		do_action( self::POST_FINALISE, $this );
116
	}
117
118
119
	/**
120
	 * Get all the events, allowing other modules to extend and add their own.
121
	 *
122
	 * @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...
123
	 */
124
	public function get_events(): array {
125
		$events = apply_filters( self::STATE_EVENTS, $this->events );
126
		$events = array_unique( $events );
127
		$events = array_filter( $events, 'is_string' );
128
		$events = array_filter( $events, fn( $event ) => is_subclass_of( $event, Plugin_State_Change::class ) );
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
}
146