Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Report_Executor
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * Picks the handler of a report request and runs it.
4 *
5 * @package automattic/jetpack-woocommerce-stats
6 */
7
8namespace Automattic\Jetpack\WooCommerceStats\Report_Execution;
9
10use WP_Error;
11
12/**
13 * Resolves a route to its report and a provider's handler, then runs it, without falling back.
14 *
15 * @since 0.3.3
16 */
17class Report_Executor {
18
19    /**
20     * Values the `provider` param accepts.
21     */
22    const PROVIDERS = array( 'local', 'wpcom' );
23
24    /**
25     * Provider of a request that names none.
26     */
27    private const DEFAULT_PROVIDER = 'wpcom';
28
29    /**
30     * Reports, handlers and providers.
31     *
32     * @var Report_Registry
33     */
34    private $registry;
35
36    /**
37     * Constructor.
38     *
39     * @param Report_Registry $registry Reports, handlers and providers.
40     */
41    public function __construct( Report_Registry $registry ) {
42        $this->registry = $registry;
43    }
44
45    /**
46     * Serve a report from the handler the request resolves to.
47     *
48     * @param string      $route         Route below `analytics/reports/`.
49     * @param array       $params        The report's query params, without `provider` and `force_refresh`.
50     * @param string|null $provider      The validated `provider` param, or null when the request names none.
51     * @param bool        $force_refresh Whether to skip caches.
52     * @return array|WP_Error The handler's payload, or an error: `unknown_report` (404),
53     *                        `unsupported_provider` (422), the provider's when it is not ready, or the handler's.
54     */
55    public function execute( $route, array $params, $provider = null, $force_refresh = false ) {
56        $definition = $this->registry->get_definition_for_route( $route );
57        if ( null === $definition ) {
58            return new WP_Error(
59                'unknown_report',
60                __( 'No report is defined for this route.', 'jetpack-woocommerce-stats-pkg' ),
61                array( 'status' => 404 )
62            );
63        }
64
65        $name    = $provider ?? self::DEFAULT_PROVIDER;
66        $handler = $this->registry->get_handler( $definition->id, $name );
67        if ( null === $handler ) {
68            return new WP_Error(
69                'unsupported_provider',
70                /* translators: %s: provider name, such as wpcom or local. */
71                sprintf( __( 'This report cannot be served by the %s provider.', 'jetpack-woocommerce-stats-pkg' ), $name ),
72                array( 'status' => 422 )
73            );
74        }
75
76        // A request naming no provider leaves readiness to the handler: the forward serves its cache before checking the connection.
77        if ( null !== $provider ) {
78            $ready = $this->registry->get_provider( $name )->check_ready();
79            if ( is_wp_error( $ready ) ) {
80                return $ready;
81            }
82        }
83
84        return $handler->run( new Report_Request( $definition, $route, $params, $force_refresh ) );
85    }
86}