invalid_registration_middleware()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Module Manager Exceptions
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\Perique\Exceptions
23
 * @version 2.0.0
24
 */
25
26
namespace PinkCrab\Perique\Exceptions;
27
28
use Exception;
29
30
class Module_Manager_Exception extends Exception {
31
32
	/**
33
	 * Cast any value to a string for exception message.
34
	 *
35
	 * @param mixed $value
36
	 * @return string
37
	 */
38
	private static function cast_to_string( $value ): string {
39
		if ( is_object( $value ) ) {
40
			return get_class( $value );
41
		}
42
43
		if ( is_array( $value ) ) {
44
			return \wp_json_encode( $value ) ?: 'FAILED_TO_CAST ARRAY'; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found
45
		}
46
47
		if ( is_null( $value ) ) {
48
			return 'NULL';
49
		}
50
51
		if ( is_bool( $value ) ) {
52
			return $value ? 'BOOL::true' : 'BOOL::false';
53
		}
54
55
		return (string) $value;
56
	}
57
58
	/**
59
	 * Returns an exception if a module being added is not a valid module.
60
	 * @code 20
61
	 * @param string $module
62
	 * @return Module_Manager_Exception
63
	 */
64
	public static function invalid_module_class_name( string $module ): Module_Manager_Exception {
65
		$message = "{$module} must be an instance of the Module interface";
66
		return new Module_Manager_Exception( $message, 20 );
67
	}
68
69
	/**
70
	 * Failed to create Registration_Middleware, none instance created.
71
	 *
72
	 * @code 21
73
	 * @param mixed $created
74
	 * @return Module_Manager_Exception
75
	 */
76
	public static function failed_to_create_registration_middleware( $created ): Module_Manager_Exception {
77
78
		$created = self::cast_to_string( $created );
79
80
		$message = "Failed to create Registration_Middleware, invalid instance created. Created: {$created}";
81
		return new Module_Manager_Exception( $message, 21 );
82
	}
83
84
	/**
85
	 * Registration_Middleware is not a valid instance of the Registration_Middleware Interface.
86
	 *
87
	 * @code 22
88
	 * @param mixed $created
89
	 * @return Module_Manager_Exception
90
	 */
91
	public static function invalid_registration_middleware( $created ): Module_Manager_Exception {
92
		$created = self::cast_to_string( $created );
93
94
		$message = "{$created} was returned as the modules Middleware, but this does not implement Registration_Middleware interface";
95
		return new Module_Manager_Exception( $message, 22 );
96
	}
97
98
	/**
99
	 * None class added to registration service.
100
	 *
101
	 * @code 23
102
	 * @param mixed $passed
103
	 * @return Module_Manager_Exception
104
	 */
105
	public static function none_class_string_passed_to_registration( $passed ): Module_Manager_Exception {
106
		$passed = self::cast_to_string( $passed );
107
108
		$message = "None class-string \"{$passed}\" passed to the registration class list";
109
		return new Module_Manager_Exception( $message, 23 );
110
	}
111
}
112