Completed
Push — master ( 74c845...c12462 )
by Vojta
03:22
created

Plugin::setNativeMailerHandler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 22
rs 9.2
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace VojtaSvoboda\ErrorLogger;
4
5
use Backend\Facades\BackendAuth;
6
use Config;
7
use Log;
8
use VojtaSvoboda\ErrorLogger\Models\Settings;
9
use Monolog\Formatter\LineFormatter;
10
use Monolog\Handler\NativeMailerHandler;
11
use Monolog\Handler\NewRelicHandler;
12
use Monolog\Handler\SlackHandler;
13
use Monolog\Logger;
14
use System\Classes\PluginBase;
15
16
/**
17
 * Error Logger Plugin Information File
18
 */
19
class Plugin extends PluginBase
20
{
21
    /**
22
     * Returns information about this plugin.
23
     *
24
     * @return array
25
     */
26
    public function pluginDetails()
27
    {
28
        return [
29
            'name' => 'vojtasvoboda.errorlogger::lang.plugin.name',
30
            'description' => 'vojtasvoboda.errorlogger::lang.plugin.description',
31
            'author' => 'Vojta Svoboda',
32
            'icon' => 'icon-bug',
33
        ];
34
    }
35
36
    public function registerSettings()
37
    {
38
        return [
39
            'config' => [
40
                'label' => 'vojtasvoboda.errorlogger::lang.settings.label',
41
                'category' => 'system::lang.system.categories.system',
42
                'icon' => 'icon-bug',
43
                'description' => 'vojtasvoboda.errorlogger::lang.settings.description',
44
                'class' => 'VojtaSvoboda\ErrorLogger\Models\Settings',
45
                'permissions' => ['vojtasvoboda.errorlogger.*'],
46
                'order' => 610,
47
            ]
48
        ];
49
    }
50
51
    public function registerPermissions()
52
    {
53
        return [
54
            'vojtasvoboda.errorlogger.*' => [
55
                'tab' => 'vojtasvoboda.errorlogger::lang.permissions.tab',
56
                'label' => 'vojtasvoboda.errorlogger::lang.permissions.all.label'
57
            ]
58
        ];
59
    }
60
61
    /**
62
     * Boot plugin and register handlers
63
     */
64
    public function boot()
65
    {
66
        $monolog = Log::getMonolog();
67
68
        $this->setNativeMailerHandler($monolog);
69
        $this->setSlackHandler($monolog);
70
        $this->setSyslogHandler($monolog);
71
        $this->setNewrelicHandler($monolog);
72
    }
73
74
    /**
75
     * Set native mailer handler
76
     * 
77
     * Formatting lines example (use before pushHandler()):
78
     *   $formater = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n");
79
     *   $handler->setFormatter($formater);
80
     *
81
     * @param $monolog
82
     *
83
     * @return Logger
84
     */
85
    private function setNativeMailerHandler($monolog)
86
    {
87
        $required = ['nativemailer_enabled', 'nativemailer_email'];
88
        if (!$this->checkRequiredFields($required)) {
89
            return $monolog;
90
        }
91
92
        // disable when debug is true
93
        $debug = Settings::get('nativemailer_debug');
94
        if ($debug & Config::get('app.debug')) {
95
            return $monolog;
96
        }
97
98
        $email = Settings::get('nativemailer_email');
99
        $subject = Config::get('app.url') . ' - error report';
100
        $from = Config::get('mail.from.address');
101
        $level = Settings::get('nativemailer_level', 100);
102
        $handler = new NativeMailerHandler($email, $subject, $from, $level);
103
        $monolog->pushHandler($handler);
104
105
        return $monolog;
106
    }
107
108
    /**
109
     * Set handler for Slack messaging app
110
     *
111
     * @param $monolog
112
     *
113
     * @return Logger
114
     */
115
    private function setSlackHandler($monolog)
116
    {
117
        $required = ['slack_enabled', 'slack_token'];
118
        if (!$this->checkRequiredFields($required)) {
119
            return $monolog;
120
        }
121
122
        $token = Settings::get('slack_token');
123
        $channel = Settings::get('slack_channel', 'random');
124
        $username = Settings::get('slack_username', 'error-bot');
125
        $attachment = Settings::get('slack_attachment', false);
126
        $level = Settings::get('slack_level', 100);
127
        $handler = new SlackHandler($token, $channel, $username, $attachment, null, $level);
128
        $monolog->pushHandler($handler);
129
130
        return $monolog;
131
    }
132
133
    /**
134
     * Set handler for Syslog
135
     *
136
     * @param $monolog
137
     *
138
     * @return Logger
139
     */
140 View Code Duplication
    private function setSyslogHandler($monolog)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $required = ['syslog_enabled', 'syslog_ident', 'syslog_facility'];
143
        if (!$this->checkRequiredFields($required)) {
144
            return $monolog;
145
        }
146
147
        $ident = Settings::get('syslog_ident');
148
        $facility = Settings::get('syslog_facility');
149
        $level = Settings::get('syslog_level', 100);
150
        $handler = new SyslogHandler($ident, $facility, $level);
151
        $monolog->pushHandler($handler);
152
153
        return $monolog;
154
    }
155
156
    /**
157
     * Set handler for New Relic
158
     *
159
     * @param $monolog
160
     *
161
     * @return Logger
162
     */
163 View Code Duplication
    private function setNewrelicHandler($monolog)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165
        $required = ['newrelic_enabled', 'newrelic_appname'];
166
        if (!$this->checkRequiredFields($required)) {
167
            return $monolog;
168
        }
169
170
        $appname = Settings::get('newrelic_appname');
171
        $level = Settings::get('newrelic_level', 100);
172
        $bubble = true;
173
        $handler = new NewRelicHandler($level, $bubble, $appname);
174
        $monolog->pushHandler($handler);
175
176
        return $monolog;
177
    }
178
179
    /**
180
     * Check each required field if exist and not empty
181
     *
182
     * @param array $fields
183
     *
184
     * @return bool
185
     */
186
    private function checkRequiredFields(array $fields)
187
    {
188
        foreach ($fields as $field) {
189
            $value = Settings::get($field);
190
            if (!$value || empty($value)) {
191
                return false;
192
            }
193
        }
194
195
        return true;
196
    }
197
}
198