Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| PageSpeedService | |
0.00% |
0 / 33 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_url_to_check | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_pagespeed | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
30 | |||
| get_full_service_url | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| get_timeout | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_timeout | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Migration\Services; |
| 4 | |
| 5 | /** |
| 6 | * Class for pagespeed calls. |
| 7 | */ |
| 8 | class PageSpeedService { |
| 9 | /** |
| 10 | * Service endpoint. |
| 11 | * |
| 12 | * @var string $endpoint |
| 13 | */ |
| 14 | protected $endpoint = 'pagespeed'; |
| 15 | /** |
| 16 | * Timeout for the service. |
| 17 | * |
| 18 | * @var int $timeout |
| 19 | */ |
| 20 | protected $timeout = 60; |
| 21 | /** |
| 22 | * URL to check. |
| 23 | * |
| 24 | * @var string $url_to_check |
| 25 | */ |
| 26 | protected $url_to_check; |
| 27 | |
| 28 | /** |
| 29 | * Construct. Init basic parameters. |
| 30 | * |
| 31 | * @param string $url url to get speed index. |
| 32 | */ |
| 33 | public function __construct( $url ) { |
| 34 | $this->url_to_check = $url; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get url to check. |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public function get_url_to_check() { |
| 43 | return $this->url_to_check; |
| 44 | } |
| 45 | /** |
| 46 | * Get speed index. |
| 47 | * |
| 48 | * @return array |
| 49 | */ |
| 50 | public function get_pagespeed() { |
| 51 | $args = array( |
| 52 | 'timeout' => $this->get_timeout(), |
| 53 | ); |
| 54 | |
| 55 | $response = wp_remote_get( $this->get_full_service_url(), $args ); |
| 56 | if ( wp_remote_retrieve_response_code( $response ) === 200 && ! is_wp_error( $response ) ) { |
| 57 | $body_response = wp_remote_retrieve_body( $response ); |
| 58 | $data_response = json_decode( $body_response, true ); |
| 59 | if ( isset( $data_response['speedIndex'] ) ) { |
| 60 | return array( |
| 61 | 'status' => 'success', |
| 62 | 'speedIndex' => $data_response['speedIndex'], |
| 63 | ); |
| 64 | } else { |
| 65 | return array( |
| 66 | 'status' => 'error', |
| 67 | 'message' => __( 'Error decoding response', 'wp-module-migration' ), |
| 68 | ); |
| 69 | } |
| 70 | } elseif ( is_wp_error( $response ) ) { |
| 71 | return array( |
| 72 | 'status' => 'error', |
| 73 | 'message' => $response->get_error_message(), |
| 74 | ); |
| 75 | } else { |
| 76 | return array( |
| 77 | 'status' => 'error', |
| 78 | 'message' => wp_remote_retrieve_response_code( $response ) . ' ' . wp_remote_retrieve_response_message( $response ), |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get base service url. |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | public function get_full_service_url() { |
| 89 | $url_to_check = rawurlencode( $this->get_url_to_check() ); |
| 90 | $service_url = esc_url( NFD_MIGRATION_PROXY_WORKER . "/$this->endpoint?url=$url_to_check" ); |
| 91 | |
| 92 | return $service_url; |
| 93 | } |
| 94 | /** |
| 95 | * Get timeout for the service. |
| 96 | * |
| 97 | * @return int |
| 98 | */ |
| 99 | public function get_timeout() { |
| 100 | return $this->timeout; |
| 101 | } |
| 102 | /** |
| 103 | * Set timeout for the service. |
| 104 | * |
| 105 | * @param int $timeout timeout for the service. |
| 106 | */ |
| 107 | public function set_timeout( $timeout ) { |
| 108 | $timeout = intval( $timeout ) > 0 ? intval( $timeout ) : 60; |
| 109 | $this->timeout = $timeout; |
| 110 | } |
| 111 | } |