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

Rest_Controller::save_all_videos()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
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\Data\Video_Storage\Video_Storage_Factory;
10
use Wordlift\Videoobject\Data\Video\Video;
11
use WP_REST_Server;
12
13
class Rest_Controller {
14
15
	public function register_all_routes() {
16
		$that = $this;
17
		add_action( 'rest_api_init', function () use ( $that ) {
18
			$that->register_get_all_videos_route();
19
			$that->register_save_all_videos_route();
20
		} );
21
	}
22
23
	public function get_all_videos( $request ) {
24
		$data    = $request->get_params();
25
		$post_id = (int) $data['post_id'];
26
		$storage = Video_Storage_Factory::get_storage();
27
28
		return $storage->get_all_videos( $post_id );
29
	}
30
31
	public function save_all_videos( $request ) {
32
		$data    = $request->get_params();
33
		$post_id = (int) $data['post_id'];
34
		$videos  = (array) $data['videos'];
35
		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...
36
			return;
37
		}
38
		$storage = Video_Storage_Factory::get_storage();
39
		$storage->remove_all_videos( $post_id );
40
41
		foreach ( $videos as $video ) {
42
			$video_obj = new Video();
43
			$video_obj->from( (array) $video );
44
			$storage->add_video( $post_id, $video_obj );
45
		}
46
47
48
	}
49
50 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...
51
		register_rest_route(
52
			WL_REST_ROUTE_DEFAULT_NAMESPACE,
53
			'/videos',
54
			array(
55
				'methods'             => WP_REST_Server::CREATABLE,
56
				'callback'            => array( $this, 'get_all_videos' ),
57
				'permission_callback' => function () {
58
					return current_user_can( 'manage_options' );
59
				},
60
				'args'                => array(
61
					'post_id' => array(
62
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
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...
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...
63
							return is_numeric( $param ) && $param;
64
						},
65
						'required'          => true,
66
					),
67
				),
68
			)
69
		);
70
	}
71
72 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...
73
		register_rest_route(
74
			WL_REST_ROUTE_DEFAULT_NAMESPACE,
75
			'/videos/save',
76
			array(
77
				'methods'             => WP_REST_Server::CREATABLE,
78
				'callback'            => array( $this, 'save_all_videos' ),
79
				'permission_callback' => function () {
80
					return current_user_can( 'manage_options' );
81
				},
82
				'args'                => array(
83
					'post_id' => array(
84
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
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...
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...
85
							return is_numeric( $param ) && $param;
86
						},
87
						'required'          => true,
88
					),
89
					'videos'  => array(
90
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
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...
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...
91
							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...
92
						},
93
						'required'          => true,
94
					),
95
				),
96
			)
97
		);
98
	}
99
100
101
}