Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Report_Definition
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
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
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, contract version and routes.
12 *
13 * @since 0.3.3
14 */
15class Report_Definition {
16
17    /**
18     * Report id, e.g. `orders.by_date`.
19     *
20     * @var string
21     */
22    public $id;
23
24    /**
25     * Contract version; 0 until the contract is written down.
26     *
27     * @var int
28     */
29    public $version;
30
31    /**
32     * Regex patterns over the route below `analytics/reports/`.
33     *
34     * @var string[]
35     */
36    public $routes;
37
38    /**
39     * Constructor.
40     *
41     * @param string   $id      Report id.
42     * @param int      $version Contract version.
43     * @param string[] $routes  Route patterns.
44     */
45    public function __construct( $id, $version, array $routes ) {
46        $this->id      = $id;
47        $this->version = $version;
48        $this->routes  = $routes;
49    }
50
51    /**
52     * Whether a route names this report, with or without a trailing slash.
53     *
54     * @param string $route Route below `analytics/reports/`.
55     * @return bool
56     */
57    public function matches_route( $route ) {
58        foreach ( $this->routes as $pattern ) {
59            if ( preg_match( '#^' . $pattern . '/?$#D', $route ) ) {
60                return true;
61            }
62        }
63
64        return false;
65    }
66}