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
|
12 |
|
public function record(Exception $e) |
34
|
|
|
{ |
35
|
12 |
|
if ($this->shouldntHandle($e)) { |
36
|
3 |
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$opts = [ |
40
|
9 |
|
'class' => get_class($e), |
41
|
9 |
|
'file' => $e->getFile(), |
42
|
9 |
|
'line' => $e->getLine(), |
43
|
9 |
|
'code' => (is_int($e->getCode()) ? $e->getCode() : 0), |
44
|
9 |
|
'message' => $e->getMessage(), |
45
|
9 |
|
'trace' => $e->getTraceAsString(), |
46
|
9 |
|
]; |
47
|
|
|
|
48
|
9 |
|
$configDependant = ['user_id', 'status_code', 'method', 'data', 'url']; |
49
|
|
|
|
50
|
|
|
try { |
51
|
9 |
|
foreach ($configDependant as $key) { |
52
|
9 |
|
if ($this->canCollect($key)) { |
53
|
6 |
|
$value = $this->collect($key, $e); |
54
|
3 |
|
if ($value !== null) { |
55
|
3 |
|
$opts[$key] = $value; |
56
|
3 |
|
} |
57
|
3 |
|
} |
58
|
6 |
|
} |
59
|
|
|
|
60
|
6 |
|
return ExceptionModel::create($opts); |
61
|
3 |
|
} catch (Exception $e) { |
62
|
3 |
|
$code = (is_int($e->getCode()) ? $e->getCode() : 0); |
63
|
3 |
|
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
|
9 |
|
private function canCollect($type) { |
73
|
9 |
|
if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) { |
74
|
6 |
|
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 'status_code': |
96
|
3 |
|
if ($e === null) { |
97
|
|
|
return 0; |
98
|
|
|
} |
99
|
3 |
|
return $this->getStatusCode($e); |
100
|
|
|
default: |
101
|
|
|
throw new Exception("{$key} is not supported! Therefore it cannot be collected!"); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets the ID of the User that is logged in |
107
|
|
|
* @return integer|null The ID of the User or Null if not logged in |
108
|
|
|
*/ |
109
|
3 |
|
protected function getUserId() { |
110
|
3 |
|
$user = Auth::user(); |
111
|
3 |
|
if (is_object($user) && !empty($user->id)) { |
112
|
|
|
return $user->id; |
113
|
|
|
} else { |
114
|
3 |
|
return null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets the Method of the Request |
120
|
|
|
* @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... |
121
|
|
|
*/ |
122
|
3 |
|
protected function getMethod() { |
123
|
3 |
|
$method = Request::method(); |
124
|
3 |
|
if (!empty($method)) { |
125
|
3 |
|
return $method; |
126
|
|
|
} else { |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Gets the input data of the Request |
133
|
|
|
* @return array|null The Input data or null |
134
|
|
|
*/ |
135
|
3 |
|
protected function getData() { |
136
|
3 |
|
$data = Input::all(); |
137
|
3 |
|
if (is_array($data)) { |
138
|
3 |
|
$this->excludeKeys($data); |
139
|
3 |
|
return $data; |
140
|
|
|
} else { |
141
|
|
|
return null; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the URL of the Request |
147
|
|
|
* @return string|null Returns a URL string or null |
148
|
|
|
*/ |
149
|
3 |
|
protected function getUrl() { |
150
|
3 |
|
$url = Request::url(); |
151
|
3 |
|
if (is_string($url)) { |
152
|
3 |
|
return $url; |
153
|
|
|
} else { |
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Gets the status code of the Exception |
160
|
|
|
* @param Exception $e The Exception to check |
161
|
|
|
* @return string|integer The status code value |
162
|
|
|
*/ |
163
|
3 |
|
protected function getStatusCode(Exception $e) { |
164
|
3 |
|
if ($e instanceof HttpExceptionInterface) { |
165
|
|
|
return $e->getStatusCode(); |
166
|
|
|
} else { |
167
|
3 |
|
return 0; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* This function will remove all keys from an array recursively as defined in the config file |
173
|
|
|
* @param array $data The array to remove keys from |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
9 |
|
protected function excludeKeys(array $data) { |
177
|
9 |
|
$keys = isset($this->config['excludeKeys']) ? $this->config['excludeKeys'] : []; |
178
|
9 |
|
foreach ($data as $key => &$value) { |
179
|
6 |
|
if (in_array($key, $keys)) { |
180
|
6 |
|
unset($data[$key]); |
181
|
6 |
|
} else if (is_array($value)) { |
182
|
3 |
|
$value = $this->excludeKeys($value); |
183
|
3 |
|
} |
184
|
9 |
|
} |
185
|
|
|
|
186
|
9 |
|
return $data; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|