| 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 | 39 |  |     public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 39 |  |         $this->config = config('lern.record'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 39 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 9 |  |         if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 6 |  |             return $this->config['collect'][$type] === true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |     protected function collect($key,Exception $e = null){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         switch ($key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |             case 'user_id': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |                 return $this->getUserId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |             case 'method': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |                 return $this->getMethod(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |             case 'url': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |                 return $this->getUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 3 |  |             case 'data': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 3 |  |                 return $this->getData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |             case 'status_code': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |                 if($e===null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |                     return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |                 return $this->getStatusCode($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 throw new Exception("{$key} is not supported! Therefore it cannot be collected!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |         $user = Auth::user(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 3 |  |         if (is_object($user)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             return $user->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 3 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  |         $method = Request::method(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 3 |  |         if (!empty($method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 3 |  |             return $method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 3 |  |         if ($e instanceof HttpExceptionInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             return $e->getStatusCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 3 |  |             return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 9 |  |         $keys = isset($this->config['excludeKeys']) ? $this->config['excludeKeys'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 9 |  |         foreach ($data as $key => &$value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 6 |  |             if(in_array($key,$keys)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 6 |  |                 unset($data[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 6 |  |             } else if(is_array($value)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 3 |  |                 $value = $this->excludeKeys($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 3 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 9 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 9 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  |