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

WC_Pagantis_Logger   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
c 0
b 0
f 0
dl 0
loc 82
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A clearLogs() 0 6 2
A insert_log_entry_in_wpdb() 0 14 2
B writeLog() 0 36 11
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
     * @since 8.6.7
22
     */
23
    public static function writeLog($message, $start_time = null, $end_time = null)
24
    {
25
        if (! class_exists('WC_Logger')) {
26
            return;
27
        }
28
29
        if ($message) {
30
            if (empty(self::$logger)) {
31
                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

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

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

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

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

68
            self::$logger = /** @scrutinizer ignore-call */ wc_get_logger();
Loading history...
69
        }
70
        self::$logger->clear(self::WC_LOG_FILENAME);
71
    }
72
73
    /**
74
     * @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...
75
     * @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...
76
     * @global wpdb $wpdb WordPress database abstraction object.
77
     */
78
    public static function insert_log_entry_in_wpdb($exception = null, $message = null)
79
    {
80
        require_once(__ROOT__.'/vendor/autoload.php');
81
82
        global $wpdb;
83
        pg_wc_check_db_log_table();
84
        $logEntry = new LogEntry();
85
        if ($exception instanceof \Exception) {
86
            $logEntry = $logEntry->error($exception);
87
        } else {
88
            $logEntry = $logEntry->info($message);
89
        }
90
        $tableName = $wpdb->prefix . PAGANTIS_LOGS_TABLE;
91
        $wpdb->insert($tableName, array('log' => $logEntry->toJson()));
92
    }
93
}
94