Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.66% covered (warning)
89.66%
26 / 29
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Bookings_Over_Time_Controller
89.29% covered (warning)
89.29%
25 / 28
87.50% covered (warning)
87.50%
7 / 8
9.10
0.00% covered (danger)
0.00%
0 / 1
 get_report_key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_report_label
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_data_endpoint
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_column_headers
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 get_default_values
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 format_row_for_csv
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 get_fields
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get_additional_params
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * REST API Reports Bookings Over Time controller class.
4 *
5 * @package Automattic\Jetpack\PremiumAnalytics\Reports\Export\Exports
6 */
7
8declare( strict_types=1 );
9
10namespace Automattic\Jetpack\PremiumAnalytics\Reports\Export\Exports;
11
12defined( 'ABSPATH' ) || exit;
13
14use Automattic\Jetpack\PremiumAnalytics\Reports\Export\Abstract_Csv_Report_Controller;
15
16/**
17 * Bookings Over Time CSV Export Controller.
18 *
19 * Handles CSV exports for the Bookings Over Time report, supporting both
20 * single interval and comparison interval data.
21 *
22 * This controller automatically filters for booking product types:
23 * 'booking', 'bookable-event', and 'bookable-service'.
24 *
25 * @since 0.1.0
26 */
27class Bookings_Over_Time_Controller extends Abstract_Csv_Report_Controller {
28
29    /**
30     * Get the report key for this controller.
31     *
32     * @return string The report key.
33     */
34    public function get_report_key(): string {
35        return 'bookingsovertime';
36    }
37
38    /**
39     * Get the report label for this controller.
40     *
41     * @return string The report label.
42     */
43    public function get_report_label(): string {
44        return __( 'Bookings Over Time', 'jetpack-premium-analytics-pkg' );
45    }
46
47    /**
48     * Get the data endpoint for this controller.
49     *
50     * @return string The data endpoint.
51     */
52    public function get_data_endpoint(): string {
53        return 'reports/orders-by-product-type/by-date';
54    }
55
56    /**
57     * Get the column headers for this controller.
58     *
59     * @param string|null $interval Optional time interval for dynamic headers.
60     * @return array The column headers.
61     */
62    public function get_column_headers( ?string $interval = null ): array {
63        return array(
64            'time_interval' => $this->get_interval_label( $interval ),
65            'orders_no'     => __( 'Bookings created', 'jetpack-premium-analytics-pkg' ),
66        );
67    }
68
69    /**
70     * Get default values for missing data fields.
71     *
72     * @return array Array of field_name => default_value pairs.
73     */
74    public function get_default_values(): array {
75        return array(
76            'orders_no' => 0,
77        );
78    }
79
80    /**
81     * Format a row for CSV export.
82     *
83     * @param array       $item     The row data.
84     * @param string|null $interval Optional time interval for formatting.
85     * @return array The formatted row.
86     */
87    public function format_row_for_csv( array $item, ?string $interval = null ): array {
88        $defaults = $this->get_default_values();
89        return array(
90            'time_interval' => $this->format_time_interval( $item, $interval ),
91            'orders_no'     => isset( $item['orders_no'] ) ? (int) $item['orders_no'] : $defaults['orders_no'],
92        );
93    }
94
95    /**
96     * Get the list of API fields needed for this report.
97     *
98     * @return array
99     */
100    public function get_fields(): array {
101        // time_interval, date_start, and date_end are always returned by the
102        // API for time-series endpoints and do not need to be requested.
103        return array(
104            'orders_no',
105        );
106    }
107
108    /**
109     * Get additional request parameters for data fetching.
110     *
111     * Automatically filters for booking product types.
112     *
113     * @return array Additional parameters to include in data requests.
114     */
115    public function get_additional_params(): array {
116        return array(
117            'date_type' => self::DEFAULT_DATE_TYPE,
118            'filters'   => array(
119                array(
120                    'key'     => 'product_type',
121                    'compare' => 'IN',
122                    'value'   => 'booking,bookable-event,bookable-service',
123                ),
124            ),
125        );
126    }
127}