Completed
Push — master ( 668e96...9de17e )
by Tyler
02:08
created

MonologHandlerFactory::hipchat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
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  array  $subject                  Title or Subject line for the notification
16
     * @return \Monolog\Handler\HandlerInterface A handler to use with a Monolog\Logger instance
17
     */
18
    public function create($driver, $subject = null)
19
    {
20
        $this->config = config('lern.notify.' . $driver);
21
        if (is_array($this->config)) {
22
                    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
    protected function fleephook(){
31
        return new \Monolog\Handler\FleepHookHandler(
32
            $this->config['token'],
33
            Logger::ERROR
34
        );
35
    }
36
37
    /**
38
     * Creates HipChat Monolog Handler
39
     * @return \Monolog\Handler\HipChatHandler A handler to use with a Monolog\Logger instance
40
     */
41
    protected function hipchat(){
42
        return new \Monolog\Handler\HipChatHandler(
43
            $this->config['token'],
44
            $this->config['room'],
45
            $this->config['name'],
46
            $this->config['notify'],
47
            Logger::ERROR
48
        );
49
    }
50
51
    /**
52
     * Creates Flowdock Monolog Handler
53
     * @return \Monolog\Handler\FlowdockHandler A handler to use with a Monolog\Logger instance
54
     */
55
    protected function flowdock(){
56
        return new \Monolog\Handler\FlowdockHandler(
57
            $this->config['token'],
58
            Logger::ERROR
59
        );
60
    }
61
62
    /**
63
     * Creates Pushover Monolog Handler
64
     * @param  array $subject  Title or Subject line for the notification
65
     * @return \Monolog\Handler\PushoverHandler A handler to use with a Monolog\Logger instance
66
     */
67
    protected function pushover($subject)
68
    {
69
        $this->checkSubject($subject);
70
        return new \Monolog\Handler\PushoverHandler(
71
            $this->config['token'],
72
            $this->config['user'],
73
            $subject,
0 ignored issues
show
Documentation introduced by
$subject is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
            Logger::ERROR
75
        );
76
    }
77
78
    /**
79
     * Creates Mail Monolog Handler
80
     * @param  array $subject      Title or Subject line for the notification
81
     * @return \Monolog\Handler\NativeMailerHandler A handler to use with a Monolog\Logger instance
82
     */
83
    protected function mail($subject)
84
    {
85
        $this->checkSubject($subject);
86
        return new \Monolog\Handler\NativeMailerHandler(
87
            $this->config['to'],
88
            $subject,
0 ignored issues
show
Documentation introduced by
$subject is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
            $this->config['from']
90
        );
91
    }
92
93
    /**
94
     * Creates Slack Monolog Handler
95
     * @return \Monolog\Handler\SlackHandler   A handler to use with a Monolog\Logger instance
96
     */
97
    protected function slack()
98
    {
99
        return new \Monolog\Handler\SlackHandler(
100
            $this->config['token'], 
101
            $this->config['channel'], 
102
            $this->config['username']
103
        );
104
    }
105
106
    /**
107
     * Validates that the subject is an unempty string
108
     * @param  mixed $subject [description]
109
     * @return [type]          [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
110
     */
111
    private function checkSubject($subject) {
112
        if (empty($subject)) {
113
            throw new Exception('$subject must not be empty!');
114
        }
115
116
        if (!is_string($subject)) {
117
            throw new Exception('$subject must be a string!');
118
        }
119
    }
120
}