Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
HostingInfoService
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_url_to_check
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_info
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
42
 get_full_service_url
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get_timeout
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace NewfoldLabs\WP\Module\Migration\Services;
4
5/**
6 * Class for pagespeed calls.
7 */
8class HostingInfoService {
9    /**
10     * Service endpoint.
11     *
12     * @var string $endpoint
13     */
14    protected $endpoint = 'hostinginfo';
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_info() {
51
52        $args = array(
53            'timeout' => $this->get_timeout(),
54        );
55
56        $response = wp_remote_get( $this->get_full_service_url(), $args );
57
58        if ( wp_remote_retrieve_response_code( $response ) === 200 && ! is_wp_error( $response ) ) {
59            $body_response = wp_remote_retrieve_body( $response );
60            $data_response = json_decode( $body_response, true );
61
62            if ( ! isset( $data_response['error'] ) && isset( $data_response['url'] ) ) {
63                return array(
64                    'status' => 'success',
65                    'url'    => $data_response['url'] ?? '',
66                    'ip'     => $data_response['ip'] ?? '',
67                    'isp'    => $data_response['isp'] ?? '',
68                    'org'    => $data_response['org'] ?? '',
69                    'as'     => $data_response['as'] ?? '',
70                );
71            } else {
72                return array(
73                    'status'  => 'error',
74                    'message' => __( 'Error decoding response', 'wp-module-migration' ),
75                );
76            }
77        } elseif ( is_wp_error( $response ) ) {
78            return array(
79                'status'  => 'error',
80                'message' => $response->get_error_message(),
81            );
82        } else {
83            return array(
84                'status'  => 'error',
85                'message' => wp_remote_retrieve_response_code( $response ) . ' ' . wp_remote_retrieve_response_message( $response ),
86            );
87        }
88    }
89
90    /**
91     * Get base service url.
92     *
93     * @return string
94     */
95    public function get_full_service_url() {
96        $url_to_check = rawurlencode( $this->get_url_to_check() );
97        $service_url  = esc_url( apply_filters( 'nfd_build_url', NFD_MIGRATION_PROXY_WORKER . "/$this->endpoint?url=$url_to_check" ) );
98
99        return $service_url;
100    }
101    /**
102     * Get timeout for the service.
103     *
104     * @return int
105     */
106    public function get_timeout() {
107        return $this->timeout;
108    }
109}