Completed
Push — develop ( 1d2391...a94b56 )
by David
02:55 queued 11s
created

Sync_Wpjson_Endpoint::rest_api_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 42
rs 9.248
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Dataset;
4
5
use WP_REST_Server;
6
7
class Sync_Wpjson_Endpoint {
8
9
	/**
10
	 * @var Sync_Service
11
	 */
12
	private $sync_service;
13
14
	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...
15
16
		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
17
		$this->sync_service = $sync_service;
18
	}
19
20
	function rest_api_init() {
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...
21
22
		// register_rest_route() handles more arguments but we are going to stick to the basics for now.
23
		register_rest_route( 'wordlift/v1', '/dataset/count', array(
24
			// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
25
			'methods'  => WP_REST_Server::READABLE,
26
			// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
27
			'callback' => array( $this, 'count' ),
28
		) );
29
30
		register_rest_route( 'wordlift/v1', '/dataset/next', array(
31
			// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
32
			'methods'  => WP_REST_Server::READABLE,
33
			// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
34
			'callback' => array( $this, 'next' ),
35
		) );
36
37
		register_rest_route( 'wordlift/v1', '/dataset/info', array(
38
			// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
39
			'methods'  => WP_REST_Server::READABLE,
40
			// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
41
			'callback' => array( $this, 'info' ),
42
		) );
43
44
		// register_rest_route() handles more arguments but we are going to stick to the basics for now.
45
		register_rest_route( 'wordlift/v1', '/dataset/sync', array(
46
			// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
47
			'methods'  => WP_REST_Server::CREATABLE,
48
			// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
49
			'callback' => array( $this, 'post__dataset__sync' ),
50
		) );
51
52
		// register_rest_route() handles more arguments but we are going to stick to the basics for now.
53
		register_rest_route( 'wordlift/v1', '/dataset/sync', array(
54
			// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
55
			'methods'  => WP_REST_Server::DELETABLE,
56
			// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
57
			'callback' => array( $this, 'delete__dataset__sync' ),
58
		) );
59
60
61
	}
62
63
	function count() {
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...
64
		return rest_ensure_response( $this->sync_service->count() );
65
	}
66
67
	function next() {
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...
68
		return rest_ensure_response( $this->sync_service->next() );
69
	}
70
71
	function post__dataset__sync() {
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...
72
73
		$this->sync_service->start();
74
75
	}
76
77
	function delete__dataset__sync() {
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...
78
79
		$this->sync_service->request_cancel();
80
81
	}
82
83
	function info() {
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...
84
		return rest_ensure_response( $this->sync_service->info() );
85
	}
86
87
}
88