1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FlyingLuscas\BugNotifier; |
4
|
|
|
|
5
|
|
|
use FlyingLuscas\BugNotifier\Drivers\DriverInterface; |
6
|
|
|
use FlyingLuscas\BugNotifier\Exceptions\InvalidDriverNameException; |
7
|
|
|
|
8
|
|
|
class BugNotifier |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Fire a notification for the given exception using the configured driver. |
12
|
|
|
* |
13
|
|
|
* @param \Exception $e |
14
|
|
|
* |
15
|
|
|
* @return mixed |
16
|
|
|
*/ |
17
|
|
|
public function exception(\Exception $e) |
18
|
|
|
{ |
19
|
|
|
if (! $this->shouldBeReported($e) || ! $this->shouldRunOnThisEnvironment()) { |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$message = new Message($e); |
24
|
|
|
$driverClass = $this->getNotificationDriver(); |
25
|
|
|
|
26
|
|
|
return $this->sendNotification($message, new $driverClass); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Send notification using the given driver. |
31
|
|
|
* |
32
|
|
|
* @param \FlyingLuscas\BugNotifier\Message $message |
33
|
|
|
* @param \FlyingLuscas\BugNotifier\Drivers\DriverInterface $driver |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
private function sendNotification(Message $message, DriverInterface $driver) |
38
|
|
|
{ |
39
|
|
|
return $driver->handle($message); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the notification driver that should be used. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
private function getNotificationDriver() |
48
|
|
|
{ |
49
|
|
|
$use = config('bugnotifier.driver'); |
50
|
|
|
$driver = config(sprintf('bugnotifier.%s.driver', $use)); |
51
|
|
|
|
52
|
|
|
if (! $driver) { |
53
|
|
|
throw new InvalidDriverNameException(sprintf('Driver "%s" not supported.', $use)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $driver; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Check if the package should run on the current environment. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
private function shouldRunOnThisEnvironment() |
65
|
|
|
{ |
66
|
|
|
return in_array(app()->environment(), config('bugnotifier.environments')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check if a given exception should be reported. |
71
|
|
|
* |
72
|
|
|
* @param \Exception $e |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
private function shouldBeReported(\Exception $e) |
77
|
|
|
{ |
78
|
|
|
return ! in_array(get_class($e), config('bugnotifier.ignore', [])); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|