Completed
Branch dev-record-extra-information (46ca32)
by Tyler
03:17
created

Recorder::collect()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 8.439
cc 6
eloc 20
nc 6
nop 2
crap 42
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\Models\ExceptionModel;
11
12
class Recorder {
13
14
    /**
15
     * @var mixed
16
     */
17
    protected $config = [];
18
19
    /**
20
     * The constructor
21
     */
22 21
    public function __construct() {
23 21
        $this->config = config('lern.record');
24 21
    }
25
26
    /**
27
     * Records an Exception to the database
28
     * @param  Exception $e The exception you want to record
29
     * @return ExceptionModel
30
     */
31 3
    public function record(Exception $e)
32
    {
33
        $opts = [
34 3
            'class'       => get_class($e),
35 3
            'file'        => $e->getFile(),
36 3
            'line'        => $e->getLine(),
37 3
            'code'        => $e->getCode(),
38 3
            'message'     => $e->getMessage(),
39 3
            'trace'       => $e->getTraceAsString(),
40 3
        ];
41
42
43 3
        $configDependant = ['user_id','status_code','method','data','url'];
44
45 3
        foreach ($configDependant as $key) {
46 3
            if($this->canCollect($key)){
47
                $opts[$key] = $this->collect($key,$e);
48
            }
49 3
        }
50
51 3
        return ExceptionModel::create($opts);
52
    }
53
54
    /**
55
     * Checks the config to see if you can collect certain information
56
     * @param  string $type the config value you want to check
57
     * @return boolean      
58
     */
59 3
    private function canCollect($type) {
60 3
        if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
61
            return $this->config['collect'][$type] === true;
62
        }
63 3
        return false;
64
    }
65
66
    protected function collect($key,Exception $e = null){
67
        switch ($key) {
68
            case 'user_id':
69
                return $this->getUserId();
70
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
71
72
            case 'method':
73
                return $this->getMethod();
74
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
75
76
            case 'status_code':
77
                return $this->getStatusCode($e);
0 ignored issues
show
Bug introduced by
It seems like $e defined by parameter $e on line 66 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...
78
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
79
80
            case 'url':
81
                return $this->getUrl();
82
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
83
84
            case 'data':
85
                return $this->getData();
86
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
87
            
88
            default:
89
                throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
90
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

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