Issues (84)

src/Exceptions/LogExceptions.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace Someshwer\Firewall\src\Exceptions;
4
5
use Illuminate\Database\QueryException;
0 ignored issues
show
The type Illuminate\Database\QueryException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Someshwer\Firewall\src\Entities\ExceptionLog;
7
use Someshwer\Firewall\src\Events\NotifyException;
8
9
/**
10
 * Class LogExceptions.
11
 *
12
 * @author Someshwer
13
 * Date: 16-09-2018
14
 */
15
class LogExceptions
16
{
17
    /**
18
     * @var
19
     */
20
    private $request;
21
22
    /**
23
     * @var
24
     */
25
    private $exception;
26
27
    /**
28
     * LogExceptions constructor.
29
     *
30
     * @param $req
31
     * @param $e
32
     */
33
    public function __construct($req, $e)
34
    {
35
        $this->request = $req;
36
        $this->exception = $e;
37
        $this->logException($req, $e);
38
        $this->notifyExceptionViaEmail($e);
39
    }
40
41
    /**
42
     * @param $request
43
     * @param $exception
44
     *
45
     * Logs the exception
46
     */
47
    public function logException($request, $exception)
48
    {
49
        if (config('firewall.log_exceptions')) {
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        if (/** @scrutinizer ignore-call */ config('firewall.log_exceptions')) {
Loading history...
50
            $exception_log = new ExceptionLog();
51
            if (($exception instanceof QueryException) && ($exception->getCode() != 2002)) {
52
                $exception_log = $this->prepareRequestData($exception_log, $request);
53
                $this->prepareExceptionData($exception_log, $exception);
54
            }
55
            // Stop sending exception email from here
56
            // $this->notifyExceptionViaEmail($exception);
57
        }
58
    }
59
60
    /**
61
     * @param $exception_log
62
     * @param $request
63
     *
64
     * @return mixed
65
     *
66
     * Prepares request data to be stored
67
     */
68
    private function prepareRequestData($exception_log, $request)
69
    {
70
        $exception_log->fill([
71
            'path'             => $request->path(),
72
            'url'              => $request->url(),
73
            'full_url'         => $request->fullUrl(),
74
            'method'           => $_SERVER['REQUEST_METHOD'],
75
            'uri'              => $_SERVER['REQUEST_URI'],
76
            'query'            => $request->query() ? $request->query() : null,
77
            'file_name'        => $_SERVER['SCRIPT_FILENAME'],
78
            'http_host'        => $_SERVER['HTTP_HOST'],
79
            'http_user_agent'  => $_SERVER['HTTP_USER_AGENT'],
80
            'ip_address'       => $request->ip(),
81
            'all_request_data' => $_SERVER,
82
        ]);
83
        $exception_log->save();
84
85
        return $exception_log;
86
    }
87
88
    /**
89
     * @param $exception_log
90
     * @param $exception
91
     *
92
     * @return mixed
93
     *
94
     * Prepares exception data to be stored
95
     */
96
    private function prepareExceptionData($exception_log, $exception)
97
    {
98
        $exception_data = [
99
            // 'original_class' => $exception->getOriginalClassName(),
100
            'message'     => $exception->getMessage(),
101
            'error_code'  => $exception->getCode(),
102
            'file_name'   => $exception->getFile(),
103
            'line_number' => $exception->getLine(),
104
            // 'severity' => $exception->getSeverity(),
105
            // 'trace' => $exception->getTrace(),
106
            'trace_string' => $exception->getTraceAsString(),
107
            'previous'     => $exception->getPrevious(),
108
        ];
109
        $exception_log->fill([
110
            'exception_data' => $exception_data,
111
        ]);
112
        $exception_log->save();
113
114
        return $exception_log;
115
    }
116
117
    /**
118
     * @param $exception
119
     *
120
     * Sends an email with exception information when ever an exception raised by the application.
121
     */
122
    private function notifyExceptionViaEmail($exception)
123
    {
124
        if (config('firewall.notify_exceptions.via_email')) {
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        if (/** @scrutinizer ignore-call */ config('firewall.notify_exceptions.via_email')) {
Loading history...
125
            $data = $exception->getTraceAsString();
126
127
            // Firing event to send exception notification
128
            event(new NotifyException($data));
0 ignored issues
show
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
            /** @scrutinizer ignore-call */ 
129
            event(new NotifyException($data));
Loading history...
129
        }
130
    }
131
}
132