Passed
Push — master ( c5db5f...2dc661 )
by Brian
07:13 queued 26s
created

GetPaid_REST_Report_Invoice_Counts_Controller   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 99
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_reports() 0 20 3
A prepare_item_for_response() 0 19 2
A get_item_schema() 0 28 1
1
<?php
2
/**
3
 * REST API invoice counts controller
4
 *
5
 * Handles requests to the reports/invoice/counts endpoint.
6
 *
7
 * @package GetPaid
8
 * @subpackage REST API
9
 * @since   2.0.0
10
 */
11
12
defined( 'ABSPATH' ) || exit;
13
14
/**
15
 * GetPaid REST invoice counts controller class.
16
 *
17
 * @package GetPaid
18
 */
19
class GetPaid_REST_Report_Invoice_Counts_Controller extends GetPaid_REST_Reports_Controller {
20
21
	/**
22
	 * Route base.
23
	 *
24
	 * @var string
25
	 */
26
	protected $rest_base = 'reports/invoices/counts';
27
28
	/**
29
	 * Prepare a report object for serialization.
30
	 *
31
	 * @param  stdClass        $report Report data.
32
	 * @param  WP_REST_Request $request Request object.
33
	 * @return WP_REST_Response $response Response data.
34
	 */
35
	public function prepare_item_for_response( $report, $request ) {
36
37
		$data    = (array) $report;
38
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
39
		$data    = $this->add_additional_fields_to_object( $data, $request );
40
		$data    = $this->filter_response_by_context( $data, $context );
41
42
		// Wrap the data in a response object.
43
		$response = rest_ensure_response( $data );
44
45
		$response->add_links(
46
			array(
47
				'about' => array(
48
					'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ),
49
				),
50
			)
51
		);
52
53
		return apply_filters( 'getpaid_rest_prepare_report_invoices_count', $response, $report, $request );
54
	}
55
56
	/**
57
	 * Get reports list.
58
	 *
59
	 * @since 2.0.0
60
	 * @return array
61
	 */
62
	protected function get_reports() {
63
64
		$counts = wp_count_posts( 'wpi_invoice' );
65
		$data   = array();
66
67
		foreach ( wpinv_get_invoice_statuses() as $slug => $name ) {
68
69
			if ( ! isset( $counts->$slug ) ) {
70
				continue;
71
			}
72
73
			$data[] = array(
74
				'slug'  => $slug,
75
				'name'  => $name,
76
				'count' => (int) $counts->$slug,
77
			);
78
79
		}
80
81
		return $data;
82
83
	}
84
85
	/**
86
	 * Get the Report's schema, conforming to JSON Schema.
87
	 *
88
	 * @return array
89
	 */
90
	public function get_item_schema() {
91
		$schema = array(
92
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
93
			'title'      => 'report_invoice_counts',
94
			'type'       => 'object',
95
			'properties' => array(
96
				'slug'  => array(
97
					'description' => __( 'An alphanumeric identifier for the resource.', 'invoicing' ),
98
					'type'        => 'string',
99
					'context'     => array( 'view' ),
100
					'readonly'    => true,
101
				),
102
				'name'  => array(
103
					'description' => __( 'Invoice status name.', 'invoicing' ),
104
					'type'        => 'string',
105
					'context'     => array( 'view' ),
106
					'readonly'    => true,
107
				),
108
				'count' => array(
109
					'description' => __( 'Number of invoices.', 'invoicing' ),
110
					'type'        => 'string',
111
					'context'     => array( 'view' ),
112
					'readonly'    => true,
113
				),
114
			),
115
		);
116
117
		return $this->add_additional_fields_schema( $schema );
118
	}
119
}
120