Completed
Push — master ( 158d16...1af118 )
by Roy
13s queued 10s
created

WC_Stripe_Logger   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C log() 0 44 11
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit; // Exit if accessed directly
4
}
5
6
/**
7
 * Log all things!
8
 *
9
 * @since 4.0.0
10
 * @version 4.0.0
11
 */
12
class WC_Stripe_Logger {
13
14
	public static $logger;
15
	const WC_LOG_FILENAME = 'woocommerce-gateway-stripe';
16
17
	/**
18
	 * Utilize WC logger class
19
	 *
20
	 * @since 4.0.0
21
	 * @version 4.0.0
22
	 */
23
	public static function log( $message, $start_time = null, $end_time = null ) {
24
		if ( ! class_exists( 'WC_Logger' ) ) {
25
			return;
26
		}
27
28
		if ( apply_filters( 'wc_stripe_logging', true, $message ) ) {
29
			if ( empty( self::$logger ) ) {
30
				if ( version_compare( WC_VERSION, '3.0.0', '>=' ) ) {
31
					self::$logger = wc_get_logger();
32
				} else {
33
					self::$logger = new WC_Logger();
34
				}
35
			}
36
37
			$settings = get_option( 'woocommerce_stripe_settings' );
38
39
			if ( empty( $settings ) || isset( $settings['logging'] ) && 'yes' !== $settings['logging'] ) {
40
				return;
41
			}
42
43
			if ( ! is_null( $start_time ) ) {
44
45
				$formatted_start_time = date_i18n( get_option( 'date_format' ) . ' g:ia', $start_time );
46
				$end_time             = is_null( $end_time ) ? current_time( 'timestamp' ) : $end_time;
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" . '====Stripe Version: ' . WC_STRIPE_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
54
			} else {
55
				$log_entry = "\n" . '====Stripe Version: ' . WC_STRIPE_VERSION . '====' . "\n";
56
				$log_entry .= '====Start Log====' . "\n" . $message . "\n" . '====End Log====' . "\n\n";
57
58
			}
59
60
			if ( version_compare( WC_VERSION, '3.0.0', '>=' ) ) {
61
				self::$logger->debug( $log_entry, array( 'source' => self::WC_LOG_FILENAME ) );
62
			} else {
63
				self::$logger->add( self::WC_LOG_FILENAME, $log_entry );
64
			}
65
		}
66
	}
67
}
68