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\Models\ExceptionModel; |
11
|
|
|
|
12
|
|
|
class Recorder { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
protected $config = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The constructor |
21
|
|
|
*/ |
22
|
21 |
|
public function __construct() { |
23
|
21 |
|
$this->config = config('lern.record'); |
24
|
21 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Records an Exception to the database |
28
|
|
|
* @param Exception $e The exception you want to record |
29
|
|
|
* @return ExceptionModel |
30
|
|
|
*/ |
31
|
3 |
|
public function record(Exception $e) |
32
|
|
|
{ |
33
|
|
|
$opts = [ |
34
|
3 |
|
'class' => get_class($e), |
35
|
3 |
|
'file' => $e->getFile(), |
36
|
3 |
|
'line' => $e->getLine(), |
37
|
3 |
|
'code' => $e->getCode(), |
38
|
3 |
|
'message' => $e->getMessage(), |
39
|
3 |
|
'trace' => $e->getTraceAsString(), |
40
|
3 |
|
]; |
41
|
|
|
|
42
|
|
|
|
43
|
3 |
|
$configDependant = ['user_id','status_code','method','data','url']; |
44
|
|
|
|
45
|
3 |
|
foreach ($configDependant as $key) { |
46
|
3 |
|
if($this->canCollect($key)){ |
47
|
|
|
$opts[$key] = $this->collect($key,$e); |
48
|
|
|
} |
49
|
3 |
|
} |
50
|
|
|
|
51
|
3 |
|
return ExceptionModel::create($opts); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Checks the config to see if you can collect certain information |
56
|
|
|
* @param string $type the config value you want to check |
57
|
|
|
* @return boolean |
58
|
|
|
*/ |
59
|
3 |
|
private function canCollect($type) { |
60
|
3 |
|
if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) { |
61
|
|
|
return $this->config['collect'][$type] === true; |
62
|
|
|
} |
63
|
3 |
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function collect($key,Exception $e = null){ |
67
|
|
|
switch ($key) { |
68
|
|
|
case 'user_id': |
69
|
|
|
return $this->getUserId(); |
70
|
|
|
break; |
|
|
|
|
71
|
|
|
|
72
|
|
|
case 'method': |
73
|
|
|
return $this->getMethod(); |
74
|
|
|
break; |
|
|
|
|
75
|
|
|
|
76
|
|
|
case 'status_code': |
77
|
|
|
return $this->getStatusCode($e); |
|
|
|
|
78
|
|
|
break; |
|
|
|
|
79
|
|
|
|
80
|
|
|
case 'url': |
81
|
|
|
return $this->getUrl(); |
82
|
|
|
break; |
|
|
|
|
83
|
|
|
|
84
|
|
|
case 'data': |
85
|
|
|
return $this->getData(); |
86
|
|
|
break; |
|
|
|
|
87
|
|
|
|
88
|
|
|
default: |
89
|
|
|
throw new Exception("{$key} is not supported! Therefore it cannot be collected!"); |
90
|
|
|
break; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets the ID of the User that is logged in |
96
|
|
|
* @return integer|null The ID of the User or Null if not logged in |
97
|
|
|
*/ |
98
|
|
|
protected function getUserId() { |
99
|
|
|
$user = Auth::user(); |
100
|
|
|
if (is_object($user)) { |
101
|
|
|
return $user->id; |
102
|
|
|
} else { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Gets the Method of the Request |
109
|
|
|
* @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... |
110
|
|
|
*/ |
111
|
|
|
protected function getMethod() { |
112
|
|
|
$method = Request::method(); |
113
|
|
|
if (!empty($method)) { |
114
|
|
|
return $method; |
115
|
|
|
} else { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets the input data of the Request |
122
|
|
|
* @return array|null The Input data or null |
123
|
|
|
*/ |
124
|
|
|
protected function getData() { |
125
|
|
|
$data = Input::all(); |
126
|
|
|
if (is_array($data)) { |
127
|
|
|
return $data; |
128
|
|
|
} else { |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Gets the URL of the Request |
135
|
|
|
* @return string|null Returns a URL string or null |
136
|
|
|
*/ |
137
|
|
|
protected function getUrl() { |
138
|
|
|
$url = Request::url(); |
139
|
|
|
if (is_string($url)) { |
140
|
|
|
return $url; |
141
|
|
|
} else { |
142
|
|
|
return null; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Gets the status code of the Exception |
148
|
|
|
* @param Exception $e The Exception to check |
149
|
|
|
* @return string|integer The status code value |
150
|
|
|
*/ |
151
|
|
|
protected function getStatusCode(Exception $e) { |
152
|
|
|
if ($e instanceof HttpExceptionInterface) { |
153
|
|
|
return $e->getStatusCode(); |
154
|
|
|
} else { |
155
|
|
|
return 0; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.