Passed
Push — master ( ff661f...e7383b )
by
unknown
17:40 queued 15:39
created

class.baseexception.php (2 issues)

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
	 * @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 false
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
	 * Construct the exception.
58
	 *
59
	 * @param string    $errorMessage
60
	 * @param int       $code
61
	 * @param Exception $previous
62
	 * @param string    $displayMessage
63
	 */
64
	public function __construct($errorMessage, $code = 0, Exception $previous = null, $displayMessage = null) {
65
		// assign display message
66
		$this->displayMessage = $displayMessage;
67
68
		parent::__construct($errorMessage, (int) $code, $previous);
69
	}
70
71
	/**
72
	 * @return string returns file name and line number combined where exception occurred
73
	 */
74
	public function getFileLine(): string {
75
		return $this->getBaseFile() . ':' . $this->getLine();
76
	}
77
78
	/**
79
	 * @return string returns message that should be sent to client to display
80
	 */
81
	public function getDisplayMessage() {
82
		if (!is_null($this->displayMessage)) {
83
			return $this->displayMessage;
84
		}
85
86
		return $this->getMessage();
87
	}
88
89
	/**
90
	 * Function sets display message of an exception that will be sent to the client side
91
	 * to show it to user.
92
	 *
93
	 * @param string $message display message
94
	 */
95
	public function setDisplayMessage($message): void {
96
		$this->displayMessage = $message . " (" . mapi_strerror($this->getCode()) . ")";
0 ignored issues
show
The function mapi_strerror was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
		$this->displayMessage = $message . " (" . /** @scrutinizer ignore-call */ mapi_strerror($this->getCode()) . ")";
Loading history...
97
	}
98
99
	/**
100
	 * Function sets title of an exception that will be sent to the client side
101
	 * to show it to user.
102
	 *
103
	 * @param string $title title of an exception
104
	 */
105
	public function setTitle($title): void {
106
		$this->title = $title;
107
	}
108
109
	/**
110
	 * @return null|string returns title that should be sent to client to display as a message box title
111
	 */
112
	public function getTitle() {
113
		return $this->title;
114
	}
115
116
	/**
117
	 * Function sets a flag in exception class to indicate that exception is already handled
118
	 * so if it is caught again in the top level of function stack then we have to silently
119
	 * ignore it.
120
	 */
121
	public function setHandled(): void {
122
		$this->isHandled = true;
123
	}
124
125
	/**
126
	 * @return string returns base path of the file where exception occurred
127
	 */
128
	public function getBaseFile() {
129
		if (is_null($this->baseFile)) {
130
			$this->baseFile = basename(parent::getFile());
131
		}
132
133
		return $this->baseFile;
134
	}
135
136
	/**
137
	 * Name of the class of exception.
138
	 *
139
	 * @return get-class-of<$this, BaseException&static>
0 ignored issues
show
Documentation Bug introduced by
The doc comment get-class-of<$this, BaseException&static> at position 0 could not be parsed: Unknown type name 'get-class-of' at position 0 in get-class-of<$this, BaseException&static>.
Loading history...
140
	 */
141
	public function getName(): string {
142
		return get_class($this);
143
	}
144
145
	/**
146
	 * It will return details error message if allowToShowDetailsMessage is set.
147
	 *
148
	 * @return string returns details error message
149
	 */
150
	public function getDetailsMessage() {
151
		return $this->allowToShowDetailsMessage ? $this->__toString() : '';
152
	}
153
154
	// @TODO getTrace and getTraceAsString
155
}
156