ExceptionNotificationHandlerTrait::sendEmail()   A
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 5
nop 1
1
<?php
2
3
namespace App\Traits;
4
5
use App\Mail\ExceptionOccured;
6
use Illuminate\Support\Facades\Log;
7
use Mail;
8
use Symfony\Component\Debug\Exception\FlattenException;
9
use Symfony\Component\ErrorHandler\ErrorHandler as SymfonyExceptionHandler;
10
use Throwable;
11
12
trait ExceptionNotificationHandlerTrait
13
{
14
    /**
15
     * A list of the exception types that should not be reported.
16
     *
17
     * @var array
18
     */
19
    protected $dontReport = [
20
        \Illuminate\Auth\AuthenticationException::class,
21
        \Illuminate\Auth\Access\AuthorizationException::class,
22
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
23
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
24
        \Illuminate\Session\TokenMismatchException::class,
25
        \Illuminate\Validation\ValidationException::class,
26
    ];
27
28
    /**
29
     * Report or log an exception.
30
     *
31
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
32
     *
33
     * @param \Throwable $exception
34
     *
35
     * @return void
36
     */
37
    public function report(Throwable $exception)
38
    {
39
        $enableEmailExceptions = config('exceptions.emailExceptionEnabled');
40
41
        if ($enableEmailExceptions === '') {
42
            $enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault');
43
        }
44
45
        if ($enableEmailExceptions) {
46
            if ($this->shouldReport($exception)) {
0 ignored issues
show
Bug introduced by
It seems like shouldReport() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
            if ($this->/** @scrutinizer ignore-call */ shouldReport($exception)) {
Loading history...
47
                $this->sendEmail($exception);
48
            }
49
        }
50
51
        parent::report($exception);
52
    }
53
54
    /**
55
     * Sends an email upon exception.
56
     *
57
     * @param \Throwable $exception
58
     *
59
     * @return void
60
     */
61
    public function sendEmail(Throwable $exception)
62
    {
63
        try {
64
            $e = FlattenException::create($exception);
65
            $handler = new SymfonyExceptionHandler();
66
            $html = $handler->getHtml($e);
0 ignored issues
show
Bug introduced by
The method getHtml() does not exist on Symfony\Component\ErrorHandler\ErrorHandler. ( Ignorable by Annotation )

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

66
            /** @scrutinizer ignore-call */ 
67
            $html = $handler->getHtml($e);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
            Mail::send(new ExceptionOccured($html));
69
        } catch (Throwable $exception) {
70
            Log::error($exception);
71
        }
72
    }
73
}
74