Completed
Push — master ( 9f66c1...bf2f83 )
by Adrian
42s
created

ExceptionMonitor::getDriverInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Adriandmitroca\LaravelExceptionMonitor;
4
5
class ExceptionMonitor
6
{
7
8
    /**
9
     * It calls all enabled drivers and triggers requests to send notifications.
10
     *
11
     * @param \Exception $e
12
     */
13
    public function notifyException(\Exception $e)
14
    {
15
        $driver = config('exception-monitor.drivers');
16
17
        if ($this->enabledEnvironment(app()->environment())) {
18
            if (is_array($driver)) {
19
                foreach ($driver as $instance) {
20
                    $this->sendException($e, $instance);
21
                }
22
            } else {
23
                $this->sendException($e, $driver);
24
            }
25
        }
26
    }
27
28
29
    /**
30
     * It sends notification to given driver.
31
     *
32
     * @param \Exception $e
33
     * @param            $driver
34
     */
35
    protected function sendException(\Exception $e, $driver)
36
    {
37
        $channel = $this->getDriverInstance($driver);
38
        $channel->send($e);
39
    }
40
41
42
    /**
43
     * It injects driver's class to Laravel application.
44
     *
45
     * @param $driver
46
     *
47
     * @return mixed
48
     */
49
    protected function getDriverInstance($driver)
50
    {
51
        $class = '\Adriandmitroca\LaravelExceptionMonitor\Drivers\\' . ucfirst($driver) . 'Driver';
52
53
        return app($class);
54
    }
55
56
57
    /**
58
     * Check if given environment is enabled in configuration file.
59
     *
60
     * @param $environment
61
     *
62
     * @return mixed
63
     */
64
    public function enabledEnvironment($environment)
65
    {
66
        $config = config('exception-monitor.environments');
67
68
        if (is_array($config)) {
69
            return in_array($environment, config('exception-monitor.environments'));
70
        }
71
72
        return $environment === $config;
73
    }
74
75
}
76