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

WcPagantisLogger   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 61
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A clearLogs() 0 6 2
B writeLog() 0 36 11
1
<?php
2
3
if (!defined('ABSPATH')) {
4
    exit; // Exit if accessed directly
5
}
6
7
8
class WcPagantisLogger
9
{
10
    public static $logger;
11
    const WC_LOG_FILENAME = 'pagantis-wc';
12
13
    /**
14
     * Utilize WC logger class
15
     *
16
     * @param $message
17
     * @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...
18
     * @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...
19
     * @since 8.6.7
20
     */
21
    public static function writeLog($message, $start_time = null, $end_time = null)
22
    {
23
        if (! class_exists('WC_Logger')) {
24
            return;
25
        }
26
27
        if ($message) {
28
            if (empty(self::$logger)) {
29
                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

29
                self::$logger = /** @scrutinizer ignore-call */ wc_get_logger();
Loading history...
30
            }
31
32
            $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...
33
            $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

33
            $settings = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
34
35
            if (empty($settings) || isset($settings['debug']) && $isDebugLogEnabled) {
36
                return;
37
            }
38
39
            if (! is_null($start_time)) {
0 ignored issues
show
introduced by
The condition is_null($start_time) is always true.
Loading history...
40
                $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

40
                $formatted_start_time = /** @scrutinizer ignore-call */ date_i18n(get_option('date_format') . ' g:ia', $start_time);
Loading history...
41
                $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

41
                $end_time             = is_null($end_time) ? /** @scrutinizer ignore-call */ current_time('timestamp') : $end_time;
Loading history...
42
                $formatted_end_time   = date_i18n(get_option('date_format') . ' g:ia', $end_time);
43
                $elapsed_time         = round(abs($end_time - $start_time) / 60, 2);
44
45
                $log_entry  = "\n" . '====Pagantis Version: ' . PAGANTIS_VERSION . '====' . "\n";
46
                $log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n";
47
                $log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n";
48
            } else {
49
                $log_entry  = "\n" . '====Pagantis Version: ' . PAGANTIS_VERSION . '====' . "\n";
50
                $log_entry .= '====Start Log====' . "\n" . $message . "\n" . '====End Log====' . "\n\n";
51
            }
52
            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...
53
                error_log($log_entry);
54
            }
55
            print_r($log_entry);
56
            self::$logger->debug($log_entry, array( 'source' => self::WC_LOG_FILENAME ));
57
        }
58
    }
59
60
    /**
61
     * empty log file
62
     */
63
    public static function clearLogs()
64
    {
65
        if (empty(self::$logger)) {
66
            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

66
            self::$logger = /** @scrutinizer ignore-call */ wc_get_logger();
Loading history...
67
        }
68
        self::$logger->clear(self::WC_LOG_FILENAME);
69
    }
70
}
71