Completed
Push — fix-37 ( ed2202 )
by Tyler
04:19
created

Recorder::collect()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.7656

Importance

Changes 4
Bugs 3 Features 1
Metric Value
c 4
b 3
f 1
dl 0
loc 19
ccs 12
cts 16
cp 0.75
rs 8.2222
cc 7
eloc 16
nc 7
nop 2
crap 7.7656
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 42
    public function __construct() {
24 42
        $this->config = config('lern.record');
25 42
    }
26
27
    /**
28
     * Records an Exception to the database
29
     * @param  Exception $e The exception you want to record
30
     * @return false|ExceptionModel
31
     * @throws RecorderFailedException
32
     */
33 12
    public function record(Exception $e)
34
    {
35 12
        if ($this->shouldntHandle($e)) {
36 3
            return false;
37
        }
38
39
        $opts = [
40 9
            'class'       => get_class($e),
41 9
            'file'        => $e->getFile(),
42 9
            'line'        => $e->getLine(),
43 9
            'code'        => (is_int($e->getCode()) ? $e->getCode() : 0),
44 9
            'message'     => $e->getMessage(),
45 9
            'trace'       => $e->getTraceAsString(),
46 9
        ];
47
48
49 9
        $configDependant = ['user_id', 'status_code', 'method', 'data', 'url'];
50
51
        try {
52 9
            foreach ($configDependant as $key) {
53 9
                if ($this->canCollect($key)) {
54 6
                    $value = $this->collect($key, $e);
55 3
                    if ($value !== null) {
56 3
                        $opts[$key] = $value;
57 3
                    }
58 3
                }
59 6
            }
60
61 6
            return ExceptionModel::create($opts);
62 3
        } catch (Exception $e) {
63 3
            $code = (is_int($e->getCode()) ? $e->getCode() : 0);
64 3
            throw new RecorderFailedException($e->getMessage(), $code, $e);
65
        }
66
    }
67
68
    /**
69
     * Checks the config to see if you can collect certain information
70
     * @param  string $type the config value you want to check
71
     * @return boolean      
72
     */
73 9
    private function canCollect($type) {
74 9
        if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
75 6
            return $this->config['collect'][$type] === true;
76
        }
77 3
        return false;
78
    }
79
80
    /**
81
     * @param string $key
82
     * @param Exception $e
83
     * @return array|int|null|string
84
     * @throws Exception
85
     */
86 3
    protected function collect($key, Exception $e = null) {
87
        switch ($key) {
88 3
            case 'user_id':
89 3
                return $this->getUserId();
90 3
            case 'method':
91 3
                return $this->getMethod();
92 3
            case 'url':
93 3
                return $this->getUrl();
94 3
            case 'data':
95 3
                return $this->getData();
96 3
            case 'status_code':
97 3
                if ($e === null) {
98
                                    return 0;
99
                }
100 3
                return $this->getStatusCode($e);
101
            default:
102
                throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
103
        }
104
    }
105
106
    /**
107
     * Gets the ID of the User that is logged in
108
     * @return integer|null The ID of the User or Null if not logged in
109
     */
110 3
    protected function getUserId() {
111 3
        $user = Auth::user();
112 3
        if (is_object($user) && !empty($user->id)) {
113
            return $user->id;
114
        } else {
115 3
            return null;
116
        }
117
    }
118
119
    /**
120
     * Gets the Method of the Request
121
     * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
122
     */
123 3
    protected function getMethod() {
124 3
        $method = Request::method();
125 3
        if (!empty($method)) {
126 3
            return $method;
127
        } else {
128
            return null;
129
        }
130
    }
131
132
    /**
133
     * Gets the input data of the Request
134
     * @return array|null The Input data or null
135
     */
136 3
    protected function getData() {
137 3
        $data = Input::all();
138 3
        if (is_array($data)) {
139 3
            $this->excludeKeys($data);
140 3
            return $data;
141
        } else {
142
            return null;
143
        }
144
    }
145
146
    /**
147
     * Gets the URL of the Request
148
     * @return string|null Returns a URL string or null
149
     */
150 3
    protected function getUrl() {
151 3
        $url = Request::url();
152 3
        if (is_string($url)) {
153 3
            return $url;
154
        } else {
155
            return null;
156
        }
157
    }
158
159
    /**
160
     * Gets the status code of the Exception
161
     * @param  Exception $e The Exception to check
162
     * @return string|integer The status code value
163
     */
164 3
    protected function getStatusCode(Exception $e) {
165 3
        if ($e instanceof HttpExceptionInterface) {
166
            return $e->getStatusCode();
167
        } else {
168 3
            return 0;
169
        }
170
    }
171
172
    /**
173
     * This function will remove all keys from an array recursively as defined in the config file
174
     * @param  array $data The array to remove keys from
175
     * @return void
176
     */
177 9
    protected function excludeKeys(array $data) {
178 9
        $keys = isset($this->config['excludeKeys']) ? $this->config['excludeKeys'] : [];
179 9
        foreach ($data as $key => &$value) {
180 6
            if (in_array($key, $keys)) {
181 6
                unset($data[$key]);
182 6
            } else if (is_array($value)) {
183 3
                $value = $this->excludeKeys($value);
184 3
            }
185 9
        }
186
187 9
        return $data;
188
    }
189
}
190