Completed
Pull Request — master (#11176)
by Jeff
07:52
created

WC_REST_Shipping_Zones_Controller_Base::get_zone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 9.6666
1
<?php
2
/**
3
 * REST API Shipping Zones Controller base
4
 *
5
 * Houses common functionality between Shipping Zones and Locations.
6
 *
7
 * @author   WooThemes
8
 * @category API
9
 * @package  WooCommerce/API
10
 * @since    2.7.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * REST API Shipping Zones base class.
19
 *
20
 * @package WooCommerce/API
21
 * @extends WC_REST_Controller
22
 */
23
abstract class WC_REST_Shipping_Zones_Controller_Base extends WC_REST_Controller {
24
25
	/**
26
	 * Endpoint namespace.
27
	 *
28
	 * @var string
29
	 */
30
	protected $namespace = 'wc/v1';
31
32
	/**
33
	 * Route base.
34
	 *
35
	 * @var string
36
	 */
37
	protected $rest_base = 'shipping/zones';
38
39
	/**
40
	 * Retrieve a Shipping Zone by it's ID.
41
	 *
42
	 * @param int $zone_id Shipping Zone ID.
43
	 * @return WC_Shipping_Zone|WP_Error
44
	 */
45
	protected function get_zone( $zone_id ) {
46
		$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id );
47
48
		if ( false === $zone ) {
49
			return new WP_Error( 'woocommerce_rest_shipping_zone_invalid', __( "Resource doesn't exist.", 'woocommerce' ), array( 'status' => 404 ) );
50
		}
51
52
		return $zone;
53
	}
54
55
	/**
56
	 * Check whether a given request has permission to read Shipping Zones.
57
	 *
58
	 * @param  WP_REST_Request $request Full details about the request.
59
	 * @return WP_Error|boolean
60
	 */
61 View Code Duplication
	public function get_items_permissions_check( $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...
62
		if ( ! wc_shipping_enabled() ) {
63
			return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.' ), array( 'status' => 404 ) );
64
		}
65
66
		if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
67
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
68
		}
69
70
		return true;
71
	}
72
73
	/**
74
	 * Check whether a given request has permission to edit Shipping Zones.
75
	 *
76
	 * @param  WP_REST_Request $request Full details about the request.
77
	 * @return WP_Error|boolean
78
	 */
79 View Code Duplication
	public function update_items_permissions_check( $request ) {
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...
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...
80
		if ( ! wc_shipping_enabled() ) {
81
			return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.' ), array( 'status' => 404 ) );
82
		}
83
84
		if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
85
			return new WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot update resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
86
		}
87
88
		return true;
89
	}
90
}