Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
InstaMigrateService
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2namespace NewfoldLabs\WP\Module\Migration\Services;
3
4use InstaWP\Connect\Helpers\Helper;
5use NewfoldLabs\WP\Module\Migration\Steps\GetInstaWpApiKey;
6use NewfoldLabs\WP\Module\Migration\Steps\InstallActivateInstaWp;
7use NewfoldLabs\WP\Module\Migration\Steps\ConnectToInstaWp;
8use NewfoldLabs\WP\Module\Migration\Services\Tracker;
9/**
10 * Class InstaMigrateService
11 */
12class InstaMigrateService {
13
14    /**
15     * InstaWP Connect plugin API key used for connecting the instaWP plugin
16     *
17     * @var string $insta_api_key
18     */
19    private $insta_api_key = '';
20
21    /**
22     * Tracker class instance.
23     *
24     * @var Tracker $tracker
25     */
26    private $tracker;
27
28    /**
29     * Set required API keys for insta to initiate the migration
30     */
31    public function __construct() {
32        $this->tracker = new Tracker();
33        $this->tracker->reset();
34    }
35
36    /**
37     * Get Insta Wp api key, Install InstaWP plugin and connect to it
38     */
39    public function run() {
40
41        delete_option( 'nfd_migration_status_sent' );
42
43        $instawp_get_key_step = new GetInstaWpApiKey();
44        EventService::send_application_event(
45            'migration_get_vendor_api_key',
46            array(
47                'status' => $instawp_get_key_step->get_status(),
48            )
49        );
50        $this->tracker->update_track( $instawp_get_key_step );
51        if ( ! $instawp_get_key_step->failed() ) {
52            $this->insta_api_key = $instawp_get_key_step->get_data( 'insta_api_key' );
53        } else {
54            return new \WP_Error(
55                'Bad request',
56                esc_html__( 'Cannot get api key.', 'wp-module-migration' ),
57                array( 'status' => 400 )
58            );
59        }
60
61        $install_activate = new InstallActivateInstaWp();
62        $this->tracker->update_track( $install_activate );
63        EventService::send_application_event(
64            'migration_vendor_plugin_activate',
65            array(
66                'status' => $install_activate->get_status(),
67            )
68        );
69        if ( ! $install_activate->failed() ) {
70            $connect_to_instawp = new ConnectToInstaWp( $this->insta_api_key );
71            $this->tracker->update_track( $connect_to_instawp );
72            EventService::send_application_event(
73                'migration_vendor_plugin_connect',
74                array(
75                    'status' => $connect_to_instawp->get_status(),
76                )
77            );
78            if ( ! $connect_to_instawp->failed() ) {
79                // Add the current WordPress locale to the redirect URL
80                $locale = get_locale();
81                return array(
82                    'message'      => esc_html__( 'Connect plugin is installed and ready to start the migration.', 'wp-module-migration' ),
83                    'response'     => true,
84                    'redirect_url' => esc_url_raw(
85                        apply_filters(
86                            'nfd_build_url',
87                            sprintf(
88                                '%s/%s?g_id=%s&locale=%s',
89                                NFD_MIGRATION_PROXY_WORKER,
90                                INSTAWP_MIGRATE_ENDPOINT,
91                                Helper::get_mig_gid(),
92                                $locale
93                            )
94                        )
95                    ),
96                );
97            } else {
98                return new \WP_Error(
99                    'bad_request',
100                    esc_html__( 'Website could not connect successfully.', 'wp-module-migration' ),
101                    array( 'status' => 400 )
102                );
103            }
104        } else {
105            return new \WP_Error(
106                'Error',
107                esc_html__( 'Migration service could not be started.', 'wp-module-migration' ),
108                array( 'status' => 400 )
109            );
110        }
111    }
112}