1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\LERN\Components; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Tylercd100\LERN\Models\ExceptionModel; |
7
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Illuminate\Support\Facades\Request; |
10
|
|
|
use Illuminate\Support\Facades\Input; |
11
|
|
|
|
12
|
|
|
class Recorder { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
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 Tylercd100\LERN\Models\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
|
3 |
|
$opts['status_code'] = $this->getStatusCode($e); |
43
|
3 |
|
$opts['user_id'] = $this->getUserId($e); |
|
|
|
|
44
|
3 |
|
$opts['method'] = $this->getMethod($e); |
|
|
|
|
45
|
3 |
|
$opts['data'] = $this->getData($e); |
|
|
|
|
46
|
3 |
|
$opts['url'] = $this->getUrl($e); |
|
|
|
|
47
|
|
|
|
48
|
3 |
|
return ExceptionModel::create($opts); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks the config to see if you can collect certain information |
53
|
|
|
* @param string $type the config value you want to check |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
3 |
|
private function canCollect($type){ |
57
|
3 |
|
if(!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])){ |
58
|
|
|
return $this->config['collect'][$type] === true; |
59
|
|
|
} |
60
|
3 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the ID of the User that is logged in |
65
|
|
|
* @return integer|null The ID of the User or Null if not logged in |
66
|
|
|
*/ |
67
|
3 |
|
protected function getUserId(){ |
68
|
3 |
|
$user = Auth::user(); |
69
|
3 |
|
if($this->canCollect('user_id') && is_object($user)) { |
70
|
|
|
return $user->id; |
71
|
|
|
} else { |
72
|
3 |
|
return null; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the Method of the Request |
78
|
|
|
* @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... |
79
|
|
|
*/ |
80
|
3 |
|
protected function getMethod(){ |
81
|
3 |
|
$method = Request::method(); |
82
|
3 |
|
if($this->canCollect('method') && !empty($method)) { |
83
|
|
|
return $method; |
84
|
|
|
} else { |
85
|
3 |
|
return null; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets the input data of the Request |
91
|
|
|
* @return array|null The Input data or null |
92
|
|
|
*/ |
93
|
3 |
|
protected function getData(){ |
94
|
3 |
|
$data = Input::all(); |
95
|
3 |
|
if($this->canCollect('data') && is_array($data)) { |
96
|
|
|
return $data; |
97
|
|
|
} else { |
98
|
3 |
|
return null; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the URL of the Request |
104
|
|
|
* @return string|null Returns a URL string or null |
105
|
|
|
*/ |
106
|
3 |
|
protected function getUrl(){ |
107
|
3 |
|
$url = Request::url(); |
108
|
3 |
|
if($this->canCollect('url') && is_array($url)) { |
109
|
|
|
return $url; |
110
|
|
|
} else { |
111
|
3 |
|
return null; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Gets the status code of the Exception |
117
|
|
|
* @param Exception $e The Exception to check |
118
|
|
|
* @return string|integer The status code value |
119
|
|
|
*/ |
120
|
3 |
|
protected function getStatusCode(Exception $e){ |
121
|
3 |
|
if ($e instanceof HttpExceptionInterface) { |
122
|
|
|
return $e->getStatusCode(); |
123
|
|
|
} else { |
124
|
3 |
|
return 0; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
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..