Completed
Push — master ( ccd1f4...dac471 )
by Claudio
07:15
created

WC_REST_Controller   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 160
Duplicated Lines 31.88 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 51
loc 160
rs 10
wmc 18
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_normalized_rest_base() 0 3 1
B check_batch_limit() 3 22 5
C batch_items() 48 67 11
B get_public_batch_schema() 0 26 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
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Abstract Rest Controler Class
9
 *
10
 * @author   WooThemes
11
 * @category API
12
 * @package  WooCommerce/Abstracts
13
 * @extends  WC_REST_Controller
14
 * @version  2.6.0
15
 */
16
abstract class WC_REST_Controller extends WP_REST_Controller {
17
18
	/**
19
	 * Endpoint namespace.
20
	 *
21
	 * @var string
22
	 */
23
	protected $namespace = 'wc/v1';
24
25
	/**
26
	 * Route base.
27
	 *
28
	 * @var string
29
	 */
30
	protected $rest_base = '';
31
32
	/**
33
	 * Get normalized rest base.
34
	 *
35
	 * @return string
36
	 */
37
	protected function get_normalized_rest_base() {
38
		return preg_replace( '/\(.*\)\//i', '', $this->rest_base );
39
	}
40
41
	/**
42
	 * Check batch limit.
43
	 *
44
	 * @param array $items Request items.
45
	 * @return bool|WP_Error
46
	 */
47
	protected function check_batch_limit( $items ) {
48
		$limit = apply_filters( 'woocommerce_rest_batch_items_limit', 100, $this->get_normalized_rest_base() );
49
		$total = 0;
50
51
		if ( ! empty( $items['create'] ) ) {
52
			$total += count( $items['create'] );
53
		}
54
55
		if ( ! empty( $items['update'] ) ) {
56
			$total += count( $items['update'] );
57
		}
58
59
		if ( ! empty( $items['delete'] ) ) {
60
			$total += count( $items['delete'] );
61
		}
62
63 View Code Duplication
		if ( $total > $limit ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64
			return new WP_Error( 'woocommerce_rest_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), array( 'status' => 413 ) );
65
		}
66
67
		return true;
68
	}
69
70
	/**
71
	 * Bulk create, update and delete items.
72
	 *
73
	 * @param WP_REST_Request $request Full details about the request.
74
	 * @return array Of WP_Error or WP_REST_Response.
75
	 */
76
	public function batch_items( $request ) {
77
		/** @var WP_REST_Server $wp_rest_server */
78
		global $wp_rest_server;
79
80
		// Get the request params.
81
		$items    = array_filter( $request->get_params() );
82
		$response = array();
83
84
		// Check batch limit.
85
		$limit = $this->check_batch_limit( $items );
86
		if ( is_wp_error( $limit ) ) {
87
			return $limit;
88
		}
89
90 View Code Duplication
		if ( ! empty( $items['create'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
			foreach ( $items['create'] as $item ) {
92
				$_item = new WP_REST_Request( 'POST' );
93
				$_item->set_body_params( $item );
94
				$_response = $this->create_item( $_item );
95
96
				if ( is_wp_error( $_response ) ) {
97
					$response['create'][] = array(
98
						'id'    => 0,
99
						'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ),
100
					);
101
				} else {
102
					$response['create'][] = $wp_rest_server->response_to_data( $_response, '' );
103
				}
104
			}
105
		}
106
107 View Code Duplication
		if ( ! empty( $items['update'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
108
			foreach ( $items['update'] as $item ) {
109
				$_item = new WP_REST_Request( 'PUT' );
110
				$_item->set_body_params( $item );
111
				$_response = $this->update_item( $_item );
112
113
				if ( is_wp_error( $_response ) ) {
114
					$response['update'][] = array(
115
						'id'    => $item['id'],
116
						'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ),
117
					);
118
				} else {
119
					$response['update'][] = $wp_rest_server->response_to_data( $_response, '' );
120
				}
121
			}
122
		}
123
124 View Code Duplication
		if ( ! empty( $items['delete'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
125
			foreach ( $items['delete'] as $id ) {
126
				$_item = new WP_REST_Request( 'DELETE' );
127
				$_item->set_query_params( array( 'id' => $id, 'force' => true ) );
128
				$_response = $this->delete_item( $_item );
129
130
				if ( is_wp_error( $_response ) ) {
131
					$response['delete'][] = array(
132
						'id'    => $id,
133
						'error' => array( 'code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data() ),
134
					);
135
				} else {
136
					$response['delete'][] = $wp_rest_server->response_to_data( $_response, '' );
137
				}
138
			}
139
		}
140
141
		return $response;
142
	}
143
144
	/**
145
	 * Get the batch schema, conforming to JSON Schema.
146
	 *
147
	 * @return array
148
	 */
149
	public function get_public_batch_schema() {
150
		$schema = array(
151
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
152
			'title'      => 'batch',
153
			'type'       => 'object',
154
			'properties' => array(
155
				'create' => array(
156
					'description' => __( 'List of created resources.', 'woocommerce' ),
157
					'type'        => 'array',
158
					'context'     => array( 'view', 'edit' ),
159
				),
160
				'update' => array(
161
					'description' => __( 'List of updated resources.', 'woocommerce' ),
162
					'type'        => 'array',
163
					'context'     => array( 'view', 'edit' ),
164
				),
165
				'delete' => array(
166
					'description' => __( 'List of delete resources.', 'woocommerce' ),
167
					'type'        => 'array',
168
					'context'     => array( 'view', 'edit' ),
169
				),
170
			),
171
		);
172
173
		return $schema;
174
	}
175
}
176