Completed
Pull Request — master (#35)
by Tyler
07:12 queued 04:50
created

Recorder::collect()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8.4275

Importance

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