|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Optimization Detective: OD_Template_Optimization_Context class |
| 4 | + * |
| 5 | + * @package optimization-detective |
| 6 | + * @since n.e.x.t |
| 7 | + */ |
| 8 | + |
| 9 | +// @codeCoverageIgnoreStart |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { |
| 11 | + exit; // Exit if accessed directly. |
| 12 | +} |
| 13 | +// @codeCoverageIgnoreEnd |
| 14 | + |
| 15 | +/** |
| 16 | + * Context for optimizing a template prior to rendering. |
| 17 | + * |
| 18 | + * @since n.e.x.t |
| 19 | + * |
| 20 | + * @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection. |
| 21 | + * @property-read OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry. |
| 22 | + * @property-read positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection. |
| 23 | + * @property-read array<string, mixed> $normalized_query_vars Normalized query vars. |
| 24 | + * @property-read non-empty-string $url_metrics_slug Slug for the od_url_metrics post. |
| 25 | + * @property-read non-empty-string $current_etag Current ETag. |
| 26 | + */ |
| 27 | +final class OD_Template_Optimization_Context { |
| 28 | + |
| 29 | + /** |
| 30 | + * URL Metric group collection. |
| 31 | + * |
| 32 | + * @since n.e.x.t |
| 33 | + * @var OD_URL_Metric_Group_Collection |
| 34 | + */ |
| 35 | + private $url_metric_group_collection; |
| 36 | + |
| 37 | + /** |
| 38 | + * Tag visitor registry. |
| 39 | + * |
| 40 | + * @since n.e.x.t |
| 41 | + * @var OD_Tag_Visitor_Registry |
| 42 | + */ |
| 43 | + private $tag_visitor_registry; |
| 44 | + |
| 45 | + /** |
| 46 | + * ID for the od_url_metrics post which provided the URL Metrics in the collection. |
| 47 | + * |
| 48 | + * May be null if no post has been created yet. |
| 49 | + * |
| 50 | + * @since n.e.x.t |
| 51 | + * @var positive-int|null |
| 52 | + */ |
| 53 | + private $url_metrics_id; |
| 54 | + |
| 55 | + /** |
| 56 | + * Normalized query vars. |
| 57 | + * |
| 58 | + * @since n.e.x.t |
| 59 | + * @var array<string, mixed> |
| 60 | + */ |
| 61 | + private $normalized_query_vars; |
| 62 | + |
| 63 | + /** |
| 64 | + * Slug for the od_url_metrics post. |
| 65 | + * |
| 66 | + * @since n.e.x.t |
| 67 | + * @var non-empty-string |
| 68 | + */ |
| 69 | + private $url_metrics_slug; |
| 70 | + |
| 71 | + /** |
| 72 | + * Current ETag. |
| 73 | + * |
| 74 | + * @since n.e.x.t |
| 75 | + * @var non-empty-string |
| 76 | + */ |
| 77 | + private $current_etag; |
| 78 | + |
| 79 | + /** |
| 80 | + * Constructor. |
| 81 | + * |
| 82 | + * @since n.e.x.t |
| 83 | + * |
| 84 | + * @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection. |
| 85 | + * @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry. |
| 86 | + * @param positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection. May be null if no post has been created yet. |
| 87 | + * @param array<string, mixed> $normalized_query_vars Normalized query vars. |
| 88 | + * @param non-empty-string $url_metrics_slug Slug for the od_url_metrics post. |
| 89 | + * @param non-empty-string $current_etag Current ETag. |
| 90 | + */ |
| 91 | + public function __construct( OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_Tag_Visitor_Registry $tag_visitor_registry, ?int $url_metrics_id, array $normalized_query_vars, string $url_metrics_slug, string $current_etag ) { |
| 92 | + $this->url_metric_group_collection = $url_metric_group_collection; |
| 93 | + $this->tag_visitor_registry = $tag_visitor_registry; |
| 94 | + $this->url_metrics_id = $url_metrics_id; |
| 95 | + $this->normalized_query_vars = $normalized_query_vars; |
| 96 | + $this->url_metrics_slug = $url_metrics_slug; |
| 97 | + $this->current_etag = $current_etag; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Gets a property. |
| 102 | + * |
| 103 | + * @since n.e.x.t |
| 104 | + * |
| 105 | + * @param string $name Property name. |
| 106 | + * @return mixed Property value. |
| 107 | + * |
| 108 | + * @throws Error When property is unknown. |
| 109 | + */ |
| 110 | + public function __get( string $name ) { |
| 111 | + switch ( $name ) { |
| 112 | + case 'tag_visitor_registry': |
| 113 | + return $this->tag_visitor_registry; |
| 114 | + case 'url_metrics_id': |
| 115 | + return $this->url_metrics_id; |
| 116 | + case 'url_metric_group_collection': |
| 117 | + return $this->url_metric_group_collection; |
| 118 | + case 'normalized_query_vars': |
| 119 | + return $this->normalized_query_vars; |
| 120 | + case 'url_metrics_slug': |
| 121 | + return $this->url_metrics_slug; |
| 122 | + case 'current_etag': |
| 123 | + return $this->current_etag; |
| 124 | + default: |
| 125 | + throw new Error( |
| 126 | + esc_html( |
| 127 | + sprintf( |
| 128 | + /* translators: %s is class member variable name */ |
| 129 | + __( 'Unknown property %s.', 'optimization-detective' ), |
| 130 | + __CLASS__ . '::$' . $name |
| 131 | + ) |
| 132 | + ) |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments