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

WC_Stripe_Logger::log()   C

Complexity

Conditions 11
Paths 23

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 27
nc 23
nop 3
dl 0
loc 44
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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