|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Custom exceptions for handling PLugin State Changes. |
|
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 Exception; |
|
16
|
|
|
use Throwable; |
|
17
|
|
|
|
|
18
|
|
|
class Plugin_State_Exception extends Exception { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Returns an exception if an event can not constructed with DI. |
|
22
|
|
|
* @code 101 |
|
23
|
|
|
* @param Plugin_State_Change|string $event |
|
24
|
|
|
* @param Throwable|null $exception |
|
25
|
|
|
* @return Plugin_State_Exception |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function failed_to_create_state_change_event( $event, ?Throwable $exception = null ): Plugin_State_Exception { |
|
28
|
|
|
$message = \sprintf( |
|
29
|
|
|
'Failed to construct %s using the DI Container. %s', |
|
30
|
|
|
is_string( $event ) ? $event : get_class( $event ), |
|
31
|
|
|
$exception ? $exception->getMessage() : '' |
|
32
|
|
|
); |
|
33
|
|
|
return new Plugin_State_Exception( $message, 101 ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns an exception for adding a none event change class |
|
38
|
|
|
* @code 102 |
|
39
|
|
|
* @param string|object $event |
|
40
|
|
|
* @return Plugin_State_Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function invalid_state_change_event_type( $event ): Plugin_State_Exception { |
|
43
|
|
|
$message = \sprintf( |
|
44
|
|
|
'%s is not a valid Plugin State Change Event class', |
|
45
|
|
|
is_string( $event ) ? $event : get_class( $event ) |
|
46
|
|
|
); |
|
47
|
|
|
return new Plugin_State_Exception( $message, 102 ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Failed to locate path of file which created an instace of the Plugin State Controller |
|
52
|
|
|
* @code 103 |
|
53
|
|
|
* @return Plugin_State_Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function failed_to_locate_calling_file(): Plugin_State_Exception { |
|
56
|
|
|
return new Plugin_State_Exception( 'Could not locate the file which created the Plugin State Controller. Please define this value manually using $controller->finalise( string $file )', 103 ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Throws error while calling run() in state change event. |
|
61
|
|
|
* @code 104 |
|
62
|
|
|
* @param Plugin_State_Change $event |
|
63
|
|
|
* @param Throwable $exception |
|
64
|
|
|
* @return Plugin_State_Exception |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function error_running_state_change_event( Plugin_State_Change $event, ?Throwable $exception = null ): Plugin_State_Exception { |
|
67
|
|
|
$message = \sprintf( |
|
68
|
|
|
'Failed to run %s->run(), error thrown::%s', |
|
69
|
|
|
get_class( $event ), |
|
70
|
|
|
$exception === null ? 'NO ERROR PASSED' : $exception->getMessage() |
|
71
|
|
|
); |
|
72
|
|
|
return new Plugin_State_Exception( $message, 104 ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Exception for missing plugin base file name. |
|
77
|
|
|
* @code 105 |
|
78
|
|
|
* @param ?string $error |
|
79
|
|
|
* @return Plugin_State_Exception |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function invalid_plugin_base_file( $error = null ): Plugin_State_Exception { |
|
82
|
|
|
return new Plugin_State_Exception( is_string( $error ) ? $error : 'No plugin base file name passed.', 105 ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Exception for missing controller |
|
87
|
|
|
* @code 106 |
|
88
|
|
|
* @return Plugin_State_Exception |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function missing_controller(): Plugin_State_Exception { |
|
91
|
|
|
return new Plugin_State_Exception( 'No Plugin State Controller passed.', 106 ); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|