Plugin   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 180
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 30
loc 180
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A pluginDetails() 0 9 1
A registerSettings() 0 14 1
A registerPermissions() 0 9 1
A setNativeMailerHandler() 0 22 3
A boot() 0 10 2
A setSlackHandler() 0 17 2
A setSyslogHandler() 15 15 2
A setNewrelicHandler() 15 15 2
A checkRequiredFields() 0 11 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Handler\SyslogHandler;
14
use Monolog\Logger;
15
use System\Classes\PluginBase;
16
17
/**
18
 * Error Logger Plugin Information File
19
 */
20
class Plugin extends PluginBase
21
{
22
    /**
23
     * Returns information about this plugin.
24
     *
25
     * @return array
26
     */
27
    public function pluginDetails()
28
    {
29
        return [
30
            'name' => 'vojtasvoboda.errorlogger::lang.plugin.name',
31
            'description' => 'vojtasvoboda.errorlogger::lang.plugin.description',
32
            'author' => 'Vojta Svoboda',
33
            'icon' => 'icon-bug',
34
        ];
35
    }
36
37
    public function registerSettings()
38
    {
39
        return [
40
            'config' => [
41
                'label' => 'vojtasvoboda.errorlogger::lang.settings.label',
42
                'category' => 'system::lang.system.categories.system',
43
                'icon' => 'icon-bug',
44
                'description' => 'vojtasvoboda.errorlogger::lang.settings.description',
45
                'class' => 'VojtaSvoboda\ErrorLogger\Models\Settings',
46
                'permissions' => ['vojtasvoboda.errorlogger.*'],
47
                'order' => 610,
48
            ]
49
        ];
50
    }
51
52
    public function registerPermissions()
53
    {
54
        return [
55
            'vojtasvoboda.errorlogger.*' => [
56
                'tab' => 'vojtasvoboda.errorlogger::lang.permissions.tab',
57
                'label' => 'vojtasvoboda.errorlogger::lang.permissions.all.label'
58
            ]
59
        ];
60
    }
61
62
    /**
63
     * Boot plugin and register handlers
64
     */
65
    public function boot()
66
    {
67
        $isLaravel56OrUp = method_exists(\Illuminate\Log\Logger::class, 'getLogger');
68
        $monolog = $isLaravel56OrUp ? Log::getLogger() : Log::getMonolog();
69
70
        $this->setNativeMailerHandler($monolog);
71
        $this->setSlackHandler($monolog);
72
        $this->setSyslogHandler($monolog);
73
        $this->setNewrelicHandler($monolog);
74
    }
75
76
    /**
77
     * Set native mailer handler
78
     * 
79
     * Formatting lines example (use before pushHandler()):
80
     *   $formater = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n");
81
     *   $handler->setFormatter($formater);
82
     *
83
     * @param $monolog
84
     *
85
     * @return Logger
86
     */
87
    private function setNativeMailerHandler($monolog)
88
    {
89
        $required = ['nativemailer_enabled', 'nativemailer_email'];
90
        if (!$this->checkRequiredFields($required)) {
91
            return $monolog;
92
        }
93
94
        // disable when debug is true
95
        $debug = Settings::get('nativemailer_debug');
96
        if ($debug & Config::get('app.debug')) {
97
            return $monolog;
98
        }
99
100
        $email = Settings::get('nativemailer_email');
101
        $subject = Config::get('app.url') . ' - error report';
102
        $from = Config::get('mail.from.address');
103
        $level = Settings::get('nativemailer_level', 100);
104
        $handler = new NativeMailerHandler($email, $subject, $from, $level);
105
        $monolog->pushHandler($handler);
106
107
        return $monolog;
108
    }
109
110
    /**
111
     * Set handler for Slack messaging app
112
     *
113
     * @param $monolog
114
     *
115
     * @return Logger
116
     */
117
    private function setSlackHandler($monolog)
118
    {
119
        $required = ['slack_enabled', 'slack_token'];
120
        if (!$this->checkRequiredFields($required)) {
121
            return $monolog;
122
        }
123
124
        $token = Settings::get('slack_token');
125
        $channel = Settings::get('slack_channel', 'random');
126
        $username = Settings::get('slack_username', 'error-bot');
127
        $attachment = Settings::get('slack_attachment', false);
128
        $level = Settings::get('slack_level', 100);
129
        $handler = new SlackHandler($token, $channel, $username, $attachment, null, $level);
130
        $monolog->pushHandler($handler);
131
132
        return $monolog;
133
    }
134
135
    /**
136
     * Set handler for Syslog
137
     *
138
     * @param $monolog
139
     *
140
     * @return Logger
141
     */
142 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...
143
    {
144
        $required = ['syslog_enabled', 'syslog_ident', 'syslog_facility'];
145
        if (!$this->checkRequiredFields($required)) {
146
            return $monolog;
147
        }
148
149
        $ident = Settings::get('syslog_ident');
150
        $facility = Settings::get('syslog_facility');
151
        $level = Settings::get('syslog_level', 100);
152
        $handler = new SyslogHandler($ident, $facility, $level);
153
        $monolog->pushHandler($handler);
154
155
        return $monolog;
156
    }
157
158
    /**
159
     * Set handler for New Relic
160
     *
161
     * @param $monolog
162
     *
163
     * @return Logger
164
     */
165 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...
166
    {
167
        $required = ['newrelic_enabled', 'newrelic_appname'];
168
        if (!$this->checkRequiredFields($required)) {
169
            return $monolog;
170
        }
171
172
        $appname = Settings::get('newrelic_appname');
173
        $level = Settings::get('newrelic_level', 100);
174
        $bubble = true;
175
        $handler = new NewRelicHandler($level, $bubble, $appname);
176
        $monolog->pushHandler($handler);
177
178
        return $monolog;
179
    }
180
181
    /**
182
     * Check each required field if exist and not empty
183
     *
184
     * @param array $fields
185
     *
186
     * @return bool
187
     */
188
    private function checkRequiredFields(array $fields)
189
    {
190
        foreach ($fields as $field) {
191
            $value = Settings::get($field);
192
            if (!$value || empty($value)) {
193
                return false;
194
            }
195
        }
196
197
        return true;
198
    }
199
}
200