Completed
Pull Request — develop (#1233)
by
unknown
02:52
created

Sync_User_Hooks   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 28.87 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 28
loc 97
rs 10
c 0
b 0
f 0
wmc 11
lcom 2
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A register_hooks() 0 12 1
A changed_user() 0 5 1
A changed_user_meta() 0 22 2
A sync() 0 3 1
A do_sync() 9 9 2
A delete_user() 0 3 1
A do_delete() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Wordlift\Dataset;
4
5
use Wordlift\Object_Type_Enum;
6
7
class Sync_User_Hooks extends Abstract_Sync_Hooks {
8
	/**
9
	 * @var \Wordlift_Log_Service
10
	 */
11
	private $log;
12
13
	/**
14
	 * @var Sync_Service
15
	 */
16
	private $sync_service;
17
18
	/**
19
	 * Sync_User_Hooks constructor.
20
	 *
21
	 * @param Sync_Service $sync_service
22
	 */
23 View Code Duplication
	function __construct( Sync_Service $sync_service ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
		parent::__construct();
25
26
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
27
28
		$this->sync_service = $sync_service;
29
30
		$this->register_hooks();
31
32
	}
33
34
	private function register_hooks() {
35
		/**
36
		 * Register hooks for user and meta.
37
		 */
38
		add_action( 'user_register', array( $this, 'changed_user' ) );
39
		add_action( 'profile_update', array( $this, 'changed_user' ) );
40
		add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
41
		add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
42
		add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
43
		add_action( 'delete_user', array( $this, 'delete_user' ) );
44
45
	}
46
47
	public function changed_user( $user_id ) {
48
49
		$this->sync( $user_id );
50
51
	}
52
53
	public function changed_user_meta( $meta_id, $user_id, $meta_key ) {
54
55
		if ( in_array( $meta_key,
56
			apply_filters( 'wl_dataset__sync_user_hooks__ignored_meta_keys',
57
				apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys',
58
					array(
59
						'rich_editing',
60
						'comment_shortcuts',
61
						'admin_color',
62
						'use_ssl',
63
						'show_admin_bar_front',
64
						'wptests_capabilities',
65
						'wptests_user_level',
66
						'dismissed_wp_pointers',
67
						'entity_url',
68
					) ) ) ) ) {
69
			return;
70
		}
71
72
		$this->sync( $user_id );
73
74
	}
75
76
	private function sync( $user_id ) {
77
		$this->enqueue( array( 'do_sync', $user_id ) );
78
	}
79
80 View Code Duplication
	public function do_sync( $user_id ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
82
		try {
83
			$this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id );
84
		} catch ( \Exception $e ) {
85
			$this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e );
86
		}
87
88
	}
89
90
	public function delete_user( $user_id ) {
91
		$this->enqueue( array( 'do_delete', $user_id ) );
92
	}
93
94 View Code Duplication
	public function do_delete( $user_id ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
		try {
96
			$this->sync_service->delete_one( Object_Type_Enum::USER, $user_id );
97
		} catch ( \Exception $e ) {
98
			$this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e );
99
		}
100
101
	}
102
103
}