Completed
Pull Request — master (#10)
by Tyler
02:17
created

Recorder   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85%

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 24
c 6
b 3
f 1
lcom 1
cbo 1
dl 0
loc 139
ccs 51
cts 60
cp 0.85
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A canCollect() 0 6 4
A record() 0 22 3
B collect() 0 16 6
A getUserId() 0 8 2
A getMethod() 0 8 2
A getData() 0 8 2
A getUrl() 0 8 2
A getStatusCode() 0 7 2
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
    /**
67
     * @param string $key
68
     */
69 3
    protected function collect($key,Exception $e = null){
70
        switch ($key) {
71 3
            case 'user_id':
72 3
                return $this->getUserId();
73 3
            case 'method':
74 3
                return $this->getMethod();
75 3
            case 'status_code':
76 3
                return $this->getStatusCode($e);
0 ignored issues
show
Bug introduced by
It seems like $e defined by parameter $e on line 69 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...
77 3
            case 'url':
78 3
                return $this->getUrl();
79 3
            case 'data':
80 3
                return $this->getData();
81
            default:
82
                throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
83
        }
84
    }
85
86
    /**
87
     * Gets the ID of the User that is logged in
88
     * @return integer|null The ID of the User or Null if not logged in
89
     */
90 3
    protected function getUserId() {
91 3
        $user = Auth::user();
92 3
        if (is_object($user)) {
93
            return $user->id;
94
        } else {
95 3
            return null;
96
        }
97
    }
98
99
    /**
100
     * Gets the Method of the Request
101
     * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
102
     */
103 3
    protected function getMethod() {
104 3
        $method = Request::method();
105 3
        if (!empty($method)) {
106 3
            return $method;
107
        } else {
108
            return null;
109
        }
110
    }
111
112
    /**
113
     * Gets the input data of the Request
114
     * @return array|null The Input data or null
115
     */
116 3
    protected function getData() {
117 3
        $data = Input::all();
118 3
        if (is_array($data)) {
119 3
            return $data;
120
        } else {
121
            return null;
122
        }
123
    }
124
125
    /**
126
     * Gets the URL of the Request
127
     * @return string|null Returns a URL string or null
128
     */
129 3
    protected function getUrl() {
130 3
        $url = Request::url();
131 3
        if (is_string($url)) {
132 3
            return $url;
133
        } else {
134
            return null;
135
        }
136
    }
137
138
    /**
139
     * Gets the status code of the Exception
140
     * @param  Exception $e The Exception to check
141
     * @return string|integer The status code value
142
     */
143 3
    protected function getStatusCode(Exception $e) {
144 3
        if ($e instanceof HttpExceptionInterface) {
145
            return $e->getStatusCode();
146
        } else {
147 3
            return 0;
148
        }
149
    }
150
}