Completed
Push — master ( 31131b...6176aa )
by Mike
15:27
created

AbstractObjectRequest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_param() 0 2 2
A __construct() 0 2 1
A set_meta_data() 0 6 4
1
<?php
2
/**
3
 * Convert data in the product schema format to an object.
4
 *
5
 * @package WooCommerce/RestApi
6
 */
7
8
namespace WooCommerce\RestApi\Controllers\Version4\Requests;
9
10
defined( 'ABSPATH' ) || exit;
11
12
/**
13
 * AbstractObjectRequest class.
14
 */
15
abstract class AbstractObjectRequest {
16
17
	/**
18
	 * Request data.
19
	 *
20
	 * @var array Array of request data.
21
	 */
22
	protected $request;
23
24
	/**
25
	 * Constructor. Takes an existing or blank object and updates values based on the requset.
26
	 *
27
	 * @param \WP_REST_Request $request Request data.
28
	 */
29
	public function __construct( $request ) {
30
		$this->request = (array) $request->get_params();
31
	}
32
33
	/**
34
	 * Get param from request.
35
	 *
36
	 * @param string $name Param name.
37
	 * @param mixed  $default Default to return if not set.
38
	 */
39
	protected function get_param( $name, $default = null ) {
40
		return isset( $this->request[ $name ] ) ? $this->request[ $name ] : $default;
41
	}
42
43
	/**
44
	 * Convert request to object.
45
	 *
46
	 * @throws \WC_REST_Exception Will throw an exception if the resulting product object is invalid.
47
	 */
48
	abstract public function prepare_object();
49
50
	/**
51
	 * Set meta data.
52
	 *
53
	 * @param mixed $object Product object reference.
54
	 */
55
	protected function set_meta_data( &$object ) {
56
		$meta_data = $this->get_param( 'meta_data', null );
57
58
		if ( ! is_null( $meta_data ) ) {
59
			foreach ( $meta_data as $meta ) {
60
				$object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
61
			}
62
		}
63
	}
64
}
65