Completed
Push — add/importer-stats ( c551c0...5337a5 )
by
unknown
07:23
created

Jetpack_Import_Stats::get_calling_class()   B

Complexity

Conditions 10
Paths 21

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 21
nop 0
dl 0
loc 42
rs 7.6666
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Jetpack_Import_Stats::init() 0 8 2
A Jetpack_Import_Stats::log_import_progress() 0 22 3

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
3
require_once dirname( __FILE__ ) . '/sync/class.jetpack-sync-functions.php';
4
5
class Jetpack_Import_Stats {
6
	/**
7
	 * A mapping of known importers to friendly names.
8
	 * Keys are the class name of the known importer.
9
	 * Values are the friendly name.
10
	 * @var array
11
	 */
12
	private static $known_importers = array(
13
		'Blogger_Importer' => 'blogger',
14
		'LJ_API_Import' => 'livejournal',
15
		'MT_Import' => 'mt',
16
		'RSS_Import' => 'rss',
17
		'WC_Tax_Rate_Importer' => 'woo-tax-rate',
18
		'WP_Import' => 'wordpress',
19
	);
20
21
	/**
22
	 * A mapping of action types to event name.
23
	 * Keys are the name of the action.
24
	 * Values are the event name recorded for that action.
25
	 * @var array
26
	 */
27
	private static $action_event_name_map = array(
28
		'import_start' => 'jetpack_import_start',
29
		'import_done'  => 'jetpack_import_done',
30
		'import_end'   => 'jetpack_import_done',
31
	);
32
33
	public static function init() {
34
		// Only handle import actions for sites that have agreed to TOS 
35
		if ( Jetpack::jetpack_tos_agreed() ) {
36
			add_action( 'import_start', array( 'Jetpack_Import_Stats', 'log_import_progress' ) );
37
			add_action( 'import_done',  array( 'Jetpack_Import_Stats', 'log_import_progress' ) );
38
			add_action( 'import_end',   array( 'Jetpack_Import_Stats', 'log_import_progress' ) );
39
		}
40
	}
41
42
	public static function log_import_progress( $importer ) {
43
		// prefer self-reported importer-names
44
		if ( ! $importer ) {
45
			// fall back to inferring by calling class name
46
			$importer = Jetpack_Sync_Functions::get_calling_importer_class();
47
		}
48
		
49
		// Give known importers a "friendly" name
50
		if ( isset( self::$known_importers[ $importer ] ) ) {
51
			$importer = self::$known_importers[ $importer ];
52
		}
53
		$action = current_filter();
54
		// map action to event name
55
		$event_name = self::$action_event_name_map[ $action ];
56
		
57
		$current_user = wp_get_current_user();
58
59
		// Record event to Tracks
60
		jetpack_tracks_record_event( $current_user, $event_name, array(
61
			'importer' => $importer,
62
		) );
63
	}
64
}
65
66
add_action( 'init', array( 'Jetpack_Import_Stats', 'init' ) );
67