Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SourceHostingInfo
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 success
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_plain_domain
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace NewfoldLabs\WP\Module\Migration\Steps;
4
5use NewfoldLabs\WP\Module\Migration\Steps\AbstractStep;
6use NewfoldLabs\WP\Module\Migration\Services\HostingInfoService;
7
8/**
9 * Connection to InstaWp step.
10 *
11 * @package wp-module-migration
12 */
13class SourceHostingInfo extends AbstractStep {
14
15    /**
16     * Source host url.
17     *
18     * @var string Source host url.
19     */
20    private $source_host_url;
21
22    /**
23     * Source hosting details.
24     *
25     * @var array Source hosting details.
26     */
27    private $hosting_info;
28
29    /**
30     * Construct. Init basic parameters.
31     *
32     * @param string $source_host_url Source host url.
33     */
34    public function __construct( $source_host_url ) {
35        $this->source_host_url = $source_host_url;
36        $this->set_step_slug( 'SourceHostingInfo' );
37        $this->run();
38    }
39
40    /**
41     * Execute the step.
42     *
43     * @return void
44     */
45    protected function run() {
46
47        if ( ! empty( $this->source_host_url ) ) {
48
49            $plain_domain = $this->get_plain_domain( $this->source_host_url );
50
51            $hosting_info_service = new HostingInfoService( $plain_domain );
52
53            $hosting_info = $hosting_info_service->get_info();
54
55            if ( isset( $hosting_info['status'] ) && 'success' === $hosting_info['status'] ) {
56                $this->hosting_info = $hosting_info;
57                $this->success();
58            } else {
59                $this->set_response( array( 'message' => __( 'Source hosting details not retrieved correctly', 'wp-module-migration' ) ) );
60                $this->retry();
61            }
62        }
63    }
64
65    /**
66     * Set the step as successful and store the API key.
67     *
68     * @return void
69     */
70    protected function success() {
71        parent::success();
72
73        $this->set_data( 'SourceHostingData', $this->hosting_info );
74    }
75
76
77    /**
78     * Get the plain domain from a domain.
79     *
80     * @param string $domain The domain to get the plain domain from.
81     * @return string The plain domain.
82     */
83    public function get_plain_domain( string $domain ): string {
84        if ( ! preg_match( '#^https?://#', $domain ) ) {
85            $domain = 'http://' . $domain;
86        }
87        return isset( $parsed['scheme'], $parsed['host'] ) ? $parsed['scheme'] . '://' . $parsed['host'] : $domain;
88    }
89}