1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FlyingLuscas\BugNotifier; |
4
|
|
|
|
5
|
|
|
use FlyingLuscas\BugNotifier\Message; |
6
|
|
|
use FlyingLuscas\BugNotifier\Drivers\MailDriver; |
7
|
|
|
use FlyingLuscas\BugNotifier\Drivers\DriverContract; |
8
|
|
|
use FlyingLuscas\BugNotifier\Exceptions\InvalidDriverNameException; |
9
|
|
|
|
10
|
|
|
class BugNotifier |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Notification drivers. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $drivers = [ |
18
|
|
|
'mail' => MailDriver::class, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Fire a notification for the given exception. |
23
|
|
|
* |
24
|
|
|
* @param \Exception $e |
25
|
|
|
* |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
public function exception(\Exception $e) |
29
|
|
|
{ |
30
|
|
|
if (! $this->shouldBeReported($e) || ! $this->shouldRunOnThisEnvironment()) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$message = new Message($e); |
35
|
|
|
$driverClass = $this->getNotificationDriver(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
return $this->sendNotification($message, new $driver); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Send notificaiton using the given driver. |
42
|
|
|
* |
43
|
|
|
* @param \FlyingLuscas\BugNotifier\Message $message |
44
|
|
|
* @param \FlyingLuscas\BugNotifier\Drivers\DriverContract $driver |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
private function sendNotification(Message $message, DriverContract $driver) |
49
|
|
|
{ |
50
|
|
|
return $driver->handle($message); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the notification driver that should be used. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
private function getNotificationDriver() |
59
|
|
|
{ |
60
|
|
|
$use = config('bugnotifier.driver'); |
61
|
|
|
|
62
|
|
|
if (! array_key_exists($use, $this->drivers)) { |
63
|
|
|
throw new InvalidDriverNameException(sprintf('Driver "%s" not supported.', $use)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->drivers[$use]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check if the package should run on the current environment. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
private function shouldRunOnThisEnvironment() |
75
|
|
|
{ |
76
|
|
|
return in_array(app()->environment(), config('bugnotifier.environments')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check if a given exception should be reported. |
81
|
|
|
* |
82
|
|
|
* @param \Exception $e |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
private function shouldBeReported(\Exception $e) |
87
|
|
|
{ |
88
|
|
|
return ! in_array(get_class($e), config('bugnotifier.ignore', [])); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.