BaseException::getDisplayMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
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-2024 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
	 * @var null|string
25
	 */
26
	private $baseFile;
27
28
	/**
29
	 * Flag to check if exception is already handled or not.
30
	 *
31
	 * @var bool
32
	 */
33
	public $isHandled = false;
34
35
	/**
36
	 * The exception message to show at client side.
37
	 *
38
	 * @var null|string
39
	 */
40
	public $displayMessage;
41
42
	/**
43
	 * Flag for allow to exception details message or not.
44
	 *
45
	 * @var bool
46
	 */
47
	public $allowToShowDetailsMessage = false;
48
49
	/**
50
	 * The exception title to show as a message box title at client side.
51
	 *
52
	 * @var null|string
53
	 */
54
	public $title;
55
56
	/**
57
	 * The notification type by which exception needs to be shown at client side.
58
	 *
59
	 * @var string
60
	 */
61
	public $notificationType = "";
62
63
	/**
64
	 * Constructs the exception.
65
	 *
66
	 * @param string    $errorMessage
67
	 * @param int       $code
68
	 * @param Throwable $previous
69
	 * @param string    $displayMessage
70
	 */
71
	public function __construct($errorMessage, $code = 0, $previous = null, $displayMessage = null) {
72
		// assign display message
73
		$this->displayMessage = $displayMessage;
74
75
		parent::__construct($errorMessage, (int) $code, $previous);
76
	}
77
78
	/**
79
	 * Returns file name and line number combined where exception occurred.
80
	 */
81
	public function getFileLine(): string {
82
		return $this->getBaseFile() . ':' . $this->getLine();
83
	}
84
85
	/**
86
	 * Returns message that should be sent to client to display.
87
	 */
88
	public function getDisplayMessage(): string {
89
		if (!is_null($this->displayMessage)) {
90
			return $this->displayMessage;
91
		}
92
93
		return $this->getMessage();
94
	}
95
96
	/**
97
	 * Sets display message of an exception that will be sent to the client side
98
	 * to show it to user.
99
	 *
100
	 */
101
	public function setDisplayMessage(string $message): void {
102
		$this->displayMessage = $message . " (" . mapi_strerror($this->getCode()) . ")";
103
	}
104
105
	/**
106
	 * Sets the  title of an exception that will be sent to the client side
107
	 * to show it to user.
108
	 *
109
	 */
110
	public function setTitle(string $title): void {
111
		$this->title = $title;
112
	}
113
114
	/**
115
	 * Returns title that should be sent to client to display as a message box title.
116
	 */
117
	public function getTitle(): null|string {
118
		return $this->title;
119
	}
120
121
	/**
122
	 * Sets a flag in exception class to indicate that exception is already handled
123
	 * so if it is caught again in the top level of function stack
124
	 * then we have to silently ignore it.
125
	 */
126
	public function setHandled(): void {
127
		$this->isHandled = true;
128
	}
129
130
	/**
131
	 * Returns base path of the file where exception occurred.
132
	 */
133
	public function getBaseFile(): string {
134
		if (is_null($this->baseFile)) {
135
			$this->baseFile = basename(parent::getFile());
136
		}
137
138
		return $this->baseFile;
139
	}
140
141
	/**
142
	 * Returns the name of the class of exception.
143
	 *
144
	 */
145
	public function getName(): string {
146
		return get_class($this);
147
	}
148
149
	/**
150
	 * Sets a type of notification by which exception needs to be shown at client side.
151
	 *
152
	 */
153
	public function setNotificationType(string $notificationType): void {
154
		$this->notificationType = $notificationType;
155
	}
156
157
	/**
158
	 * Returns a type of notification to show an exception.
159
	 */
160
	public function getNotificationType(): string {
161
		return $this->notificationType;
162
	}
163
164
	/**
165
	 * Returns details of the error message if allowToShowDetailsMessage is set.
166
	 *
167
	 * @return string returns details error message
168
	 */
169
	public function getDetailsMessage(): string {
170
		return $this->allowToShowDetailsMessage ? $this->__toString() : '';
171
	}
172
173
	// @TODO getTrace and getTraceAsString
174
}
175