Passed
Pull Request — master (#48)
by
unknown
02:40
created

WC_Pagantis_Logger::writeLog()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 24
c 0
b 0
f 0
nc 16
nop 3
dl 0
loc 36
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Pagantis\ModuleUtils\Model\Log\LogEntry;
4
5
if (! defined('ABSPATH')) {
6
    exit;
7
}
8
9
10
class WC_Pagantis_Logger
11
{
12
    public static $logger;
13
    const WC_LOG_FILENAME = 'pagantis-wc';
14
15
    /**
16
     * Utilize WC logger class
17
     *
18
     * @param      $message
19
     * @param null $start_time
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $start_time is correct as it would always require null to be passed?
Loading history...
20
     * @param null $end_time
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $end_time is correct as it would always require null to be passed?
Loading history...
21
     *
22
     * @since 8.6.7
23
     */
24
    public static function writeLog($message, $start_time = null, $end_time = null)
25
    {
26
        if (! class_exists('WC_Logger')) {
27
            return;
28
        }
29
30
        if ($message) {
31
            if (empty(self::$logger)) {
32
                self::$logger = wc_get_logger();
0 ignored issues
show
Bug introduced by
The function wc_get_logger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
                self::$logger = /** @scrutinizer ignore-call */ wc_get_logger();
Loading history...
33
            }
34
35
            $isDebugLogEnabled = 'yes' === WC_Admin_Settings::get_option('debug', 'no');
0 ignored issues
show
Bug introduced by
The type WC_Admin_Settings was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
            $settings          = get_option('woocommerce_pagantis_settings');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
            $settings          = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
37
38
            if (empty($settings) || isset($settings['debug']) && $isDebugLogEnabled) {
39
                return;
40
            }
41
42
            if (! is_null($start_time)) {
0 ignored issues
show
introduced by
The condition is_null($start_time) is always true.
Loading history...
43
                $formatted_start_time = date_i18n(get_option('date_format') . ' g:ia', $start_time);
0 ignored issues
show
Bug introduced by
The function date_i18n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
                $formatted_start_time = /** @scrutinizer ignore-call */ date_i18n(get_option('date_format') . ' g:ia', $start_time);
Loading history...
44
                $end_time             = is_null($end_time) ? current_time('timestamp') : $end_time;
0 ignored issues
show
Bug introduced by
The function current_time was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
                $end_time             = is_null($end_time) ? /** @scrutinizer ignore-call */ current_time('timestamp') : $end_time;
Loading history...
45
                $formatted_end_time   = date_i18n(get_option('date_format') . ' g:ia', $end_time);
46
                $elapsed_time         = round(abs($end_time - $start_time) / 60, 2);
47
48
                $log_entry = "\n" . '====Pagantis Version: ' . PAGANTIS_VERSION . '====' . "\n";
49
                $log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n";
50
                $log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n";
51
            } else {
52
                $log_entry = "\n" . '====Pagantis Version: ' . PAGANTIS_VERSION . '====' . "\n";
53
                $log_entry .= '====Start Log====' . "\n" . $message . "\n" . '====End Log====' . "\n\n";
54
            }
55
            if (defined('WP_DEBUG') && WP_DEBUG) {
0 ignored issues
show
Bug introduced by
The constant WP_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
                error_log($log_entry);
57
            }
58
            print_r($log_entry);
59
            self::$logger->debug($log_entry, array('source' => self::WC_LOG_FILENAME));
60
        }
61
    }
62
63
    /**
64
     * empty log file
65
     */
66
    public static function clearLogs()
67
    {
68
        if (empty(self::$logger)) {
69
            self::$logger = wc_get_logger();
0 ignored issues
show
Bug introduced by
The function wc_get_logger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            self::$logger = /** @scrutinizer ignore-call */ wc_get_logger();
Loading history...
70
        }
71
        self::$logger->clear(self::WC_LOG_FILENAME);
72
    }
73
74
    /**
75
     * @param null  $exception
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $exception is correct as it would always require null to be passed?
Loading history...
76
     * @param null  $message
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $message is correct as it would always require null to be passed?
Loading history...
77
     *
78
     * @global wpdb $wpdb WordPress database abstraction object.
79
     */
80
    public static function insert_log_entry_in_wpdb($exception = null, $message = null)
81
    {
82
        require_once(__ROOT__ . '/vendor/autoload.php');
83
84
        global $wpdb;
85
        pg_wc_check_db_log_table();
86
        $logEntry = new LogEntry();
87
        if ($exception instanceof \Exception) {
88
            $logEntry = $logEntry->error($exception);
89
        } else {
90
            $logEntry = $logEntry->info($message);
91
        }
92
        $tableName = $wpdb->prefix . PAGANTIS_LOGS_TABLE;
93
        $wpdb->insert($tableName, array('log' => $logEntry->toJson()));
94
    }
95
}
96