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

Sync_Post_Hooks   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 11.39 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 9
loc 79
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A register_hooks() 0 11 1
A save_post() 0 5 1
A changed_post_meta() 0 13 2
A sync() 0 9 2
A delete_post() 9 9 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_Post_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_Post_Hooks constructor.
20
	 *
21
	 * @param Sync_Service $sync_service
22
	 */
23
	function __construct( $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 post and meta.
36
		 */
37
		add_action( 'save_post', array( $this, 'save_post' ) );
38
		add_action( 'added_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
39
		add_action( 'updated_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
40
		add_action( 'deleted_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
41
		add_action( 'delete_post', array( $this, 'delete_post' ) );
42
43
	}
44
45
	public function save_post( $post_id ) {
46
47
		$this->sync( $post_id );
48
49
	}
50
51
	public function changed_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $_meta_value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
53
		if ( in_array( $meta_key, apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys', array(
54
			'_pingme',
55
			'_encloseme',
56
			'entity_url',
57
		) ) ) ) {
58
			return;
59
		}
60
61
		$this->sync( $post_id );
62
63
	}
64
65
	private function sync( $post_id ) {
66
67
		try {
68
			$this->sync_service->sync_one( Object_Type_Enum::POST, $post_id );
69
		} catch ( \Exception $e ) {
70
			$this->log->error( "An error occurred while trying to sync post $post_id: " . $e->getMessage(), $e );
71
		}
72
73
	}
74
75 View Code Duplication
	public function delete_post( $post_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...
76
77
		try {
78
			$this->sync_service->delete_one( Object_Type_Enum::POST, $post_id );
79
		} catch ( \Exception $e ) {
80
			$this->log->error( "An error occurred while trying to delete post $post_id: " . $e->getMessage(), $e );
81
		}
82
83
	}
84
85
}