1 | <?php |
||
13 | class Recorder extends Component{ |
||
14 | |||
15 | /** |
||
16 | * @var mixed |
||
17 | */ |
||
18 | protected $config = []; |
||
19 | |||
20 | /** |
||
21 | * The constructor |
||
22 | */ |
||
23 | 39 | public function __construct() { |
|
26 | |||
27 | /** |
||
28 | * Records an Exception to the database |
||
29 | * @param Exception $e The exception you want to record |
||
30 | * @return ExceptionModel|false |
||
31 | */ |
||
32 | 12 | public function record(Exception $e) |
|
33 | { |
||
34 | 12 | if($this->shouldntHandle($e)){ |
|
35 | 3 | return false; |
|
36 | } |
||
37 | |||
38 | $opts = [ |
||
39 | 9 | 'class' => get_class($e), |
|
40 | 9 | 'file' => $e->getFile(), |
|
41 | 9 | 'line' => $e->getLine(), |
|
42 | 9 | 'code' => (is_int($e->getCode()) ? $e->getCode() : 0), |
|
43 | 9 | 'message' => $e->getMessage(), |
|
44 | 9 | 'trace' => $e->getTraceAsString(), |
|
45 | 9 | ]; |
|
46 | |||
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 | $opts[$key] = $this->collect($key, $e); |
|
54 | 3 | } |
|
55 | 6 | } |
|
56 | |||
57 | 6 | return ExceptionModel::create($opts); |
|
58 | 3 | } catch (Exception $e) { |
|
59 | 3 | $code = (is_int($e->getCode()) ? $e->getCode() : 0); |
|
60 | 3 | throw new RecorderFailedException($e->getMessage(), $code, $e); |
|
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Checks the config to see if you can collect certain information |
||
66 | * @param string $type the config value you want to check |
||
67 | * @return boolean |
||
68 | */ |
||
69 | 9 | private function canCollect($type) { |
|
75 | |||
76 | /** |
||
77 | * @param string $key |
||
78 | */ |
||
79 | 3 | protected function collect($key,Exception $e = null){ |
|
97 | |||
98 | /** |
||
99 | * Gets the ID of the User that is logged in |
||
100 | * @return integer|null The ID of the User or Null if not logged in |
||
101 | */ |
||
102 | 3 | protected function getUserId() { |
|
110 | |||
111 | /** |
||
112 | * Gets the Method of the Request |
||
113 | * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... |
||
114 | */ |
||
115 | 3 | protected function getMethod() { |
|
123 | |||
124 | /** |
||
125 | * Gets the input data of the Request |
||
126 | * @return array|null The Input data or null |
||
127 | */ |
||
128 | 3 | protected function getData() { |
|
129 | 3 | $data = Input::all(); |
|
130 | 3 | if (is_array($data)) { |
|
131 | 3 | $this->excludeKeys($data); |
|
132 | 3 | return $data; |
|
133 | } else { |
||
134 | return null; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Gets the URL of the Request |
||
140 | * @return string|null Returns a URL string or null |
||
141 | */ |
||
142 | 3 | protected function getUrl() { |
|
143 | 3 | $url = Request::url(); |
|
144 | 3 | if (is_string($url)) { |
|
145 | 3 | return $url; |
|
146 | } else { |
||
147 | return null; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Gets the status code of the Exception |
||
153 | * @param Exception $e The Exception to check |
||
154 | * @return string|integer The status code value |
||
155 | */ |
||
156 | 3 | protected function getStatusCode(Exception $e) { |
|
163 | |||
164 | /** |
||
165 | * This function will remove all keys from an array recursively as defined in the config file |
||
166 | * @param array $data The array to remove keys from |
||
167 | * @return void |
||
168 | */ |
||
169 | 9 | protected function excludeKeys(array $data){ |
|
181 | } |
||
182 |