Test Failed
Branch master (1ac602)
by Mike
02:45
created

BaseException::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nop 0
dl 0
loc 2
rs 10
nc 1
1
<?php
2
/*
3
 * SPDX-License-Identifier: AGPL-3.0-only
4
 * SPDX-FileCopyrightText: Copyright 2005-2016 Zarafa Deutschland GmbH
5
 * SPDX-FileCopyrightText: Copyright 2020-2022 grommunio GmbH
6
 */
7
8
/**
9
 * Defines a base exception class for all custom exceptions, so every exceptions that
10
 * is thrown/caught by this application should extend this base class and make use of it.
11
 *
12
 * Some basic function of Exception class
13
 * getMessage()- message of exception
14
 * getCode() - code of exception
15
 * getFile() - source filename
16
 * getLine() - source line
17
 * getTrace() - n array of the backtrace()
18
 * getTraceAsString() - formatted string of trace
19
 */
20
class BaseException extends Exception {
21
	/**
22
	 * Base name of the file, so we don't have to use static path of the file.
23
	 */
24
	private $baseFile;
25
26
	/**
27
	 * Flag to check if exception is already handled or not.
28
	 */
29
	public $isHandled = false;
30
31
	/**
32
	 * The exception message to show at client side.
33
	 */
34
	public $displayMessage;
35
36
	/**
37
	 * Flag for allow to exception details message or not.
38
	 */
39
	public $allowToShowDetailsMessage = false;
40
41
	/**
42
	 * The exception title to show as a message box title at client side.
43
	 */
44
	public $title;
45
46
	/**
47
	 * Construct the exception.
48
	 *
49
	 * @param string    $errorMessage
50
	 * @param int       $code
51
	 * @param Exception $previous
52
	 * @param string    $displayMessage
53
	 */
54
	public function __construct($errorMessage, $code = 0, Exception $previous = null, $displayMessage = null) {
55
		// assign display message
56
		$this->displayMessage = $displayMessage;
57
58
		parent::__construct($errorMessage, (int) $code, $previous);
59
	}
60
61
	/**
62
	 * @return string returns file name and line number combined where exception occurred
63
	 */
64
	public function getFileLine() {
65
		return $this->getBaseFile() . ':' . $this->getLine();
66
	}
67
68
	/**
69
	 * @return string returns message that should be sent to client to display
70
	 */
71
	public function getDisplayMessage() {
72
		if (!is_null($this->displayMessage)) {
73
			return $this->displayMessage;
74
		}
75
76
		return $this->getMessage();
77
	}
78
79
	/**
80
	 * Function sets display message of an exception that will be sent to the client side
81
	 * to show it to user.
82
	 *
83
	 * @param string $message display message
84
	 */
85
	public function setDisplayMessage($message) {
86
		$this->displayMessage = $message . " (" . mapi_strerror($this->getCode()) . ")";
87
	}
88
89
	/**
90
	 * Function sets title of an exception that will be sent to the client side
91
	 * to show it to user.
92
	 *
93
	 * @param string $title title of an exception
94
	 */
95
	public function setTitle($title) {
96
		$this->title = $title;
97
	}
98
99
	/**
100
	 * @return string returns title that should be sent to client to display as a message box
101
	 *                title
102
	 */
103
	public function getTitle() {
104
		return $this->title;
105
	}
106
107
	/**
108
	 * Function sets a flag in exception class to indicate that exception is already handled
109
	 * so if it is caught again in the top level of function stack then we have to silently
110
	 * ignore it.
111
	 */
112
	public function setHandled() {
113
		$this->isHandled = true;
114
	}
115
116
	/**
117
	 * @return string returns base path of the file where exception occurred
118
	 */
119
	public function getBaseFile() {
120
		if (is_null($this->baseFile)) {
121
			$this->baseFile = basename(parent::getFile());
122
		}
123
124
		return $this->baseFile;
125
	}
126
127
	/**
128
	 * Name of the class of exception.
129
	 *
130
	 * @return string
131
	 */
132
	public function getName() {
133
		return get_class($this);
134
	}
135
136
	/**
137
	 * It will return details error message if allowToShowDetailsMessage is set.
138
	 *
139
	 * @return string returns details error message
140
	 */
141
	public function getDetailsMessage() {
142
		return $this->allowToShowDetailsMessage ? $this->__toString() : '';
143
	}
144
145
	// @TODO getTrace and getTraceAsString
146
}
147