Completed
Pull Request — develop (#1328)
by Naveen
03:47
created

Entity_Rest_Endpoint   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 150
Duplicated Lines 41.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 62
loc 150
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
A register_routes() 0 10 1
A accept_entity() 10 10 1
A undo() 0 9 1
A mark_as_no_match() 0 6 1
A register_undo_route() 21 21 2
A register_accept_route() 0 4 1
A register_reject_route() 0 4 1
A reject_entity() 8 8 1
A register_nomatch_route() 21 21 2
A register_entity_accept_or_reject_route() 0 29 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
 * @since 1.0.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Vocabulary\Api;
8
9
use Wordlift\Vocabulary\Data\Entity_List\Entity_List_Factory;
10
use WP_REST_Server;
11
12
/**
13
 * @since 1.0.0
14
 * @author Naveen Muthusamy <[email protected]>
15
 */
16
class Entity_Rest_Endpoint {
17
18
	const SAME_AS_META_KEY = 'entity_same_as';
19
	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
20
	const DESCRIPTION_META_KEY = 'entity_description';
21
	const TYPE_META_KEY = 'entity_type';
22
	const EXTERNAL_ENTITY_META_KEY = '_wl_is_external';
23
	const IGNORE_TAG_FROM_LISTING = '_wl_cmkg_ignore_tag_from_ui';
24
25
26
	public function register_routes() {
27
		$that = $this;
28
		add_action( 'rest_api_init',
29
			function () use ( $that ) {
30
				$that->register_accept_route();
31
				$that->register_undo_route();
32
				$that->register_nomatch_route();
33
				$that->register_reject_route();
34
			} );
35
	}
36
37
38 View Code Duplication
	public function accept_entity( $request ) {
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...
39
		$data        = $request->get_params();
40
		$term_id     = (int) $data['term_id'];
41
		$entity_data = (array) $data['entity'];
42
		$entity      = Entity_List_Factory::get_instance( $term_id );
43
		$entity->save_jsonld_data( $entity_data );
44
		update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 );
45
46
		return $term_id;
47
	}
48
49
	public function undo( $request ) {
50
		$data    = $request->get_params();
51
		$term_id = (int) $data['term_id'];
52
		$entity      = Entity_List_Factory::get_instance( $term_id );
53
		$entity->clear_data();
54
		delete_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING );
55
56
		return $term_id;
57
	}
58
59
60
	public function mark_as_no_match( $request ) {
61
		$data    = $request->get_params();
62
		$term_id = (int) $data['term_id'];
63
64
		return add_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 );
65
	}
66
67
68 View Code Duplication
	private function register_undo_route() {
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...
69
		register_rest_route(
70
			Api_Config::REST_NAMESPACE,
71
			'/entity/undo',
72
			array(
73
				'methods'             => WP_REST_Server::CREATABLE,
74
				'callback'            => array( $this, 'undo' ),
75
				'permission_callback' => function () {
76
					return current_user_can( 'manage_options' );
77
				},
78
				'args'                => array(
79
					'term_id' => array(
80
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $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 $request 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...
81
							return is_numeric( $param ) && $param;
82
						},
83
						'required'          => true,
84
					),
85
				),
86
			)
87
		);
88
	}
89
90
	private function register_accept_route() {
91
		$route = '/entity/accept';
92
		$this->register_entity_accept_or_reject_route( $route, array( $this, 'accept_entity' ) );
93
	}
94
95
	private function register_reject_route() {
96
		$route = '/entity/reject';
97
		$this->register_entity_accept_or_reject_route( $route, array( $this, 'reject_entity' ) );
98
	}
99
100 View Code Duplication
	public function reject_entity( $request ) {
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...
101
		$data        = $request->get_params();
102
		$term_id     = (int) $data['term_id'];
103
		$entity_data = (array) $data['entity'];
104
		$entity      = Entity_List_Factory::get_instance( $term_id );
105
		$entity->remove_entity_by_id( $entity_data['@id'] );
106
		return $term_id;
107
	}
108
109 View Code Duplication
	private function register_nomatch_route() {
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...
110
		register_rest_route(
111
			Api_Config::REST_NAMESPACE,
112
			'/entity/no_match',
113
			array(
114
				'methods'             => WP_REST_Server::CREATABLE,
115
				'callback'            => array( $this, 'mark_as_no_match' ),
116
				'permission_callback' => function () {
117
					return current_user_can( 'manage_options' );
118
				},
119
				'args'                => array(
120
					'term_id' => array(
121
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $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 $request 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...
122
							return is_numeric( $param ) && $param;
123
						},
124
						'required'          => true,
125
					),
126
				),
127
			)
128
		);
129
	}
130
131
132
	/**
133
	 * @param $accept_route
134
	 */
135
	private function register_entity_accept_or_reject_route( $accept_route, $callback ) {
136
137
		register_rest_route(
138
			Api_Config::REST_NAMESPACE,
139
			$accept_route,
140
			array(
141
				'methods'             => WP_REST_Server::CREATABLE,
142
				'callback'            => $callback,
143
				'permission_callback' => function () {
144
					return current_user_can( 'manage_options' );
145
				},
146
				'args'                => array(
147
					'term_id' => array(
148
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $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 $request 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...
149
							return is_numeric( $param ) && $param;
150
						},
151
						'required'          => true,
152
					),
153
					'entity'  => array(
154
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $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 $request 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...
155
							return is_array( $param );
156
						},
157
						'required'          => true,
158
					),
159
				),
160
			)
161
		);
162
163
	}
164
165
}