Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Marketplace_Software_Manager
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 install_marketplace_software
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 get_apd_marketplace_software
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2/**
3 * This class is responsible for installing the marketplace software.
4 *
5 * @since 5.5.0
6 * @package WPCOM_Marketplace
7 */
8require_once __DIR__ . '/class-marketplace-software-factory.php';
9
10/**
11 * Class Marketplace_Software_Manager.
12 *
13 * This class is responsible for installing the marketplace software by loading the software details from the atomic
14 * persistent data, generating and executing the WP_CLI installation commands.
15 */
16class Marketplace_Software_Manager {
17
18    /**
19     * Install the marketplace software by loading the software details from the atomic persistent data, generating
20     * and executing the WP_CLI installation commands.
21     *
22     * @return WP_Error|bool
23     */
24    public function install_marketplace_software() {
25        $wpcom_marketplace_software = $this->get_apd_marketplace_software();
26        if ( is_wp_error( $wpcom_marketplace_software ) ) {
27            return $wpcom_marketplace_software;
28        }
29
30        // @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach -- $wpcom_marketplace_software is an array.
31        foreach ( $wpcom_marketplace_software as $software ) {
32            $product_software = Marketplace_Software_Factory::get_product_software( $software );
33            if ( is_wp_error( $product_software ) ) {
34                WPCOMSH_Log::unsafe_direct_log( $product_software->get_error_message() );
35                continue;
36            }
37
38            $installer    = Marketplace_Software_Factory::get_product_installer( $product_software );
39            $installation = $installer->install();
40            if ( is_wp_error( $installation ) ) {
41                WPCOMSH_Log::unsafe_direct_log( $installation->get_error_message(), $installer->get_results() );
42            }
43        }
44
45        return true;
46    }
47
48    /**
49     * Get the marketplace software from Atomic Persist Data. This data is persisted by the
50     * woa_post_transfer_wpcomsh_cli_flags_install_marketplace_software_filter on WPCOM.
51     *
52     * @return array|WP_Error
53     */
54    protected function get_apd_marketplace_software() {
55        $atomic_persist_data = new Atomic_Persistent_Data();
56        if ( empty( $atomic_persist_data->WPCOM_MARKETPLACE_SOFTWARE ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
57            return new WP_Error( 'marketplace_software_not_found', 'WPCOM_Marketplace_Software Atomic persist data is empty. No Marketplace Software installed.' );
58        }
59
60        return json_decode( $atomic_persist_data->WPCOM_MARKETPLACE_SOFTWARE, true ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
61    }
62}