Completed
Pull Request — master (#35)
by Tyler
07:25 queued 05:05
created

Recorder::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 4
Bugs 2 Features 2
Metric Value
c 4
b 2
f 2
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
cc 2
eloc 7
nc 2
nop 0
crap 2.0185
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\Exceptions\RecorderFailedException;
11
use Tylercd100\LERN\Models\ExceptionModel;
12
13
class Recorder extends Component {
14
15
    /**
16
     * @var mixed
17
     */
18
    protected $config = [];
19
20
    /**
21
     * The constructor
22
     */
23 42
    public function __construct() {
24 42
        $this->config = config('lern.record');
25 42
    }
26
27
    /**
28
     * Records an Exception to the database
29
     * @param  Exception $e The exception you want to record
30
     * @return false|ExceptionModel
31
     * @throws RecorderFailedException
32 4
     */
33 8
    public function record(Exception $e)
34 4
    {
35 9
        if ($this->shouldntHandle($e)) {
36 2
            return false;
37
        }
38
39 3
        $opts = [
40 9
            'class'       => get_class($e),
41 9
            'file'        => $e->getFile(),
42 9
            'line'        => $e->getLine(),
43 9
            'code'        => (is_int($e->getCode()) ? $e->getCode() : 0),
44 9
            'message'     => $e->getMessage(),
45 9
            'trace'       => $e->getTraceAsString(),
46 6
        ];
47
48 3
49 6
        $configDependant = ['user_id', 'status_code', 'method', 'data', 'url'];
50
51 3
        try {
52 9
            foreach ($configDependant as $key) {
53 8
                if ($this->canCollect($key)) {
54 5
                    $opts[$key] = $this->collect($key, $e);
55 4
                }
56 4
            }
57 2
58 5
            return ExceptionModel::create($opts);
59 3
        } catch (Exception $e) {
60 3
            $code = (is_int($e->getCode()) ? $e->getCode() : 0);
61 2
            throw new RecorderFailedException($e->getMessage(), $code, $e);
62
        }
63
    }
64
65
    /**
66
     * Checks the config to see if you can collect certain information
67
     * @param  string $type the config value you want to check
68
     * @return boolean      
69 3
     */
70 9
    private function canCollect($type) {
71 8
        if (!empty($this->config) && !empty($this->config['collect']) && !empty($this->config['collect'][$type])) {
72 4
            return $this->config['collect'][$type] === true;
73 1
        }
74 2
        return false;
75
    }
76
77
    /**
78
     * @param string $key
79 1
     * @param Exception $e
80
     * @return array|int|null|string
81 1
     * @throws Exception
82 1
     */
83 3
    protected function collect($key, Exception $e = null) {
84 1
        switch ($key) {
85 3
            case 'user_id':
86 3
                return $this->getUserId();
87 3
            case 'method':
88 3
                return $this->getMethod();
89 3
            case 'url':
90 3
                return $this->getUrl();
91 2
            case 'data':
92 2
                return $this->getData();
93 3
            case 'status_code':
94 2
                if ($e === null) {
95
                                    return 0;
96
                }
97 2
                return $this->getStatusCode($e);
98
            default:
99
                throw new Exception("{$key} is not supported! Therefore it cannot be collected!");
100
        }
101
    }
102
103 1
    /**
104 1
     * Gets the ID of the User that is logged in
105 1
     * @return integer|null The ID of the User or Null if not logged in
106
     */
107 2
    protected function getUserId() {
108 3
        $user = Auth::user();
109 2
        if (is_object($user)) {
110
            return $user->id;
111
        } else {
112 2
            return null;
113
        }
114
    }
115
116 1
    /**
117 1
     * Gets the Method of the Request
118 1
     * @return string|null Possible values are null or GET, POST, DELETE, PUT, etc...
119 1
     */
120 2
    protected function getMethod() {
121 2
        $method = Request::method();
122 2
        if (!empty($method)) {
123 2
            return $method;
124
        } else {
125
            return null;
126
        }
127
    }
128
129 1
    /**
130 1
     * Gets the input data of the Request
131 1
     * @return array|null The Input data or null
132 1
     */
133 3
    protected function getData() {
134 2
        $data = Input::all();
135 2
        if (is_array($data)) {
136 2
            $this->excludeKeys($data);
137 2
            return $data;
138
        } else {
139
            return null;
140
        }
141
    }
142
143 1
    /**
144 1
     * Gets the URL of the Request
145 1
     * @return string|null Returns a URL string or null
146 1
     */
147 2
    protected function getUrl() {
148 2
        $url = Request::url();
149 2
        if (is_string($url)) {
150 2
            return $url;
151
        } else {
152
            return null;
153
        }
154
    }
155
156
    /**
157 1
     * Gets the status code of the Exception
158 1
     * @param  Exception $e The Exception to check
159
     * @return string|integer The status code value
160
     */
161 3
    protected function getStatusCode(Exception $e) {
162 2
        if ($e instanceof HttpExceptionInterface) {
163
            return $e->getStatusCode();
164
        } else {
165 2
            return 0;
166
        }
167
    }
168
169
    /**
170 3
     * This function will remove all keys from an array recursively as defined in the config file
171 3
     * @param  array $data The array to remove keys from
172 3
     * @return void
173 2
     */
174 8
    protected function excludeKeys(array $data) {
175 8
        $keys = isset($this->config['excludeKeys']) ? $this->config['excludeKeys'] : [];
176 7
        foreach ($data as $key => &$value) {
177 5
            if (in_array($key, $keys)) {
178 7
                unset($data[$key]);
179 4
            } else if (is_array($value)) {
180 5
                $value = $this->excludeKeys($value);
181 2
            }
182 6
        }
183
184 6
        return $data;
185
    }
186
}
187