1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Tylercd100\LERN\Components;
|
4
|
|
|
|
5
|
|
|
use Exception;
|
6
|
|
|
use Illuminate\Support\Facades\Auth;
|
7
|
|
|
use Illuminate\Support\Facades\Input;
|
8
|
|
|
use Illuminate\Support\Facades\Request;
|
9
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
10
|
|
|
use Tylercd100\LERN\Exceptions\RecorderFailedException;
|
11
|
|
|
use Tylercd100\LERN\Models\ExceptionModel;
|
12
|
|
|
|
13
|
|
|
class Recorder extends Component {
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
* @var mixed
|
17
|
|
|
*/
|
18
|
|
|
protected $config = [];
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* The constructor
|
22
|
|
|
*/
|
23
|
42 |
|
public function __construct() {
|
24
|
42 |
|
$this->config = config('lern.record');
|
25
|
42 |
|
}
|
26
|
|
|
|
27
|
|
|
/**
|
28
|
|
|
* Records an Exception to the database
|
29
|
|
|
* @param Exception $e The exception you want to record
|
30
|
|
|
* @return false|ExceptionModel
|
31
|
|
|
* @throws RecorderFailedException
|
32
|
|
|
*/
|
33
|
9 |
|
public function record(Exception $e)
|
34
|
|
|
{
|
35
|
9 |
|
if ($this->shouldntHandle($e)) {
|
36
|
3 |
|
return false;
|
37
|
|
|
}
|
38
|
|
|
|
39
|
|
|
$opts = [
|
40
|
6 |
|
'class' => get_class($e),
|
41
|
6 |
|
'file' => $e->getFile(),
|
42
|
6 |
|
'line' => $e->getLine(),
|
43
|
6 |
|
'code' => (is_int($e->getCode()) ? $e->getCode() : 0),
|
44
|
6 |
|
'message' => $e->getMessage(),
|
45
|
6 |
|
'trace' => $e->getTraceAsString(),
|
46
|
|
|
];
|
47
|
|
|
|
48
|
6 |
|
$configDependant = array_keys($this->config['collect']);
|
49
|
|
|
|
50
|
|
|
try {
|
51
|
6 |
|
foreach ($configDependant as $key) {
|
52
|
6 |
|
if ($this->canCollect($key)) {
|
53
|
3 |
|
$value = $this->collect($key, $e);
|
54
|
3 |
|
if ($value !== null) {
|
55
|
6 |
|
$opts[$key] = $value;
|
56
|
|
|
}
|
57
|
|
|
}
|
58
|
|
|
}
|
59
|
|
|
|
60
|
6 |
|
return ExceptionModel::create($opts);
|
61
|
|
|
} catch (Exception $e) {
|
62
|
|
|
$code = (is_int($e->getCode()) ? $e->getCode() : 0);
|
63
|
|
|
throw new RecorderFailedException($e->getMessage(), $code, $e);
|
64
|
|
|
}
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
/**
|
68
|
|
|
* Checks the config to see if you can collect certain information
|
69
|
|
|
* @param string $type the config value you want to check
|
70
|
|
|
* @return boolean
|
71
|
|
|
*/
|
72
|
6 |
|
private function canCollect($type) {
|
73
|
6 |
|
if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
|
74
|
3 |
|
return $this->config['collect'][$type] === true;
|
75
|
|
|
}
|
76
|
3 |
|
return false;
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* @param string $key
|
81
|
|
|
* @param Exception $e
|
82
|
|
|
* @return array|int|null|string
|
83
|
|
|
* @throws Exception
|
84
|
|
|
*/
|
85
|
3 |
|
protected function collect($key, Exception $e = null) {
|
86
|
|
|
switch ($key) {
|
87
|
3 |
|
case 'user_id':
|
88
|
3 |
|
return $this->getUserId();
|
89
|
3 |
|
case 'method':
|
90
|
3 |
|
return $this->getMethod();
|
91
|
3 |
|
case 'url':
|
92
|
3 |
|
return $this->getUrl();
|
93
|
3 |
|
case 'data':
|
94
|
3 |
|
return $this->getData();
|
95
|
3 |
|
case 'ip':
|
96
|
|
|
return $this->getIp();
|
97
|
3 |
|
case 'status_code':
|
98
|
3 |
|
if ($e === null) {
|
99
|
|
|
return 0;
|
100
|
|
|
}
|
101
|
3 |
|
return $this->getStatusCode($e);
|
102
|
|
|
default:
|
103
|
|
|
throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
|
104
|
|
|
}
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
/**
|
108
|
|
|
* Gets the ID of the User that is logged in
|
109
|
|
|
* @return integer|null The ID of the User or Null if not logged in
|
110
|
|
|
*/
|
111
|
3 |
|
protected function getUserId() {
|
112
|
3 |
|
$user = Auth::user();
|
113
|
3 |
|
if (is_object($user) && !empty($user->id)) {
|
|
|
|
|
114
|
|
|
return $user->id;
|
|
|
|
|
115
|
|
|
} else {
|
116
|
3 |
|
return null;
|
117
|
|
|
}
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* Gets the Method of the Request
|
122
|
|
|
* @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
|
123
|
|
|
*/
|
124
|
3 |
|
protected function getMethod() {
|
125
|
3 |
|
$method = Request::method();
|
126
|
3 |
|
if (!empty($method)) {
|
127
|
3 |
|
return $method;
|
128
|
|
|
} else {
|
129
|
|
|
return null;
|
130
|
|
|
}
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* Gets the input data of the Request
|
135
|
|
|
* @return array|null The Input data or null
|
136
|
|
|
*/
|
137
|
6 |
|
protected function getData() {
|
138
|
6 |
|
$data = Input::all();
|
139
|
6 |
|
if (is_array($data)) {
|
140
|
6 |
|
return $this->excludeKeys($data);
|
141
|
|
|
} else {
|
142
|
|
|
return null;
|
143
|
|
|
}
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
/**
|
147
|
|
|
* Gets the URL of the Request
|
148
|
|
|
* @return string|null Returns a URL string or null
|
149
|
|
|
*/
|
150
|
3 |
|
protected function getUrl() {
|
151
|
3 |
|
$url = Request::url();
|
152
|
3 |
|
if (is_string($url)) {
|
153
|
3 |
|
return $url;
|
154
|
|
|
} else {
|
155
|
|
|
return null;
|
156
|
|
|
}
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* Returns the IP from the request
|
161
|
|
|
*
|
162
|
|
|
* @return string
|
163
|
|
|
*/
|
164
|
|
|
protected function getIp() {
|
165
|
|
|
return Request::ip();
|
166
|
|
|
}
|
167
|
|
|
|
168
|
|
|
/**
|
169
|
|
|
* Gets the status code of the Exception
|
170
|
|
|
* @param Exception $e The Exception to check
|
171
|
|
|
* @return string|integer The status code value
|
172
|
|
|
*/
|
173
|
3 |
|
protected function getStatusCode(Exception $e) {
|
174
|
3 |
|
if ($e instanceof HttpExceptionInterface) {
|
175
|
|
|
return $e->getStatusCode();
|
176
|
|
|
} else {
|
177
|
3 |
|
return 0;
|
178
|
|
|
}
|
179
|
|
|
}
|
180
|
|
|
|
181
|
|
|
/**
|
182
|
|
|
* This function will remove all keys from an array recursively as defined in the config file
|
183
|
|
|
* @param array $data The array to remove keys from
|
184
|
|
|
* @return void
|
185
|
|
|
*/
|
186
|
12 |
|
protected function excludeKeys(array $data) {
|
187
|
12 |
|
$keys = isset($this->config['excludeKeys']) ? $this->config['excludeKeys'] : [];
|
188
|
12 |
|
foreach ($data as $key => &$value) {
|
189
|
9 |
|
if (in_array($key, $keys)) {
|
190
|
9 |
|
unset($data[$key]);
|
191
|
9 |
|
} else if (is_array($value)) {
|
192
|
9 |
|
$value = $this->excludeKeys($value);
|
193
|
|
|
}
|
194
|
|
|
}
|
195
|
|
|
|
196
|
12 |
|
return $data;
|
197
|
|
|
}
|
198
|
|
|
}
|
199
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: