Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Default_Reports
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
0.00% covered (danger)
0.00%
0 / 1
 register
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
1<?php
2/**
3 * The package's own reports, handler and provider.
4 *
5 * @package automattic/jetpack-woocommerce-stats
6 */
7
8namespace Automattic\Jetpack\WooCommerceStats\Report_Execution;
9
10use Automattic\Jetpack\WooCommerceStats\Api_Proxy_Controller;
11
12/**
13 * Registers the reports WordPress.com serves, on `Report_Registry::REGISTER_ACTION`.
14 *
15 * @since $$next-version$$
16 */
17class Default_Reports {
18
19    /**
20     * Summary fields of `orders.by_date`, from WordPress.com's orders data store. The averages are null without orders.
21     */
22    private const ORDERS_BY_DATE_FIELDS = array(
23        'orders_no'                => 'int',
24        'orders_value_net'         => 'money',
25        'orders_value_gross'       => 'money',
26        'total_sales'              => 'money',
27        'average_order_value'      => 'money',
28        'avg_items'                => 'number',
29        'coupons'                  => 'money',
30        'refunds'                  => 'money',
31        'total_items'              => 'int',
32        'total_items_refunded'     => 'int',
33        'total_orders_with_refund' => 'int',
34        'paid_orders_count'        => 'int',
35        'paid_net_sales'           => 'money',
36        'unpaid_orders_count'      => 'int',
37        'unpaid_net_sales'         => 'money',
38        'product_net_revenue'      => 'money',
39        'cogs_amount'              => 'money',
40        'profit_margin'            => 'money',
41    );
42
43    /**
44     * Summary fields of `sessions.by_device`, from WordPress.com's sessions data store.
45     */
46    private const SESSIONS_BY_DEVICE_FIELDS = array(
47        'active_sessions' => 'int',
48        'total_orders'    => 'int',
49    );
50
51    /**
52     * What an orders report counts, and by which calendar.
53     */
54    private const ORDER_SEMANTICS = array(
55        'population' => 'analytics_statuses',
56        'week_start' => 'site',
57        'timezone'   => 'site',
58    );
59
60    /**
61     * Reports WordPress.com serves, by id => route pattern.
62     */
63    private const ROUTES = array(
64        'bookings.by_date'                     => 'bookings/by-date',
65        'coupons'                              => 'coupons',
66        'coupons.by_date'                      => 'coupons/by-date',
67        'customers.by_date'                    => 'customers/by-date',
68        'customers.new_returning'              => 'customers/new-returning',
69        'order_attribution.compare'            => 'order-attribution/[\w-]+/compare',
70        'order_attribution.items'              => 'order-attribution/[\w-]+/items',
71        'order_attribution.summary'            => 'order-attribution/[\w-]+/summary',
72        'order_attribution_by_product.summary' => 'order-attribution-by-product/[\w-]+/summary',
73        'orders.by_date'                       => 'orders/by-date',
74        'orders_by_product_type.by_date'       => 'orders-by-product-type/by-date',
75        'products'                             => 'products',
76        'sessions.by_conversion_rate'          => 'sessions/by-conversion-rate',
77        'sessions.by_date'                     => 'sessions/by-date',
78        'sessions.by_device'                   => 'sessions/by-device',
79        'sessions.by_location'                 => 'sessions/by-location',
80        'taxes.breakdown'                      => 'taxes/breakdown',
81        'taxes.by_date'                        => 'taxes/by-date',
82    );
83
84    /**
85     * Fill the registry.
86     *
87     * @param mixed $registry The registry being filled.
88     * @return void
89     */
90    public static function register( $registry ) {
91        if ( ! $registry instanceof Report_Registry ) {
92            return;
93        }
94
95        $registry->register_provider( 'wpcom', new Wpcom_Report_Provider() );
96        $registry->register_handler( Report_Registry::ANY_REPORT, 'wpcom', new Wpcom_Forward_Handler( new Api_Proxy_Controller() ) );
97
98        $registry->register_report( new Report_Definition( 'orders.by_date', 1, array( self::ROUTES['orders.by_date'] ), self::ORDERS_BY_DATE_FIELDS, array( 'orders' ), self::ORDER_SEMANTICS ) );
99        $registry->register_report( new Report_Definition( 'sessions.by_device', 1, array( self::ROUTES['sessions.by_device'] ), self::SESSIONS_BY_DEVICE_FIELDS, array( 'sessions' ), array( 'timezone' => 'site' ) ) );
100
101        foreach ( self::ROUTES as $id => $route ) {
102            $registry->register_report( new Report_Definition( $id, 0, array( $route ) ) );
103        }
104    }
105}