Completed
Pull Request — master (#12)
by Tyler
03:55
created

Recorder::record()   B

Complexity

Conditions 5
Paths 13

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.0984

Importance

Changes 5
Bugs 2 Features 2
Metric Value
c 5
b 2
f 2
dl 0
loc 30
ccs 16
cts 19
cp 0.8421
rs 8.439
cc 5
eloc 18
nc 13
nop 1
crap 5.0984
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 21
    public function __construct() {
24 21
        $this->config = config('lern.record');
25 21
    }
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 3
    public function record(Exception $e)
1 ignored issue
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        try {
35 3
            if($this->shouldntHandle($e)){
36
                return false;
37
            }
38
39
            $opts = [
40 3
                'class'       => get_class($e),
41 3
                'file'        => $e->getFile(),
42 3
                'line'        => $e->getLine(),
43 3
                'code'        => $e->getCode(),
44 3
                'message'     => $e->getMessage(),
45 3
                'trace'       => $e->getTraceAsString(),
46 3
            ];
47
48
49 3
            $configDependant = ['user_id', 'status_code', 'method', 'data', 'url'];
50
51 3
            foreach ($configDependant as $key) {
52 3
                if ($this->canCollect($key)) {
53 3
                    $opts[$key] = $this->collect($key, $e);
54 3
                }
55 3
            }
56
57 3
            return ExceptionModel::create($opts);
58
        } catch (Exception $e) {
59
            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 3
    private function canCollect($type) {
69 3
        if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
70 3
            return $this->config['collect'][$type] === true;
71
        }
72
        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 'status_code':
85 3
                return $this->getStatusCode($e);
0 ignored issues
show
Bug introduced by
It seems like $e defined by parameter $e on line 78 can be null; however, Tylercd100\LERN\Componen...corder::getStatusCode() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
86 3
            case 'url':
87 3
                return $this->getUrl();
88 3
            case 'data':
89 3
                return $this->getData();
90
            default:
91
                throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
92
        }
93
    }
94
95
    /**
96
     * Gets the ID of the User that is logged in
97
     * @return integer|null The ID of the User or Null if not logged in
98
     */
99 3
    protected function getUserId() {
100 3
        $user = Auth::user();
101 3
        if (is_object($user)) {
102
            return $user->id;
103
        } else {
104 3
            return null;
105
        }
106
    }
107
108
    /**
109
     * Gets the Method of the Request
110
     * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
111
     */
112 3
    protected function getMethod() {
113 3
        $method = Request::method();
114 3
        if (!empty($method)) {
115 3
            return $method;
116
        } else {
117
            return null;
118
        }
119
    }
120
121
    /**
122
     * Gets the input data of the Request
123
     * @return array|null The Input data or null
124
     */
125 3
    protected function getData() {
126 3
        $data = Input::all();
127 3
        if (is_array($data)) {
128 3
            return $data;
129
        } else {
130
            return null;
131
        }
132
    }
133
134
    /**
135
     * Gets the URL of the Request
136
     * @return string|null Returns a URL string or null
137
     */
138 3
    protected function getUrl() {
139 3
        $url = Request::url();
140 3
        if (is_string($url)) {
141 3
            return $url;
142
        } else {
143
            return null;
144
        }
145
    }
146
147
    /**
148
     * Gets the status code of the Exception
149
     * @param  Exception $e The Exception to check
150
     * @return string|integer The status code value
151
     */
152 3
    protected function getStatusCode(Exception $e) {
153 3
        if ($e instanceof HttpExceptionInterface) {
154
            return $e->getStatusCode();
155
        } else {
156 3
            return 0;
157
        }
158
    }
159
}