Completed
Push — master ( 5c900c...b7df28 )
by Tyler
04:58
created

Recorder::collect()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.3229

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 18
ccs 13
cts 16
cp 0.8125
rs 8.2222
cc 7
eloc 16
nc 7
nop 2
crap 7.3229
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
            $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
            return $data;
132
        } else {
133
            return null;
134
        }
135
    }
136
137
    /**
138
     * Gets the URL of the Request
139
     * @return string|null Returns a URL string or null
140
     */
141 3
    protected function getUrl() {
142 3
        $url = Request::url();
143 3
        if (is_string($url)) {
144 3
            return $url;
145
        } else {
146
            return null;
147
        }
148
    }
149
150
    /**
151
     * Gets the status code of the Exception
152
     * @param  Exception $e The Exception to check
153
     * @return string|integer The status code value
154
     */
155 3
    protected function getStatusCode(Exception $e) {
156 3
        if ($e instanceof HttpExceptionInterface) {
157
            return $e->getStatusCode();
158
        } else {
159 3
            return 0;
160
        }
161
    }
162
}