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

LERN   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 11
c 5
b 1
f 2
lcom 1
cbo 2
dl 0
loc 112
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A handle() 0 6 1
A record() 0 17 2
A notify() 0 5 1
A pushHandler() 0 4 1
A getNotifier() 0 4 1
A setNotifier() 0 5 1
A setMessage() 0 5 1
A setSubject() 0 5 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
}