Completed
Pull Request — master (#12)
by Tyler
07:15
created

Recorder::record()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 6
Bugs 3 Features 2
Metric Value
c 6
b 3
f 2
dl 0
loc 30
ccs 19
cts 19
cp 1
rs 8.439
cc 5
eloc 18
nc 9
nop 1
crap 5
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 33
    public function __construct() {
24 33
        $this->config = config('lern.record');
25 33
    }
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) {
69 9
        if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
70 6
            return $this->config['collect'][$type] === true;
71
        }
72 3
        return false;
73
    }
74
75
    /**
76
     * @param string $key
77
     */
78 3
    protected function collect($key,Exception $e = null){
79
        switch ($key) {
80 3
            case 'user_id':
81 3
                return $this->getUserId();
82 3
            case 'method':
83 3
                return $this->getMethod();
84 3
            case 'url':
85 3
                return $this->getUrl();
86 3
            case 'data':
87 3
                return $this->getData();
88 3
            case 'status_code':
89 3
                if($e===null)
90 3
                    return 0;
91 3
                return $this->getStatusCode($e);
92
            default:
93
                throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
94
        }
95
    }
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() {
102 3
        $user = Auth::user();
103 3
        if (is_object($user)) {
104
            return $user->id;
105
        } else {
106 3
            return null;
107
        }
108
    }
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() {
115 3
        $method = Request::method();
116 3
        if (!empty($method)) {
117 3
            return $method;
118
        } else {
119
            return null;
120
        }
121
    }
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() {
128 3
        $data = Input::all();
129 3
        if (is_array($data)) {
130 3
            return $data;
131
        } else {
132
            return null;
133
        }
134
    }
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() {
141 3
        $url = Request::url();
142 3
        if (is_string($url)) {
143 3
            return $url;
144
        } else {
145
            return null;
146
        }
147
    }
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) {
155 3
        if ($e instanceof HttpExceptionInterface) {
156
            return $e->getStatusCode();
157
        } else {
158 3
            return 0;
159
        }
160
    }
161
}