Completed
Pull Request — develop (#1227)
by Naveen
02:49
created

Sync_Post_Hooks::shutdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
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_Post_Hooks {
8
9
	/**
10
	 * @var int Post id
11
	 */
12
	public $post_id;
13
14
	/**
15
	 * @var \Wordlift_Log_Service
16
	 */
17
	private $log;
18
19
	/**
20
	 * @var Sync_Service
21
	 */
22
	private $sync_service;
23
24
	/**
25
	 * @var Sync_Object_Adapter_Factory
26
	 */
27
	private $sync_object_factory;
28
29
	/**
30
	 * Sync_Post_Hooks constructor.
31
	 *
32
	 * @param Sync_Service $sync_service
33
	 * @param Sync_Object_Adapter_Factory $sync_object_factory
34
	 */
35
	function __construct( $sync_service, $sync_object_factory ) {
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...
36
37
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
38
39
		$this->sync_service        = $sync_service;
40
		$this->sync_object_factory = $sync_object_factory;
41
42
		$this->register_hooks();
43
	}
44
45
	private function register_hooks() {
46
		/**
47
		 * Register hooks for post and meta.
48
		 */
49
		add_action( 'save_post', array( $this, 'save_post' ) );
50
		add_action( 'added_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
51
		add_action( 'updated_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
52
		add_action( 'deleted_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
53
		add_action( 'delete_post', array( $this, 'delete_post' ) );
54
		/**
55
		 * @todo: Might need to change this, this will be called
56
		 * for every request, sync will occur on editor.
57
		 */
58
		add_action( 'shutdown', array( $this, 'shutdown' ) );
59
60
	}
61
62
	/**
63
	 * @return bool
64
	 */
65
	public function shutdown() {
66
		return $this->sync( $this->post_id );
67
	}
68
69
	public function save_post( $post_id ) {
70
71
		$this->post_id = $post_id;
72
73
	}
74
75
	public function changed_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $meta_key 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...
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...
76
		$this->post_id = $post_id;
77
	}
78
79
	private function sync( $post_id ) {
80
81
		try {
82
			$post = get_post( $post_id );
83
			$this->sync_service->sync_many( array(
84
				$this->sync_object_factory->create( Object_Type_Enum::POST, $post_id ),
85
				$this->sync_object_factory->create( Object_Type_Enum::USER, $post->post_author )
86
			) );
87
		} catch ( \Exception $e ) {
88
			$this->log->error( "An error occurred while trying to sync post $post_id: " . $e->getMessage(), $e );
89
		}
90
91
	}
92
93
	public function delete_post( $post_id ) {
94
95
		try {
96
			$this->sync_service->delete_one( Object_Type_Enum::POST, $post_id );
97
		} catch ( \Exception $e ) {
98
			$this->log->error( "An error occurred while trying to delete post $post_id: " . $e->getMessage(), $e );
99
		}
100
101
	}
102
103
}