Completed
Pull Request — develop (#1350)
by Naveen
03:08
created

Rest_Controller   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 161
Duplicated Lines 62.11 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 100
loc 161
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register_all_routes() 0 10 1
A get_all_videos() 0 7 1
A save_all_videos() 0 18 3
A register_get_all_videos_route() 21 21 2
A register_save_all_videos_route() 27 27 3
A register_get_sync_state_endpoint() 17 17 1
A register_background_process_start_endpoint() 18 18 1
A register_background_process_stop_endpoint() 17 17 1

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 3.31.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Videoobject\Api;
8
9
use Wordlift\Videoobject\Background_Process\Videoobject_Background_Process;
10
use Wordlift\Videoobject\Data\Video_Storage\Video_Storage_Factory;
11
use Wordlift\Videoobject\Data\Video\Video;
12
use WP_REST_Server;
13
14
class Rest_Controller {
15
	/**
16
	 * @var Videoobject_Background_Process
17
	 */
18
	private $background_process;
19
20
	/**
21
	 * Rest_Controller constructor.
22
	 *
23
	 * @param $background_process Videoobject_Background_Process
24
	 */
25
	public function __construct( $background_process ) {
26
		$this->background_process = $background_process;
27
	}
28
29
	public function register_all_routes() {
30
		$that = $this;
31
		add_action( 'rest_api_init', function () use ( $that ) {
32
			$that->register_get_all_videos_route();
33
			$that->register_save_all_videos_route();
34
			$that->register_get_sync_state_endpoint();
35
			$that->register_background_process_start_endpoint();
36
			$that->register_background_process_stop_endpoint();
37
		} );
38
	}
39
40
	public function get_all_videos( $request ) {
41
		$data    = $request->get_params();
42
		$post_id = (int) $data['post_id'];
43
		$storage = Video_Storage_Factory::get_storage();
44
45
		return $storage->get_all_videos( $post_id );
46
	}
47
48
	public function save_all_videos( $request ) {
49
		$data    = $request->get_params();
50
		$post_id = (int) $data['post_id'];
51
		$videos  = (array) $data['videos'];
52
		if ( ! $videos ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $videos of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
			return;
54
		}
55
		$storage = Video_Storage_Factory::get_storage();
56
		$storage->remove_all_videos( $post_id );
57
58
		foreach ( $videos as $video ) {
59
			$video_obj = new Video();
60
			$video_obj->from( (array) $video );
61
			$storage->add_video( $post_id, $video_obj );
62
		}
63
64
65
	}
66
67 View Code Duplication
	private function register_get_all_videos_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...
68
		register_rest_route(
69
			WL_REST_ROUTE_DEFAULT_NAMESPACE,
70
			'/videos',
71
			array(
72
				'methods'             => WP_REST_Server::CREATABLE,
73
				'callback'            => array( $this, 'get_all_videos' ),
74
				'permission_callback' => function () {
75
					return current_user_can( 'manage_options' );
76
				},
77
				'args'                => array(
78
					'post_id' => array(
79
						'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...
80
							return is_numeric( $param ) && $param;
81
						},
82
						'required'          => true,
83
					),
84
				),
85
			)
86
		);
87
	}
88
89 View Code Duplication
	private function register_save_all_videos_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...
90
		register_rest_route(
91
			WL_REST_ROUTE_DEFAULT_NAMESPACE,
92
			'/videos/save',
93
			array(
94
				'methods'             => WP_REST_Server::CREATABLE,
95
				'callback'            => array( $this, 'save_all_videos' ),
96
				'permission_callback' => function () {
97
					return current_user_can( 'manage_options' );
98
				},
99
				'args'                => array(
100
					'post_id' => array(
101
						'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...
102
							return is_numeric( $param ) && $param;
103
						},
104
						'required'          => true,
105
					),
106
					'videos'  => array(
107
						'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...
108
							return is_array( $param ) && $param;
0 ignored issues
show
Bug Best Practice introduced by
The expression $param of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
109
						},
110
						'required'          => true,
111
					),
112
				),
113
			)
114
		);
115
	}
116
117
118 View Code Duplication
	public function register_get_sync_state_endpoint() {
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...
119
		$that = $this;
120
		register_rest_route(
121
			WL_REST_ROUTE_DEFAULT_NAMESPACE,
122
			'/videos/background/get_state',
123
			array(
124
				'methods'             => WP_REST_Server::CREATABLE,
125
				'callback'            => function () use ( $that ) {
126
					return $that->background_process->get_state()->get_array();
127
				},
128
				'permission_callback' => function () {
129
					return current_user_can( 'manage_options' );
130
				},
131
			)
132
		);
133
134
	}
135
136 View Code Duplication
	public function register_background_process_start_endpoint() {
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...
137
		$that = $this;
138
		register_rest_route(
139
			WL_REST_ROUTE_DEFAULT_NAMESPACE,
140
			'/videos/background/start',
141
			array(
142
				'methods'             => WP_REST_Server::CREATABLE,
143
				'callback'            => function () use ( $that ) {
144
					$that->background_process->start();
145
				},
146
				'permission_callback' => function () {
147
					return current_user_can( 'manage_options' );
148
				},
149
			)
150
		);
151
152
153
	}
154
155 View Code Duplication
	public function register_background_process_stop_endpoint() {
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...
156
		$that = $this;
157
		register_rest_route(
158
			WL_REST_ROUTE_DEFAULT_NAMESPACE,
159
			'/videos/background/stop',
160
			array(
161
				'methods'             => WP_REST_Server::CREATABLE,
162
				'callback'            => function () use ( $that ) {
163
					$that->background_process->cancel();
164
				},
165
				'permission_callback' => function () {
166
					return current_user_can( 'manage_options' );
167
				},
168
			)
169
		);
170
171
	}
172
173
174
}