Completed
Push — master ( 053e43...1e777d )
by Tyler
04:16
created

Notifier::pushHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tylercd100\LERN\Notifications;
4
5
use Exception;
6
use Monolog\Handler\HandlerInterface;
7
use Monolog\Logger;
8
use Tylercd100\LERN\Notifications\MonologHandlerFactory;
9
10
class Notifier {
11
    protected $config;
12
    protected $log;
13
    protected $messageCb;
14
    protected $subjectCb;
15
16
    /**
17
     * You can provide a Monolog Logger instance to use in the constructor 
18
     * @param Logger|null $log Logger instance to use
19
     */
20 36
    public function __construct(Logger $log = null) {
21 36
        if ($log === null) {
22 36
            $log = new Logger(config('lern.notify.channel'));
23 36
        }
24
25 36
        $this->log = $log;
26 36
        $this->config = config('lern.notify');
27 36
    }
28
29
    /**
30
     * Transforms a value into a closure that returns itself when called
31
     * @param  callable|string $cb The value that you want to wrap in a closure
32
     * @return callable
33
     */
34 9
    private function wrapValueInClosure($cb){
35 9
        if (is_callable($cb)) {
36 6
            return $cb;
37
        } else {
38
            return function() use ($cb) { return $cb; };
39
        }
40
    }
41
42
    /**
43
     * Set a string or a closure to be called that will generate the message body for the notification
44
     * @param callable|string $cb A closure or string that will be set for the message
45
     */
46 6
    public function setMessage($cb)
47
    {
48 6
        $this->messageCb = $this->wrapValueInClosure($cb);
49 6
        return $this;
50
    }
51
52
    /**
53
     * Returns the result of the message closure
54
     * @param  Exception $e The Exception instance that you want to build the message around
55
     * @return string       The message string
56
     */
57 9
    public function getMessage(Exception $e) {
58 9
        if (is_callable($this->messageCb)) {
59 6
            return $this->messageCb->__invoke($e);
1 ignored issue
show
Bug introduced by
The method __invoke cannot be called on $this->messageCb (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
60
        } else {
61 3
            $msg = get_class($e) . " was thrown! \n" . $e->getMessage();
62 3
            if ($this->config['includeExceptionStackTrace'] === true) {
63 3
                $msg .= "\n\n" . $e->getTraceAsString();
64 3
            }
65 3
            return $msg;
66
        }
67
    }
68
69
    /**
70
     * Set a string or a closure to be called that will generate the subject line for the notification
71
     * @param callable|string $cb A closure or string that will be set for the subject line
72
     */
73 3
    public function setSubject($cb)
74
    {
75 3
        $this->subjectCb = $this->wrapValueInClosure($cb);
76 3
        return $this;
77
    }
78
79
    /**
80
     * Returns the result of the subject closure
81
     * @param  Exception $e The Exception instance that you want to build the subject around
82
     * @return string       The subject string
83
     */
84 6
    public function getSubject(Exception $e) {
85 6
        if (is_callable($this->subjectCb)) {
86 3
            return $this->subjectCb->__invoke($e);
1 ignored issue
show
Bug introduced by
The method __invoke cannot be called on $this->subjectCb (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
87
        } else {
88 3
            return get_class($e);
89
        }
90
    }
91
92
    /**
93
     * Pushes on another Monolog Handler
94
     * @param  HandlerInterface $handler The handler instance to add on
95
     * @return Notifier                  Returns this
96
     */
97 3
    public function pushHandler(HandlerInterface $handler) {
98 3
        $this->log->pushHandler($handler);
99 3
        return $this;
100
    }
101
102
    /**
103
     * Triggers the Monolog Logger instance to log an error to all handlers
104
     * @param  Exception $e The exception to use
105
     * @return Notifier     Returns this
106
     */
107 3
    public function send(Exception $e) {
108 3
        $factory = new MonologHandlerFactory();
109 3
        $drivers = $this->config['drivers'];
110
111 3
        $message = $this->getMessage($e);
112 3
        $subject = $this->getSubject($e);
113
        
114 3
        foreach ($drivers as $driver) {
115 3
            $handler = $factory->create($driver, $subject);
116 3
            $this->log->pushHandler($handler);
117 3
        }
118
119 3
        $this->log->addCritical($message);
120
121 3
        return $this;
122
    }
123
}