Passed
Push — master ( 6176aa...f7c939 )
by Mike
03:08
created

CustomerResponse   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A prepare_response() 0 28 3
1
<?php
2
/**
3
 * Convert a customer object to the product schema format.
4
 *
5
 * @package WooCommerce/RestApi
6
 */
7
8
namespace WooCommerce\RestApi\Controllers\Version4\Responses;
9
10
defined( 'ABSPATH' ) || exit;
11
12
/**
13
 * CustomerResponse class.
14
 */
15
class CustomerResponse extends AbstractObjectResponse {
16
17
	/**
18
	 * Convert object to match data in the schema.
19
	 *
20
	 * @param \WC_Customer $object Product data.
21
	 * @param string       $context Request context. Options: 'view' and 'edit'.
22
	 * @return array
23
	 */
24
	public function prepare_response( $object, $context ) {
25
		$data        = $object->get_data();
26
		$format_date = array( 'date_created', 'date_modified' );
27
28
		// Format date values.
29
		foreach ( $format_date as $key ) {
30
			// Date created is stored UTC, date modified is stored WP local time.
31
			$datetime              = 'date_created' === $key ? get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $data[ $key ]->getTimestamp() ) ) : $data[ $key ];
32
			$data[ $key ]          = wc_rest_prepare_date_response( $datetime, false );
33
			$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
34
		}
35
36
		return array(
37
			'id'                 => $object->get_id(),
38
			'date_created'       => $data['date_created'],
39
			'date_created_gmt'   => $data['date_created_gmt'],
40
			'date_modified'      => $data['date_modified'],
41
			'date_modified_gmt'  => $data['date_modified_gmt'],
42
			'email'              => $data['email'],
43
			'first_name'         => $data['first_name'],
44
			'last_name'          => $data['last_name'],
45
			'role'               => $data['role'],
46
			'username'           => $data['username'],
47
			'billing'            => $data['billing'],
48
			'shipping'           => $data['shipping'],
49
			'is_paying_customer' => $data['is_paying_customer'],
50
			'avatar_url'         => $object->get_avatar_url(),
51
			'meta_data'          => $data['meta_data'],
52
		);
53
	}
54
}
55