1 | <?php |
||
2 | |||
3 | abstract class Logger { |
||
4 | /** |
||
5 | * @var string |
||
6 | */ |
||
7 | protected $user = ''; |
||
8 | |||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $specialLogUsers = []; |
||
13 | |||
14 | /** |
||
15 | * Only used as a cache value for IsUserInSpecialLogUsers. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private $isUserInSpecialLogUsers = []; |
||
20 | |||
21 | /** |
||
22 | * Only used as a cache value for IsAuthUserInSpecialLogUsers function. |
||
23 | * |
||
24 | * @var bool |
||
25 | */ |
||
26 | private $isAuthUserInSpecialLogUsers = false; |
||
27 | |||
28 | /** |
||
29 | * Returns the current user. |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | public function GetUser() { |
||
34 | return $this->user; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Sets the current user. |
||
39 | * |
||
40 | * @param array $value user information which is currently login |
||
41 | */ |
||
42 | public function SetUser($value) { |
||
43 | $this->user = $value; |
||
0 ignored issues
–
show
|
|||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Indicates if special log users are known. |
||
48 | * |
||
49 | * @return bool True if we do have to log some specific user. False otherwise. |
||
50 | */ |
||
51 | public function HasSpecialLogUsers() { |
||
52 | return !empty($this->specialLogUsers); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Indicates if the user is in the special log users. |
||
57 | * |
||
58 | * @param string $user |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | public function IsUserInSpecialLogUsers($user) { |
||
63 | if (isset($this->isUserInSpecialLogUsers[$user])) { |
||
64 | return true; |
||
65 | } |
||
66 | if ($this->HasSpecialLogUsers()) { |
||
67 | $specialLogUsers = $this->GetSpecialLogUsers(); |
||
68 | if (array_search($user, $specialLogUsers, true) !== false) { |
||
69 | $this->isUserInSpecialLogUsers[$user] = true; |
||
70 | |||
71 | return true; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return false; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Returns the current special log users array. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | public function GetSpecialLogUsers() { |
||
84 | return $this->specialLogUsers; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Sets the current special log users array. |
||
89 | */ |
||
90 | public function SetSpecialLogUsers(array $value) { |
||
91 | $this->isUserInSpecialLogUsers = []; // reset cache |
||
92 | $this->specialLogUsers = $value; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Check that the current login user is in the special log user array. |
||
97 | * This call is equivalent to `$this->IsUserInSpecialLogUsers($this->GetUser())` at the exception that this |
||
98 | * call uses cache so there won't be more than one check to the specialLogUser for the login user. |
||
99 | * |
||
100 | * @return bool true if user exist in special log user array else false |
||
101 | */ |
||
102 | public function IsAuthUserInSpecialLogUsers() { |
||
103 | if ($this->isAuthUserInSpecialLogUsers) { |
||
104 | return true; |
||
105 | } |
||
106 | if ($this->IsUserInSpecialLogUsers($this->GetUser())) { |
||
107 | $this->isAuthUserInSpecialLogUsers = true; |
||
108 | |||
109 | return true; |
||
110 | } |
||
111 | |||
112 | return false; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Logs a message with a given log level. |
||
117 | * |
||
118 | * @param int $logLevel The log level which will be configured in config file |
||
119 | * @param string $message The log message which we want to log in user specific log file |
||
120 | * @param mixed $detailMessage (optional) The detailed log message. it can be Error/Exception array. |
||
121 | * @param mixed $request (optional) The request log the the request data which sent by the user |
||
122 | */ |
||
123 | public function Log($logLevel, $message, $detailMessage = false, $request = false) { |
||
124 | if ($logLevel <= LOG_USER_LEVEL) { |
||
125 | if ($this->IsAuthUserInSpecialLogUsers()) { |
||
126 | $this->Write($logLevel, $message, $detailMessage, $request); |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Returns the string representation of the given $loglevel. |
||
133 | * |
||
134 | * @param int $loglevel one of the LOGLEVELs |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | protected function GetLogLevelString($loglevel) { |
||
139 | return match ($loglevel) { |
||
140 | LOGLEVEL_OFF => "", |
||
141 | LOGLEVEL_FATAL => "[FATAL]", |
||
142 | LOGLEVEL_ERROR => "[ERROR]", |
||
143 | LOGLEVEL_WARN => "[WARN]", |
||
144 | LOGLEVEL_INFO => "[INFO]", |
||
145 | LOGLEVEL_DEBUG => "[DEBUG]", |
||
146 | default => "", |
||
147 | }; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Writes a log message to the general log. |
||
152 | * |
||
153 | * @param int $logLevel The log level which will be configured in config file |
||
154 | * @param string $message The log message which we want to log in user specific log file |
||
155 | * @param mixed $detailMessage (optional) The detailed log message. it can be Error/Exception array. |
||
156 | * @param mixed $request (optional) The request log the the request data which sent by the user |
||
157 | */ |
||
158 | abstract protected function Write($logLevel, $message, $detailMessage, $request); |
||
159 | } |
||
160 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..