Completed
Push — master ( 534c9b...1d5e73 )
by Tyler
04:01
created

MonologHandlerFactory::mail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tylercd100\LERN\Notifications;
4
5
use Exception;
6
use Monolog\Logger;
7
8
class MonologHandlerFactory {
9
10
    protected $config;
11
12
    /**
13
     * Creates a handler for a specified driver
14
     * @param  string $driver                    Lowercase driver string that is also in the config/lern.php file
15
     * @param  string $subject                   Title or Subject line for the notification
16
     * @return \Monolog\Handler\HandlerInterface A handler to use with a Monolog\Logger instance
17
     */
18 15
    public function create($driver, $subject = null)
19
    {
20 15
        $this->config = config('lern.notify.' . $driver);
21 15
        if (is_array($this->config)) {
22 15
            return $this->{$driver}($subject);
23
        }
24
    }
25
26
    /**
27
     * Creates FleepHook Monolog Handler
28
     * @return \Monolog\Handler\FleepHookHandler A handler to use with a Monolog\Logger instance
29
     */
30 6
    protected function fleephook() {
31 6
        return new \Monolog\Handler\FleepHookHandler(
32 6
            $this->config['token']
33 6
        );
34
    }
35
36
    /**
37
     * Creates HipChat Monolog Handler
38
     * @return \Monolog\Handler\HipChatHandler A handler to use with a Monolog\Logger instance
39
     */
40 6
    protected function hipchat() {
41 6
        return new \Monolog\Handler\HipChatHandler(
42 6
            $this->config['token'],
43 6
            $this->config['room'],
44 6
            $this->config['name'],
45 6
            $this->config['notify'],
46 6
            Logger::CRITICAL,
47 6
            true, 
48 6
            true, 
49 6
            'text', 
50 6
            'api.hipchat.com', 
51
            'v2'
52 6
        );
53
    }
54
55
    /**
56
     * Creates Flowdock Monolog Handler
57
     * @return \Monolog\Handler\FlowdockHandler A handler to use with a Monolog\Logger instance
58
     */
59 6
    protected function flowdock() {
60 6
        return new \Monolog\Handler\FlowdockHandler(
61 6
            $this->config['token']
62 6
        );
63
    }
64
65
    /**
66
     * Creates Pushover Monolog Handler
67
     * @param  string $subject  Title or Subject line for the notification
68
     * @return \Monolog\Handler\PushoverHandler A handler to use with a Monolog\Logger instance
69
     */
70 9
    protected function pushover($subject)
71
    {
72 9
        $this->checkSubject($subject);
73 9
        return new \Monolog\Handler\PushoverHandler(
74 9
            $this->config['token'],
75 9
            $this->config['user'],
76
            $subject
77 9
        );
78
    }
79
80
    /**
81
     * Creates Mail Monolog Handler
82
     * @param  string $subject Title or Subject line for the notification
83
     * @return \Monolog\Handler\NativeMailerHandler A handler to use with a Monolog\Logger instance
84
     */
85 9
    protected function mail($subject)
86
    {
87 9
        $this->checkSubject($subject);
88 9
        return new \Monolog\Handler\NativeMailerHandler(
89 9
            $this->config['to'],
90 9
            $subject,
91 9
            $this->config['from']
92 9
        );
93
    }
94
95
    /**
96
     * Creates Slack Monolog Handler
97
     * @return \Monolog\Handler\SlackHandler A handler to use with a Monolog\Logger instance
98
     */
99 12
    protected function slack()
100
    {
101 12
        return new \Monolog\Handler\SlackHandler(
102 12
            $this->config['token'], 
103 12
            $this->config['channel'], 
104 12
            $this->config['username']
105 12
        );
106
    }
107
108
    /**
109
     * Creates Plivo Monolog Handler
110
     * @return \Tylercd100\Monolog\Handler\PlivoHandler A handler to use with a Monolog\Logger instance
111
     */
112 6
    protected function plivo()
113
    {
114 6
        return new \Tylercd100\Monolog\Handler\PlivoHandler(
115 6
            $this->config['token'], 
116 6
            $this->config['auth_id'], 
117 6
            $this->config['from'],
118 6
            $this->config['to']
119 6
        );
120
    }
121
    /**
122
     * Validates that the subject is an unempty string
123
     * @param  mixed $subject The value to check
124
     * @return void
125
     */
126 12
    private function checkSubject($subject) {
127 12
        if (empty($subject)) {
128
            throw new Exception('$subject must not be empty!');
129
        }
130
131 12
        if (!is_string($subject)) {
132
            throw new Exception('$subject must be a string!');
133
        }
134
    }
135
}