1 | <?php |
||
13 | class Recorder extends Component{ |
||
14 | |||
15 | /** |
||
16 | * @var mixed |
||
17 | */ |
||
18 | protected $config = []; |
||
19 | |||
20 | /** |
||
21 | * The constructor |
||
22 | */ |
||
23 | 33 | 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' => $e->getCode(), |
|
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 | throw new RecorderFailedException($e->getMessage(), $e->getCode(), $e); |
|
60 | } |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Checks the config to see if you can collect certain information |
||
65 | * @param string $type the config value you want to check |
||
66 | * @return boolean |
||
67 | */ |
||
68 | 9 | private function canCollect($type) { |
|
74 | |||
75 | /** |
||
76 | * @param string $key |
||
77 | */ |
||
78 | 3 | protected function collect($key,Exception $e = null){ |
|
96 | |||
97 | /** |
||
98 | * Gets the ID of the User that is logged in |
||
99 | * @return integer|null The ID of the User or Null if not logged in |
||
100 | */ |
||
101 | 3 | protected function getUserId() { |
|
109 | |||
110 | /** |
||
111 | * Gets the Method of the Request |
||
112 | * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc... |
||
113 | */ |
||
114 | 3 | protected function getMethod() { |
|
122 | |||
123 | /** |
||
124 | * Gets the input data of the Request |
||
125 | * @return array|null The Input data or null |
||
126 | */ |
||
127 | 3 | protected function getData() { |
|
135 | |||
136 | /** |
||
137 | * Gets the URL of the Request |
||
138 | * @return string|null Returns a URL string or null |
||
139 | */ |
||
140 | 3 | protected function getUrl() { |
|
148 | |||
149 | /** |
||
150 | * Gets the status code of the Exception |
||
151 | * @param Exception $e The Exception to check |
||
152 | * @return string|integer The status code value |
||
153 | */ |
||
154 | 3 | protected function getStatusCode(Exception $e) { |
|
161 | } |