GeneralException::getErrorInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Implements CAS server exception classes for the WP CAS Server plugin.
4
 *
5
 * @version 1.1.0
6
 */
7
8
namespace Cassava\Exception;
9
10
/**
11
 * Base CAS server exception.
12
 *
13
 * @version 1.1.0
14
 * @since   1.1.0
15
 */
16
class GeneralException extends \Exception {
17
18
	/**
19
	 * Internal Error
20
	 */
21
	const ERROR_INTERNAL_ERROR = 'INTERNAL_ERROR';
22
23
	/**
24
	 * Error code.
25
	 * @var string
26
	 */
27
	protected $casCode;
28
29
	/**
30
	 * Exception constructor.
31
	 *
32
	 * @param string $message Exception message.
33
	 * @param string $casCode CAS error code (default: "INTERNAL_ERROR").
34
	 */
35
	public function __construct( $message = '', $casCode = self::ERROR_INTERNAL_ERROR ) {
36
		parent::__construct( $message );
37
38
		$this->casCode = $casCode;
39
	}
40
41
	/**
42
	 * Generate a new exception instance from a WordPress error.
43
	 *
44
	 * @param  \WP_Error        $error WordPress error.
45
	 * @return GeneralException        WordPress error as an exception.
46
	 */
47
	public static function fromError( \WP_Error $error ) {
48
		$code    = $error->get_error_code();
49
		$message = $error->get_error_message( $code );
50
		return new static ($message, $code);
51
	}
52
53
	/**
54
	 * Error code getter.
55
	 *
56
	 * @return string Associated error code.
57
	 */
58
	final public function getCASCode() {
59
		return $this->casCode;
60
	}
61
62
	/**
63
	 * Returns a WordPress error object based on the exception.
64
	 *
65
	 * @return \WP_Error WordPress error.
66
	 */
67
	public function getErrorInstance() {
68
		return new \WP_Error( $this->casCode, $this->message );
69
	}
70
71
}
72