Completed
Pull Request — master (#605)
by Roy
05:45
created

WC_Stripe_Logger::log()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 10
nop 3
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
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
				self::$logger = new WC_Logger();
31
			}
32
33
			$settings = get_option( 'woocommerce_stripe_settings' );
34
35
			if ( empty( $settings ) || isset( $settings['logging'] ) && 'yes' !== $settings['logging'] ) {
36
				return;
37
			}
38
39
			if ( ! is_null( $start_time ) ) {
40
41
				$formatted_start_time = date_i18n( get_option( 'date_format' ) . ' g:ia', $start_time );
42
				$end_time             = is_null( $end_time ) ? current_time( 'timestamp' ) : $end_time;
43
				$formatted_end_time   = date_i18n( get_option( 'date_format' ) . ' g:ia', $end_time );
44
				$elapsed_time         = round( abs( $end_time - $start_time ) / 60, 2 );
45
46
				$log_entry = "\n" . '====Stripe Version: ' . WC_STRIPE_VERSION . '====' . "\n";
47
				$log_entry .= '====Start Log ' . $formatted_start_time . '====' . "\n" . $message . "\n";
48
				$log_entry .= '====End Log ' . $formatted_end_time . ' (' . $elapsed_time . ')====' . "\n\n";
49
50
			} else {
51
				$log_entry = "\n" . '====Stripe Version: ' . WC_STRIPE_VERSION . '====' . "\n";
52
				$log_entry .= '====Start Log====' . "\n" . $message . "\n" . '====End Log====' . "\n\n";
53
54
			}
55
56
			self::$logger->add( self::WC_LOG_FILENAME, $log_entry );
57
		}
58
	}
59
}
60