Completed
Branch dev-record-extra-information (c02ed6)
by Tyler
03:04
created

Recorder::getMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2.032
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 3
                $opts[$key] = $this->collect($key,$e);
48 3
            }
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 3
            return $this->config['collect'][$type] === true;
62
        }
63
        return false;
64
    }
65
66 3
    protected function collect($key,Exception $e = null){
67
        switch ($key) {
68 3
            case 'user_id':
69 3
                return $this->getUserId();
70 3
            case 'method':
71 3
                return $this->getMethod();
72 3
            case 'status_code':
73 3
                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...
74 3
            case 'url':
75 3
                return $this->getUrl();
76 3
            case 'data':
77 3
                return $this->getData();
78
            default:
79
                throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
80
        }
81
    }
82
83
    /**
84
     * Gets the ID of the User that is logged in
85
     * @return integer|null The ID of the User or Null if not logged in
86
     */
87 3
    protected function getUserId() {
88 3
        $user = Auth::user();
89 3
        if (is_object($user)) {
90
            return $user->id;
91
        } else {
92 3
            return null;
93
        }
94
    }
95
96
    /**
97
     * Gets the Method of the Request
98
     * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
99
     */
100 3
    protected function getMethod() {
101 3
        $method = Request::method();
102 3
        if (!empty($method)) {
103 3
            return $method;
104
        } else {
105
            return null;
106
        }
107
    }
108
109
    /**
110
     * Gets the input data of the Request
111
     * @return array|null The Input data or null
112
     */
113 3
    protected function getData() {
114 3
        $data = Input::all();
115 3
        if (is_array($data)) {
116 3
            return $data;
117
        } else {
118
            return null;
119
        }
120
    }
121
122
    /**
123
     * Gets the URL of the Request
124
     * @return string|null Returns a URL string or null
125
     */
126 3
    protected function getUrl() {
127 3
        $url = Request::url();
128 3
        if (is_string($url)) {
129 3
            return $url;
130
        } else {
131
            return null;
132
        }
133
    }
134
135
    /**
136
     * Gets the status code of the Exception
137
     * @param  Exception $e The Exception to check
138
     * @return string|integer The status code value
139
     */
140 3
    protected function getStatusCode(Exception $e) {
141 3
        if ($e instanceof HttpExceptionInterface) {
142
            return $e->getStatusCode();
143
        } else {
144 3
            return 0;
145
        }
146
    }
147
}