Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.41% covered (warning)
56.41%
22 / 39
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
LCP_Analyzer
56.41% covered (warning)
56.41%
22 / 39
42.86% covered (danger)
42.86%
3 / 7
15.71
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 start
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 start_partial_analysis
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 get_cornerstone_pages
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 analyze_pages
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 get_state
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_storage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Automattic\Jetpack_Boost\Modules\Optimizations\Lcp;
4
5use Automattic\Jetpack\Boost_Core\Lib\Boost_API;
6use Automattic\Jetpack_Boost\Lib\Cornerstone\Cornerstone_Utils;
7
8class LCP_Analyzer {
9    /** @var LCP_State */
10    private $state;
11
12    /** @var LCP_Storage */
13    private $storage;
14
15    /**
16     * @since 4.0.0
17     */
18    public function __construct() {
19        $this->state   = new LCP_State();
20        $this->storage = new LCP_Storage();
21    }
22
23    /**
24     * Start the LCP analysis process
25     *
26     * @return array The current state data
27     */
28    public function start() {
29        // Get cornerstone pages to analyze
30        $pages = $this->get_cornerstone_pages();
31
32        // Store those pages in the LCP State
33        $this->state
34            ->prepare_request()
35            ->set_pages( $pages )
36            ->set_pending_pages( $pages )
37            ->save();
38
39        // Get the data
40        $data = $this->state->get();
41
42        // Start the analysis process
43        $this->analyze_pages( $pages );
44
45        // Clear previous LCP analysis data from storage
46        $this->storage->clear();
47
48        return $data;
49    }
50
51    /**
52     * Start a partial analysis for the given pages.
53     * Sets status to pending and updates only the pages that are in the $pages array.
54     * Pages that are not in the $pages array will not be updated.
55     *
56     * @param array $pages The pages to analyze.
57     * @return array The current state data
58     */
59    public function start_partial_analysis( $pages ) {
60        $current_pages = $this->state->get_pages();
61
62        $this->state
63            ->prepare_request()
64            ->set_pages( $current_pages )
65            ->set_pending_pages( $pages )
66            ->save();
67
68        $response = $this->analyze_pages( $pages );
69
70        // No callback follows a refused request, so the pages would otherwise stay pending.
71        if ( is_wp_error( $response ) ) {
72            $this->state->set_error( $response->get_error_message() )->save();
73        }
74
75        // @todo - Clear previous LCP analysis data from storage for pages in the $pages array.
76        // We currently don't have a way to do this.
77
78        return $this->state->get();
79    }
80
81    /**
82     * Get cornerstone pages for analysis
83     *
84     * @return array
85     */
86    protected function get_cornerstone_pages() {
87        $pages                  = array();
88        $cornerstone_pages_list = Cornerstone_Utils::get_list();
89
90        foreach ( $cornerstone_pages_list as $url ) {
91            $pages[] = Cornerstone_Utils::prepare_provider_data( $url );
92        }
93
94        return $pages;
95    }
96
97    /**
98     * Run analysis for the given pages
99     *
100     * @param array $pages Pages to analyze
101     */
102    private function analyze_pages( $pages ) {
103        $payload = array(
104            'pages'     => $pages,
105            'requestId' => md5(
106                wp_json_encode(
107                    $pages,
108                    0 // phpcs:ignore Jetpack.Functions.JsonEncodeFlags.ZeroFound -- No `json_encode()` flags because this needs to match whatever is calculating the hash on the other end.
109                )
110            ),
111        );
112        return Boost_API::post( 'lcp', $payload );
113    }
114
115    /**
116     * Get the current state
117     *
118     * @return LCP_State
119     */
120    public function get_state() {
121        return $this->state;
122    }
123
124    /**
125     * Get the storage instance
126     *
127     * @return LCP_Storage
128     */
129    public function get_storage() {
130        return $this->storage;
131    }
132}