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

Sync_Object_Adapter::get_jsonld()   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 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Dataset;
4
5
use Wordlift\Jsonld\Jsonld_Service;
6
use Wordlift\Object_Type_Enum;
7
8
class Sync_Object_Adapter {
9
10
	const HASH = '_wl_jsonld_hash';
11
12
	private $object_id;
13
14
	private $type;
15
16
	private $jsonld_service;
17
18
	private $get_meta;
19
	private $update_meta;
20
21
	/**
22
	 * Sync_Object_Adapter constructor.
23
	 *
24
	 * @param int $type One of Object_Type_Enum.
25
	 * @param int $object_id A post or term id.
26
	 * @param Jsonld_Service
27
	 *
28
	 * @throws \Exception
29
	 */
30
	function __construct( $type, $object_id, $jsonld_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...
31
32
		$this->type = filter_var( $type, FILTER_VALIDATE_INT, array(
33
			'options' => array(
34
				'min_range' => 0,
35
				'max_range' => 1,
36
			),
37
		) );
38
39
		$this->object_id = filter_var( $object_id, FILTER_VALIDATE_INT );
40
41
		if ( null === $this->type ) {
42
			throw new \Exception( 'Invalid $type.' );
43
		}
44
		if ( null === $this->object_id ) {
45
			throw new \Exception( 'Invalid $object.' );
46
		}
47
48
		$this->jsonld_service = $jsonld_service;
49
50
		if ( Object_Type_Enum::POST === $this->type ) {
51
			$this->get_meta    = 'get_post_meta';
52
			$this->update_meta = 'update_post_meta';
53
		} else {
54
			$this->get_meta    = 'get_term_meta';
55
			$this->update_meta = 'update_term_meta';
56
		}
57
	}
58
59
	function is_changed() {
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...
60
61
		$hash = call_user_func( $this->get_meta, $this->object_id, self::HASH, true );
62
63
		return empty( $hash ) || $hash !== $this->hash( $this->get_jsonld() );
64
	}
65
66
	function get_jsonld() {
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...
67
68
		return apply_filters( 'wl_dataset__sync_service__sync_item__jsonld',
69
			$this->jsonld_service->get( $this->type, $this->object_id ), $this->type, $this->object_id );
70
	}
71
72
	function get_jsonld_and_update_hash() {
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...
73
		$jsonld = $this->get_jsonld();
74
75
		call_user_func( $this->update_meta, $this->object_id, self::HASH, $this->hash( $jsonld ) );
76
77
		return $jsonld;
78
	}
79
80
	private function hash( $jsonld ) {
81
		return sha1( wp_json_encode( $jsonld ) );
82
	}
83
84
}
85