Passed
Pull Request — master (#82)
by
unknown
04:31 queued 01:31
created

pagantisLogger::log()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 26
c 2
b 0
f 0
nc 16
nop 3
dl 0
loc 38
rs 8.4444
1
<?php
2
3
4
if (!defined('ABSPATH')) {
5
    exit; // Exit if accessed directly
6
}
7
8
/**
9
 * Log helper for debugging
10
 */
11
class pagantisLogger
12
{
13
14
    public static $logger;
15
    const PG_LOG_FILENAME = 'pagantis-woocommerce-gateway';
16
17
    /**
18
     * Utilize WC logger class
19
     *
20
     * @param      $message
21
     * @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...
22
     * @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...
23
     * @version 1.0.0
24
     * @since   8.6.9
25
     */
26
    public static function log($message, $start_time = null, $end_time = null)
27
    {
28
29
        if (!class_exists('WC_Logger')) {
30
            return;
31
        }
32
        if (empty(self::$logger)) {
33
            if (version_compare(WC_VERSION, 3.0, '<')) {
0 ignored issues
show
Bug introduced by
The constant WC_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
34
                self::$logger = new WC_Logger();
0 ignored issues
show
Bug introduced by
The type WC_Logger 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
            } else {
36
                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

36
                self::$logger = /** @scrutinizer ignore-call */ wc_get_logger();
Loading history...
37
            }
38
39
40
            if (!defined('WP_DEBUG')) {
41
                return;
42
            }
43
44
            if (!is_null($start_time)) {
0 ignored issues
show
introduced by
The condition is_null($start_time) is always true.
Loading history...
45
                $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

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

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

46
                $end_time = is_null($end_time) ? /** @scrutinizer ignore-call */ current_time('timestamp') : $end_time;
Loading history...
47
                $formatted_end_time = date_i18n(get_option('date_format') . ' g:ia', $end_time);
48
                $elapsed_time = round(abs($end_time - $start_time) / 60, 2);
49
50
                $log_entry = "\n" . '====Pagantis Version: ' . '====' . "\n";
51
                $log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n";
52
                $log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n";
53
            } else {
54
                $log_entry = "\n" . '====Pagantis LOG ====' . PHP_EOL;
55
                $log_entry .= date_i18n('M j, Y @ G:i' , strtotime( 'now' ), true ) . PHP_EOL;
56
                $log_entry .= json_encode($message, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) . PHP_EOL;
57
                $log_entry .= PHP_EOL;
58
            }
59
60
            if (version_compare(WC_VERSION, 3.0, '<')) {
61
                self::$logger->add(self::PG_LOG_FILENAME, json_encode($log_entry, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) );
62
            } else {
63
                self::$logger->debug($log_entry, array('source' => self::PG_LOG_FILENAME));
64
            }
65
        }
66
    }
67
68
    public static function pg_debug_backtrace($return = true, $html = false, $show_first = true)
69
    {
70
71
        $d = debug_backtrace();
72
        $out = '';
73
        if ($html) {
74
            $out .= "<pre>";
75
        }
76
        foreach ($d as $i => $r) {
77
            if (!$show_first && $i == 0) {
78
                continue;
79
            }
80
            // sometimes there is undefined index 'file'
81
            @$out .= "[$i] {$r['file']}:{$r['line']}\n";
82
        }
83
        if ($html) {
84
            $out .= "</pre>";
85
        }
86
        if ($return) {
87
            return $out;
88
        } else {
89
            echo $out;
90
        }
91
    }
92
93
94
    public static function pg_print_r($var)
95
    {
96
        echo '<pre>';
97
        print_r($var);
98
        echo '</pre>';
99
    }
100
101
    public static function toSnakeCase($str, $delimiter = '_')
102
    {
103
        $str = lcfirst($str);
104
        $lowerCase = strtolower($str);
105
        $result = '';
106
        $length = strlen($str);
107
        for ($i = 0; $i < $length; $i++) {
108
            $result .= ($str[$i] === $lowerCase[$i] ? '' : $delimiter) . $lowerCase[$i];
109
        }
110
        return $result;
111
    }
112
113
    public static function jsonSerialize($data)
114
    {
115
        $arrayProperties = array();
116
117
        foreach ($data as $key => $value) {
118
            $arrayProperties[self::toSnakeCase($key)] = $value;
119
        }
120
121
        return $arrayProperties;
122
    }
123
124
    public static function toJson($initialResponse)
125
    {
126
        if (!is_array($initialResponse)){
127
            return;
128
        }
129
        $response = self::jsonSerialize($initialResponse);
130
131
        return json_encode($response, JSON_UNESCAPED_SLASHES);
132
133
    }
134
135
}
136