Completed
Push — master ( 968afa...1b75b2 )
by Lucas Pires
02:01
created

BugNotifier   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 81
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A exception() 0 11 3
A sendNotification() 0 4 1
A getNotificationDriver() 0 10 2
A shouldRunOnThisEnvironment() 0 4 1
A shouldBeReported() 0 4 1
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();
0 ignored issues
show
Unused Code introduced by
$driverClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
36
37
        return $this->sendNotification($message, new $driver);
0 ignored issues
show
Bug introduced by
The variable $driver does not exist. Did you mean $driverClass?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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