Completed
Push — develop ( c1be74...f8bbf2 )
by David
06:55
created

Sync_User_Hooks::changed_user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Dataset;
4
5
use Wordlift\Object_Type_Enum;
6
7
class Sync_User_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
	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...
24
25
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
26
27
		$this->sync_service = $sync_service;
28
29
		$this->register_hooks();
30
31
	}
32
33
	private function register_hooks() {
34
		/**
35
		 * Register hooks for user and meta.
36
		 */
37
		add_action( 'user_register', array( $this, 'changed_user' ) );
38
		add_action( 'profile_update', array( $this, 'changed_user' ) );
39
		add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
40
		add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
41
		add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
42
		add_action( 'delete_user', array( $this, 'delete_user' ) );
43
44
	}
45
46
	public function changed_user( $user_id ) {
47
48
		$this->sync( $user_id );
49
50
	}
51
52
	public function changed_user_meta( $meta_id, $user_id, $meta_key ) {
53
54
		if ( in_array( $meta_key, apply_filters( 'wl_dataset__sync_user_hooks__ignored_meta_keys', array(
55
			'rich_editing',
56
			'comment_shortcuts',
57
			'admin_color',
58
			'use_ssl',
59
			'show_admin_bar_front',
60
			'wptests_capabilities',
61
			'wptests_user_level',
62
			'dismissed_wp_pointers',
63
			'entity_url',
64
		) ) ) ) {
65
			return;
66
		}
67
68
		$this->sync( $user_id );
69
70
	}
71
72 View Code Duplication
	private function 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...
73
74
		try {
75
			$this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id );
76
		} catch ( \Exception $e ) {
77
			$this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e );
78
		}
79
80
	}
81
82 View Code Duplication
	public function delete_user( $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...
83
84
		try {
85
			$this->sync_service->delete_one( Object_Type_Enum::USER, $user_id );
86
		} catch ( \Exception $e ) {
87
			$this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e );
88
		}
89
90
	}
91
92
}