Completed
Push — master ( 87829d...8abbe0 )
by Tyler
59:29 queued 57:49
created

LERN::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
crap 3
1
<?php
2
3
namespace Tylercd100\LERN;
4
5
use Exception;
6
use Monolog\Handler\HandlerInterface;
7
use Tylercd100\LERN\Components\Notifier;
8
use Tylercd100\LERN\Components\Recorder;
9
10
/**
11
* The master class
12
*/
13
class LERN 
14
{
15
    /**
16
     * @var Exception
17
     */
18
    private $exception;
19
20
    /**
21
     * @var \Tylercd100\LERN\Components\Notifier
22
     */
23
    private $notifier;
24
25
    /**
26
     * @var \Tylercd100\LERN\Components\Recorder
27
     */
28
    private $recorder;
29
    
30
    /**
31
     * @param \Tylercd100\LERN\Components\Notifier|null $notifier Notifier instance
32
     * @param \Tylercd100\LERN\Components\Recorder|null $recorder Recorder instance
33
     */
34 27
    public function __construct(Notifier $notifier = null, Recorder $recorder = null)
35
    {
36 27
        if (empty($notifier)) {
37 15
            $notifier = new Notifier();
38
        }
39 27
        $this->notifier = $notifier;
40
41 27
        if (empty($recorder)) {
42 27
            $recorder = new Recorder();
43
        }
44 27
        $this->recorder = $recorder;
45 27
    }
46
47
    /**
48
     * Will execute record and notify methods
49
     * @param  Exception $e   The exception to use
50
     * @return ExceptionModel the recorded Eloquent Model
51
     */
52 3
    public function handle(Exception $e)
53
    {
54 3
        $this->exception = $e;
55 3
        $this->notify($e);
56 3
        return $this->record($e);
57
    }
58
59
    /**
60
     * Stores the exception in the database
61
     * @param  Exception $e   The exception to use
62
     * @return \Tylercd100\LERN\Models\ExceptionModel|false The recorded Exception as an Eloquent Model
63
     */
64 3
    public function record(Exception $e)
65
    {
66 3
        $this->exception = $e;
67 3
        return $this->recorder->record($e);
68
    }
69
70
    /**
71
     * Will send the exception to all monolog handlers
72
     * @param  Exception $e The exception to use
73
     * @return void
74
     */
75 3
    public function notify(Exception $e)
76
    {
77 3
        $this->exception = $e;
78 3
        $this->notifier->send($e);
79 3
    }
80
81
    /**
82
     * Pushes on another Monolog Handler
83
     * @param  HandlerInterface $handler The handler instance to add on
84
     * @return $this
85
     */
86 3
    public function pushHandler(HandlerInterface $handler) {
87 3
        $this->notifier->pushHandler($handler);
88 3
        return $this;
89
    }
90
91
    /**
92
     * Get Notifier
93
     * @return \Tylercd100\LERN\Components\Notifier 
94
     */
95 3
    public function getNotifier()
96
    {
97 3
        return $this->notifier;
98
    }
99
100
    /**
101
     * Set Notifier
102
     * @param \Tylercd100\LERN\Components\Notifier $notifier A Notifier instance to use
103
     * @return \Tylercd100\LERN\LERN
104
     */
105 3
    public function setNotifier(Notifier $notifier)
106
    {
107 3
        $this->notifier = $notifier;
108 3
        return $this;
109
    }
110
111
    /**
112
     * Get Recorder
113
     * @return \Tylercd100\LERN\Components\Recorder 
114
     */
115 3
    public function getRecorder()
116
    {
117 3
        return $this->recorder;
118
    }
119
120
    /**
121
     * Set Recorder
122
     * @param \Tylercd100\LERN\Components\Recorder $recorder A Recorder instance to use
123
     * @return \Tylercd100\LERN\LERN
124
     */
125 3
    public function setRecorder(Recorder $recorder)
126
    {
127 3
        $this->recorder = $recorder;
128 3
        return $this;
129
    }
130
131
    /**
132
     * Set a string or a closure to be called that will generate the message body for the notification
133
     * @param function|string $cb This closure function will be passed an Exception and must return a string
134
     * @return $this
135
     */
136 3
    public function setMessage($cb)
137
    {
138 3
        $this->notifier->setMessage($cb);
139 3
        return $this;
140
    }
141
142
    /**
143
     * Set a string or a closure to be called that will generate the subject line for the notification
144
     * @param function|string $cb This closure function will be passed an Exception and must return a string
145
     * @return $this
146
     */
147 3
    public function setSubject($cb)
148
    {
149 3
        $this->notifier->setSubject($cb);
150 3
        return $this;
151
    }
152
153
}