Completed
Pull Request — trunk (#541)
by Justin
07:44
created

CMB2_REST_Controller_Boxes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 28 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 7
lcom 1
cbo 3
dl 21
loc 75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register_routes() 21 21 1
A get_boxes() 0 17 3
A get_box() 0 11 3

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
 * Creates CMB2 objects/fields endpoint for WordPres REST API.
4
 * Allows access to fields registered to a specific post type and more.
5
 *
6
 * @todo  Add better documentation.
7
 * @todo  Research proper schema.
8
 *
9
 * @since 2.2.0
10
 *
11
 * @category  WordPress_Plugin
12
 * @package   CMB2
13
 * @author    WebDevStudios
14
 * @license   GPL-2.0+
15
 * @link      http://webdevstudios.com
16
 */
17
class CMB2_REST_Controller_Boxes extends CMB2_REST_Controller {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
19
	/**
20
	 * Register the routes for the objects of the controller.
21
	 *
22
	 * @since 2.2.0
23
	 */
24 View Code Duplication
	public function register_routes() {
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...
25
		// Returns all boxes data.
26
		register_rest_route( CMB2_REST::BASE, '/boxes/', array(
27
			array(
28
				'methods'         => WP_REST_Server::READABLE,
29
				'callback'        => array( $this, 'get_boxes' ),
30
				'permission_callback' => array( $this, 'get_item_permissions_check' ),
31
			),
32
			'schema' => array( $this, 'get_item_schema' ),
33
		) );
34
35
		// Returns specific box's data.
36
		register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)', array(
37
			array(
38
				'methods'         => WP_REST_Server::READABLE,
39
				'callback'        => array( $this, 'get_box' ),
40
				'permission_callback' => array( $this, 'get_item_permissions_check' ),
41
			),
42
			'schema' => array( $this, 'get_item_schema' ),
43
		) );
44
	}
45
46
	/**
47
	 * Get all public fields
48
	 *
49
	 * @since 2.2.0
50
	 *
51
	 * @param WP_REST_Request $request The API request object.
52
	 * @return array
53
	 */
54
	public function get_boxes( $request ) {
55
		$this->initiate_request( $request );
56
57
		$boxes = CMB2_Boxes::get_by_property( 'show_in_rest', false );
58
59
		if ( empty( $boxes ) ) {
60
			return $this->prepare_item( array( 'error' => __( 'No boxes found.', 'cmb2' ) ), $this->request );
61
		}
62
63
		$boxes_data = array();
64
		// Loop boxes and get specific field.
65
		foreach ( $boxes as $key => $cmb ) {
66
			$boxes_data[ $cmb->cmb_id ] = $this->get_rest_box( $cmb );
67
		}
68
69
		return $this->prepare_item( $boxes_data );
70
	}
71
72
	/**
73
	 * Get all public fields
74
	 *
75
	 * @since 2.2.0
76
	 *
77
	 * @param WP_REST_Request $request The API request object.
78
	 * @return array
79
	 */
80
	public function get_box( $request ) {
81
		$this->initiate_request( $request );
82
83
		$cmb_id = $this->request->get_param( 'cmb_id' );
84
85
		if ( $cmb_id && ( $cmb = cmb2_get_metabox( $cmb_id, $this->object_id, $this->object_type ) ) ) {
86
			return $this->prepare_item( $this->get_rest_box( $cmb ) );
87
		}
88
89
		return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) );
90
	}
91
}
92