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