-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathclass-od-url-metric-store-request-context.php
More file actions
141 lines (131 loc) · 3.9 KB
/
class-od-url-metric-store-request-context.php
File metadata and controls
141 lines (131 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Optimization Detective: OD_URL_Metric_Store_Request_Context class
*
* @package optimization-detective
* @since 0.7.0
*/
declare( strict_types = 1 );
// @codeCoverageIgnoreStart
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// @codeCoverageIgnoreEnd
/**
* Context for when a URL Metric is successfully stored via the REST API.
*
* @since 0.7.0
*
* @property-read WP_REST_Request<array<string, mixed>> $request Request.
* @property-read positive-int $url_metrics_id ID for the od_url_metrics post.
* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
* @property-read OD_URL_Metric_Group $url_metric_group URL Metric group.
* @property-read OD_URL_Metric $url_metric URL Metric.
* @property-read positive-int $post_id Deprecated alias for the $url_metrics_id property.
*/
final class OD_URL_Metric_Store_Request_Context {
/**
* Request.
*
* @since 0.7.0
* @var WP_REST_Request<array<string, mixed>>
*/
private $request;
/**
* ID for the od_url_metrics post.
*
* This was originally $post_id which was introduced in 0.7.0.
*
* @since 1.0.0
* @var positive-int
*/
private $url_metrics_id;
/**
* URL Metric group collection.
*
* @since 0.7.0
* @var OD_URL_Metric_Group_Collection
*/
private $url_metric_group_collection;
/**
* URL Metric group.
*
* @since 0.7.0
* @var OD_URL_Metric_Group
*/
private $url_metric_group;
/**
* URL Metric.
*
* @since 0.7.0
* @var OD_URL_Metric
*/
private $url_metric;
/**
* Constructor.
*
* @since 0.7.0
*
* @phpstan-param WP_REST_Request<array<string, mixed>> $request
*
* @param WP_REST_Request $request REST API request.
* @param positive-int $url_metrics_id ID for the URL Metric post.
* @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
* @param OD_URL_Metric_Group $url_metric_group URL Metric group.
* @param OD_URL_Metric $url_metric URL Metric.
*/
public function __construct( WP_REST_Request $request, int $url_metrics_id, OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_URL_Metric_Group $url_metric_group, OD_URL_Metric $url_metric ) {
$this->request = $request;
$this->url_metrics_id = $url_metrics_id;
$this->url_metric_group_collection = $url_metric_group_collection;
$this->url_metric_group = $url_metric_group;
$this->url_metric = $url_metric;
}
/**
* Gets a property.
*
* @since 1.0.0
*
* @param string $name Property name.
* @return mixed Property value.
*
* @throws Error When property is unknown.
*/
public function __get( string $name ) {
switch ( $name ) {
case 'request':
return $this->request;
case 'url_metrics_id':
return $this->url_metrics_id;
case 'url_metric_group_collection':
return $this->url_metric_group_collection;
case 'url_metric_group':
return $this->url_metric_group;
case 'url_metric':
return $this->url_metric;
case 'post_id':
_doing_it_wrong(
esc_html( __CLASS__ . '::$' . $name ),
esc_html(
sprintf(
/* translators: %s is the class member variable name */
__( 'Use %s instead.', 'optimization-detective' ),
__CLASS__ . '::$url_metrics_id'
)
),
'optimization-detective 1.0.0'
);
return $this->url_metrics_id;
default:
throw new Error(
esc_html(
sprintf(
/* translators: %s is the class member variable name */
__( 'Unknown property %s.', 'optimization-detective' ),
__CLASS__ . '::$' . $name
)
)
);
}
}
}