Passed
Pull Request — master (#78)
by
unknown
03:41
created

pagantisLogger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B log() 0 38 8
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
    exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Log helper for debugging
9
 */
10
class pagantisLogger {
11
12
    public static $logger;
13
    const PG_LOG_FILENAME = 'pagantis-woocommerce-gateway';
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
     * @version 1.0.0
23
     * @since   8.6.9
24
     */
25
    public static function log( $message, $start_time = null, $end_time = null ) {
26
        if ( ! class_exists( 'WC_Logger' ) ) {
27
            return;
28
        }
29
30
            if ( empty( self::$logger ) ) {
31
                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...
32
                    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...
33
                } else {
34
                    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

34
                    self::$logger = /** @scrutinizer ignore-call */ wc_get_logger();
Loading history...
35
                }
36
37
38
            if (!defined( 'WP_DEBUG' )) {
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
44
                $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

44
                $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

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

45
                $end_time             = is_null( $end_time ) ? /** @scrutinizer ignore-call */ current_time( 'timestamp' ) : $end_time;
Loading history...
46
                $formatted_end_time   = date_i18n( get_option( 'date_format' ) . ' g:ia', $end_time );
47
                $elapsed_time         = round( abs( $end_time - $start_time ) / 60, 2 );
48
49
                $log_entry  = "\n" . '====Pagantis Version: ' . PG_VERSION . '====' . "\n";
50
                $log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n";
51
                $log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n";
52
53
            } else {
54
                $log_entry  = "\n" . '====Pagantis Version: ' . PG_VERSION . '====' . "\n";
55
                $log_entry .= '====Start Log====' . "\n" . $message . "\n" . '====End Log====' . "\n\n";
56
57
            }
58
59
            if (version_compare( WC_VERSION, 3.0, '<' )) {
60
                self::$logger->add( self::PG_LOG_FILENAME, $log_entry );
61
            } else {
62
                self::$logger->debug( $log_entry, array( 'source' => self::PG_LOG_FILENAME ) );
63
            }
64
        }
65
    }
66
}
67