Completed
Push — fix/56 ( d15d50...729cbd )
by Tyler
26:35 queued 21:43
created

Recorder::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
cc 2
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 Illuminate\Support\Facades\Cache;
10
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
11
use Tylercd100\LERN\Exceptions\RecorderFailedException;
12
use Tylercd100\LERN\Models\ExceptionModel;
13
use Carbon\Carbon;
14
15
class Recorder extends Component {
16
17
    /**
18
     * @var mixed
19
     */
20
    protected $config = [];
21
22
    /**
23
     * @var array
24
     */
25
    protected $absolutelyDontHandle = [
26
        \Tylercd100\LERN\Exceptions\RecorderFailedException::class,
27
    ];
28
29
    /**
30
     * The constructor
31
     */
32 36
    public function __construct() {
33 36
        $this->config = config('lern.record');
34 36
    }
35
36
    /**
37
     * Records an Exception to the database
38
     * @param  Exception $e The exception you want to record
39
     * @return false|ExceptionModel
40
     * @throws RecorderFailedException
41
     */
42 6
    public function record(Exception $e)
43
    {
44 6
        if ($this->shouldntHandle($e)) {
45
            return false;
46
        }
47
48
        $opts = [
49 6
            'class'       => get_class($e),
50 6
            'file'        => $e->getFile(),
51 6
            'line'        => $e->getLine(),
52 6
            'code'        => (is_int($e->getCode()) ? $e->getCode() : 0),
53 6
            'message'     => $e->getMessage(),
54 6
            'trace'       => $e->getTraceAsString(),
55
        ];
56
57 6
        $configDependant = array_keys($this->config['collect']);
58
59
        try {
60 6
            foreach ($configDependant as $key) {
61 6
                if ($this->canCollect($key)) {
62 6
                    $value = $this->collect($key, $e);
63 6
                    if ($value !== null) {
64 6
                        $opts[$key] = $value;
65
                    }
66
                }
67
            }
68
69 6
            $class = config('lern.recorder.model');
70 6
            $class = !empty($class) ? $class : ExceptionModel::class;
71
72 6
            $model = new $class();
73 6
            foreach($opts as $key => $value) {
74 6
                $model->{$key} = $value;
75
            }
76
77 6
            $model->save();
78
79 3
            Cache::forever($this->getCacheKey($e), Carbon::now());
80
81 3
            return $model;
82 3
        } catch (Exception $e) {
83 3
            $code = (is_int($e->getCode()) ? $e->getCode() : 0);
84 3
            throw new RecorderFailedException($e->getMessage(), $code, $e);
85
        }
86
    }
87
88
    /**
89
     * Checks the config to see if you can collect certain information
90
     * @param  string $type the config value you want to check
91
     * @return boolean      
92
     */
93 6
    private function canCollect($type) {
94 6
        if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
95 6
            return $this->config['collect'][$type] === true;
96
        }
97
        return false;
98
    }
99
100
    /**
101
     * @param string $key
102
     * @param Exception $e
103
     * @return array|int|null|string
104
     * @throws Exception
105
     */
106 6
    protected function collect($key, Exception $e = null) {
107
        switch ($key) {
108 6
            case 'user_id':
109 6
                return $this->getUserId();
110 6
            case 'method':
111 6
                return $this->getMethod();
112 6
            case 'url':
113 6
                return $this->getUrl();
114 6
            case 'data':
115 6
                return $this->getData();
116 6
            case 'ip':
117
                return $this->getIp();
118 6
            case 'status_code':
119 6
                if ($e === null) {
120
                    return 0;
121
                }
122 6
                return $this->getStatusCode($e);
123
            default:
124
                throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
125
        }
126
    }
127
128
    /**
129
     * Gets the ID of the User that is logged in
130
     * @return integer|null The ID of the User or Null if not logged in
131
     */
132 6
    protected function getUserId() {
133 6
        $user = Auth::user();
134 6
        if (is_object($user) && !empty($user->id)) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
135
            return $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
136
        } else {
137 6
            return null;
138
        }
139
    }
140
141
    /**
142
     * Gets the Method of the Request
143
     * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
144
     */
145 6
    protected function getMethod() {
146 6
        $method = Request::method();
147 6
        if (!empty($method)) {
148 6
            return $method;
149
        } else {
150
            return null;
151
        }
152
    }
153
154
    /**
155
     * Gets the input data of the Request
156
     * @return array|null The Input data or null
157
     */
158 6
    protected function getData() {
159 6
        $data = Input::all();
160 6
        if (is_array($data)) {
161 6
            return $this->excludeKeys($data);
162
        } else {
163
            return null;
164
        }
165
    }
166
167
    /**
168
     * Gets the URL of the Request
169
     * @return string|null Returns a URL string or null
170
     */
171 6
    protected function getUrl() {
172 6
        $url = Request::url();
173 6
        if (is_string($url)) {
174 6
            return $url;
175
        } else {
176
            return null;
177
        }
178
    }
179
180
    /**
181
     * Returns the IP from the request
182
     *
183
     * @return string
184
     */
185
    protected function getIp() {
186
        return Request::ip();
187
    }
188
189
    /**
190
     * Gets the status code of the Exception
191
     * @param  Exception $e The Exception to check
192
     * @return string|integer The status code value
193
     */
194 6
    protected function getStatusCode(Exception $e) {
195 6
        if ($e instanceof HttpExceptionInterface) {
196
            return $e->getStatusCode();
197
        } else {
198 6
            return 0;
199
        }
200
    }
201
202
    /**
203
     * This function will remove all keys from an array recursively as defined in the config file
204
     * @param  array $data The array to remove keys from
205
     * @return void
206
     */
207 6
    protected function excludeKeys(array $data) {
208 6
        $keys = isset($this->config['excludeKeys']) ? $this->config['excludeKeys'] : [];
209 6
        foreach ($data as $key => &$value) {
210
            if (in_array($key, $keys)) {
211
                unset($data[$key]);
212
            } else if (is_array($value)) {
213
                $value = $this->excludeKeys($value);
214
            }
215
        }
216
217 6
        return $data;
218
    }
219
}
220