Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Speed_Score_Graph_History_Request | |
0.00% |
0 / 32 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| jsonUnserialize | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| cache_prefix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Represents a request to generate an array of speed scores history. |
| 4 | * |
| 5 | * @package automattic/jetpack-boost-speed-score |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Boost_Speed_Score; |
| 9 | |
| 10 | use Automattic\Jetpack\Boost_Core\Lib\Boost_API; |
| 11 | use Automattic\Jetpack\Boost_Core\Lib\Cacheable; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit( 0 ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Class Speed_Score_Graph_History_Request |
| 19 | */ |
| 20 | class Speed_Score_Graph_History_Request extends Cacheable { |
| 21 | /** |
| 22 | * Algorithm to use when defining a hash for the cache. |
| 23 | */ |
| 24 | const CACHE_KEY_HASH_ALGO = 'md5'; |
| 25 | |
| 26 | /** |
| 27 | * The timestamp start windown in ms. |
| 28 | * |
| 29 | * @var int|float $start timestamp start windown in ms. |
| 30 | */ |
| 31 | private $start; |
| 32 | |
| 33 | /** |
| 34 | * The timestamp end windown in ms. |
| 35 | * |
| 36 | * @var int|float $end timestamp end windown in ms. |
| 37 | */ |
| 38 | private $end; |
| 39 | |
| 40 | /** |
| 41 | * Number of retries attempted. |
| 42 | * |
| 43 | * @var int $retry_count Number of times this Speed Score request has been retried. |
| 44 | */ |
| 45 | private $retry_count; |
| 46 | |
| 47 | /** |
| 48 | * The error returned |
| 49 | * |
| 50 | * @var array $error Speed Scores error. |
| 51 | */ |
| 52 | private $error; |
| 53 | |
| 54 | /** |
| 55 | * Used when there's an error. |
| 56 | * |
| 57 | * @var string $status Status. |
| 58 | */ |
| 59 | private $status; |
| 60 | |
| 61 | /** |
| 62 | * Constructor. |
| 63 | * |
| 64 | * @param int|float $start timestamp start windown in ms. |
| 65 | * @param int|float $end timestamp end windown in ms. |
| 66 | * @param array $error Speed Scores error. |
| 67 | */ |
| 68 | public function __construct( $start, $end, $error ) { |
| 69 | $this->start = $start; |
| 70 | $this->end = $end; |
| 71 | $this->error = $error; |
| 72 | $this->retry_count = 0; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Convert this object to a plain array for JSON serialization. |
| 77 | */ |
| 78 | #[\ReturnTypeWillChange] |
| 79 | public function jsonSerialize() { |
| 80 | return array( |
| 81 | 'start' => $this->start, |
| 82 | 'end' => $this->end, |
| 83 | 'error' => $this->error, |
| 84 | 'retry_count' => $this->retry_count, |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * This is intended to be the reverse of JsonSerializable->jsonSerialize. |
| 90 | * |
| 91 | * @param mixed $data The data to turn into an object. |
| 92 | * |
| 93 | * @return Speed_Score_Graph_History_Request |
| 94 | */ |
| 95 | public static function jsonUnserialize( $data ) { |
| 96 | $object = new Speed_Score_Graph_History_Request( |
| 97 | $data['start'], |
| 98 | $data['end'], |
| 99 | $data['error'] |
| 100 | ); |
| 101 | |
| 102 | if ( ! empty( $data['retry_count'] ) ) { |
| 103 | $object->retry_count = intval( $data['retry_count'] ); |
| 104 | } |
| 105 | |
| 106 | return $object; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Return the cache prefix. |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | protected static function cache_prefix() { |
| 115 | return 'jetpack_boost_speed_scores_graph_history_'; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Send a Speed History request to the API. |
| 120 | * |
| 121 | * @return true|\WP_Error True on success, WP_Error on failure. |
| 122 | */ |
| 123 | public function execute() { |
| 124 | $response = Boost_API::get( |
| 125 | 'speed-scores-history', |
| 126 | array( |
| 127 | 'start' => $this->start, |
| 128 | 'end' => $this->end, |
| 129 | ) |
| 130 | ); |
| 131 | |
| 132 | if ( is_wp_error( $response ) ) { |
| 133 | $this->status = 'error'; |
| 134 | $this->error = $response->get_error_message(); |
| 135 | $this->store(); |
| 136 | |
| 137 | return $response; |
| 138 | } |
| 139 | |
| 140 | return $response; |
| 141 | } |
| 142 | } |