Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.54% covered (warning)
58.54%
24 / 41
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
LCP_Analyzer
58.54% covered (warning)
58.54%
24 / 41
42.86% covered (danger)
42.86%
3 / 7
17.13
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%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
 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 when the saved state has pages, marking only the given pages pending.
53     * Otherwise return the state unchanged.
54     *
55     * @param array $pages The pages to analyze.
56     * @return array The current state data
57     */
58    public function start_partial_analysis( $pages ) {
59        $current_state = $this->state->get();
60        if ( empty( $current_state['pages'] ) ) {
61            return $current_state;
62        }
63
64        $this->state
65            ->prepare_request()
66            ->set_pages( $current_state['pages'] )
67            ->set_pending_pages( $pages )
68            ->save();
69
70        $response = $this->analyze_pages( $pages );
71
72        // No callback follows a refused request, so the pages would otherwise stay pending.
73        if ( is_wp_error( $response ) ) {
74            $this->state->set_error( $response->get_error_message() )->save();
75        }
76
77        // @todo - Clear previous LCP analysis data from storage for pages in the $pages array.
78        // We currently don't have a way to do this.
79
80        return $this->state->get();
81    }
82
83    /**
84     * Get cornerstone pages for analysis
85     *
86     * @return array
87     */
88    protected function get_cornerstone_pages() {
89        $pages                  = array();
90        $cornerstone_pages_list = Cornerstone_Utils::get_list();
91
92        foreach ( $cornerstone_pages_list as $url ) {
93            $pages[] = Cornerstone_Utils::prepare_provider_data( $url );
94        }
95
96        return $pages;
97    }
98
99    /**
100     * Run analysis for the given pages
101     *
102     * @param array $pages Pages to analyze
103     */
104    private function analyze_pages( $pages ) {
105        $payload = array(
106            'pages'     => $pages,
107            'requestId' => md5(
108                wp_json_encode(
109                    $pages,
110                    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.
111                )
112            ),
113        );
114        return Boost_API::post( 'lcp', $payload );
115    }
116
117    /**
118     * Get the current state
119     *
120     * @return LCP_State
121     */
122    public function get_state() {
123        return $this->state;
124    }
125
126    /**
127     * Get the storage instance
128     *
129     * @return LCP_Storage
130     */
131    public function get_storage() {
132        return $this->storage;
133    }
134}