Completed
Push — master ( 39684b...9267d8 )
by Tyler
02:05
created

LERN::pushHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Tylercd100\LERN;
4
5
use Exception;
6
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
7
use Tylercd100\LERN\Models\ExceptionModel;
8
use Tylercd100\LERN\Notifications\Notifier;
9
10
/**
11
* The master class
12
*/
13
class LERN 
14
{
15
16
    private $exception;
17
    private $notifier;
18
    
19
    /**
20
     * @param Notifier|null $notifier Notifier instance
21
     */
22
    public function __construct(Notifier $notifier = null)
23
    {
24
        if (empty($notifier))
25
            $notifier = new Notifier();
26
        $this->notifier = $notifier;
27
    }
28
29
    /**
30
     * Will execute record and notify methods
31
     * @param  Exception $e   The exception to use
32
     * @return ExceptionModel the recorded Eloquent Model
33
     */
34
    public function handle(Exception $e)
35
    {
36
        $this->exception = $e;
37
        $this->notify($e);
38
        return $this->record($e);
39
    }
40
41
    /**
42
     * Stores the exception in the database
43
     * @param  Exception $e   The exception to use
44
     * @return ExceptionModel the recorded Eloquent Model
45
     */
46
    public function record(Exception $e)
47
    {
48
        $this->exception = $e;
49
        $opts = [
50
            'class'       => get_class($e),
51
            'file'        => $e->getFile(),
52
            'line'        => $e->getLine(),
53
            'code'        => $e->getCode(),
54
            'message'     => $e->getMessage(),
55
            'trace'       => $e->getTraceAsString(),
56
        ];
57
58
        if ($e instanceof HttpExceptionInterface)
59
            $opts['status_code'] = $e->getStatusCode();
60
61
        return ExceptionModel::create($opts);
62
    }
63
64
    /**
65
     * Will send the exception to all monolog handlers
66
     * @param  Exception $e The exception to use
67
     * @return void
68
     */
69
    public function notify(Exception $e)
70
    {
71
        $this->exception = $e;
72
        $this->notifier->send($e);
73
    }
74
75
    /**
76
     * Pushes on another Monolog Handler
77
     * @param  HandlerInterface $handler The handler instance to add on
78
     * @return Notifier                  Returns this
79
     */
80
    public function pushHandler(HandlerInterface $handler){
81
        $this->notifier->pushHandler($handler);
82
        return $this;
83
    }
84
85
    /**
86
     * Get Notifier
87
     * @return Notifier 
88
     */
89
    public function getNotifier()
90
    {
91
        return $this->notifier;
92
    }
93
94
    /**
95
     * Set Notifier
96
     * @param Notifier $notifier A Notifier instance to use
97
     */
98
    public function setNotifier(Notifier $notifier)
99
    {
100
        $this->notifier = $notifier;
101
        return $this;
102
    }
103
104
    /**
105
     * Set a string or a closure to be called that will generate the message body for the notification
106
     * @param function|string $cb This closure function will be passed an Exception and must return a string
107
     */
108
    public function setMessage($cb)
109
    {
110
        $this->notifier->setMessage($cb);
111
        return $this;
112
    }
113
114
    /**
115
     * Set a string or a closure to be called that will generate the subject line for the notification
116
     * @param function|string $cb This closure function will be passed an Exception and must return a string
117
     */
118
    public function setSubject($cb)
119
    {
120
        $this->notifier->setSubject($cb);
121
        return $this;
122
    }
123
124
}