Completed
Branch dev-record-extra-information (673ac4)
by Tyler
03:54
created

Recorder::getUserId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 4
cts 5
cp 0.8
cc 3
eloc 6
nc 2
nop 0
crap 3.072
1
<?php
2
3
namespace Tylercd100\LERN\Components;
4
5
use Exception;
6
use Tylercd100\LERN\Models\ExceptionModel;
7
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Request;
10
use Illuminate\Support\Facades\Input;
11
12
class Recorder {
13
14
    /**
15
     * @var array
16
     */
17
    protected $config = [];
18
19
    /**
20
     * The constructor
21
     */
22 21
    public function __construct(){
23 21
        $this->config = config('lern.record');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('lern.record') of type * is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24 21
    }
25
26
    /**
27
     * Records an Exception to the database
28
     * @param  Exception $e The exception you want to record
29
     * @return Tylercd100\LERN\Models\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 3
        $opts['status_code'] = $this->getStatusCode($e);
43 3
        $opts['user_id'] = $this->getUserId($e);
0 ignored issues
show
Unused Code introduced by
The call to Recorder::getUserId() has too many arguments starting with $e.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
44 3
        $opts['method'] = $this->getMethod($e);
0 ignored issues
show
Unused Code introduced by
The call to Recorder::getMethod() has too many arguments starting with $e.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45 3
        $opts['data'] = $this->getData($e);
0 ignored issues
show
Unused Code introduced by
The call to Recorder::getData() has too many arguments starting with $e.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46 3
        $opts['url'] = $this->getUrl($e);
0 ignored issues
show
Unused Code introduced by
The call to Recorder::getUrl() has too many arguments starting with $e.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
48 3
        return ExceptionModel::create($opts);
49
    }
50
51
    /**
52
     * Checks the config to see if you can collect certain information
53
     * @param  string $type the config value you want to check
54
     * @return boolean      
55
     */
56 3
    private function canCollect($type){
57 3
        if(!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])){
58
            return $this->config['collect'][$type] === true;
59
        }
60 3
        return false;
61
    }
62
63
    /**
64
     * Gets the ID of the User that is logged in
65
     * @return integer|null The ID of the User or Null if not logged in
66
     */
67 3
    protected function getUserId(){
68 3
        $user = Auth::user();
69 3
        if($this->canCollect('user_id') && is_object($user)) {
70
            return $user->id;
71
        } else {
72 3
            return null;
73
        }
74
    }
75
76
    /**
77
     * Gets the Method of the Request
78
     * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
79
     */
80 3
    protected function getMethod(){
81 3
        $method = Request::method();
82 3
        if($this->canCollect('method') && !empty($method)) {
83
            return $method;
84
        } else {
85 3
            return null;
86
        }
87
    }
88
89
    /**
90
     * Gets the input data of the Request
91
     * @return array|null The Input data or null
92
     */
93 3
    protected function getData(){
94 3
        $data = Input::all();
95 3
        if($this->canCollect('data') && is_array($data)) {
96
            return $data;
97
        } else {
98 3
            return null;
99
        }
100
    }
101
102
    /**
103
     * Gets the URL of the Request
104
     * @return string|null Returns a URL string or null
105
     */
106 3
    protected function getUrl(){
107 3
        $url = Request::url();
108 3
        if($this->canCollect('url') && is_array($url)) {
109
            return $url;
110
        } else {
111 3
            return null;
112
        }
113
    }
114
115
    /**
116
     * Gets the status code of the Exception
117
     * @param  Exception $e The Exception to check
118
     * @return string|integer The status code value
119
     */
120 3
    protected function getStatusCode(Exception $e){
121 3
        if ($e instanceof HttpExceptionInterface) {
122
            return $e->getStatusCode();
123
        } else {
124 3
            return 0;
125
        }
126
    }
127
}