Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Report_Definition
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 matches_route
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 get_field_names
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_contract
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * What a store report is, independent of who serves it.
4 *
5 * @package automattic/jetpack-woocommerce-stats
6 */
7
8namespace Automattic\Jetpack\WooCommerceStats\Report_Execution;
9
10/**
11 * A report's identity, routes and contract: its summary fields, the datasets it reads and the
12 * semantics two handlers must share to be interchangeable.
13 *
14 * @since 0.3.3
15 */
16class Report_Definition {
17
18    /**
19     * Report id, e.g. `orders.by_date`.
20     *
21     * @var string
22     */
23    public $id;
24
25    /**
26     * Contract version; 0 until the contract is written down.
27     *
28     * @var int
29     */
30    public $version;
31
32    /**
33     * Regex patterns over the route below `analytics/reports/`.
34     *
35     * @var string[]
36     */
37    public $routes;
38
39    /**
40     * Summary fields by Premium Analytics metric type (`number`, `currency` or `average`), or null for an undescribed report.
41     *
42     * @var array<string, string>|null
43     */
44    public $fields;
45
46    /**
47     * What the report reads, such as `orders` or `sessions`.
48     *
49     * @var string[]
50     */
51    public $datasets;
52
53    /**
54     * Rules two handlers must share, such as `week_start => site`.
55     *
56     * @var array<string, string>
57     */
58    public $semantics;
59
60    /**
61     * Constructor.
62     *
63     * @param string                     $id        Report id.
64     * @param int                        $version   Contract version.
65     * @param string[]                   $routes    Route patterns.
66     * @param array<string, string>|null $fields    Summary fields by type, or null.
67     * @param string[]                   $datasets  Datasets it reads.
68     * @param array<string, string>      $semantics Shared rules.
69     */
70    public function __construct( $id, $version, array $routes, $fields = null, array $datasets = array(), array $semantics = array() ) {
71        $this->id        = $id;
72        $this->version   = $version;
73        $this->routes    = $routes;
74        $this->fields    = $fields;
75        $this->datasets  = $datasets;
76        $this->semantics = $semantics;
77    }
78
79    /**
80     * Whether a route names this report, with or without a trailing slash.
81     *
82     * @param string $route Route below `analytics/reports/`.
83     * @return bool
84     */
85    public function matches_route( $route ) {
86        foreach ( $this->routes as $pattern ) {
87            if ( preg_match( '#^' . $pattern . '/?$#D', $route ) ) {
88                return true;
89            }
90        }
91
92        return false;
93    }
94
95    /**
96     * The contract's summary field names, none for an undescribed report.
97     *
98     * @return string[]
99     */
100    public function get_field_names() {
101        return null === $this->fields ? array() : array_keys( $this->fields );
102    }
103
104    /**
105     * The contract as the `X-Jetpack-Report-Contract` header names it.
106     *
107     * @return string `id@version`.
108     */
109    public function get_contract() {
110        return $this->id . '@' . $this->version;
111    }
112}