Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Marketplace_Software_Factory
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 get_product_software
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
90
 get_product_installer
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2/**
3 * Factory class to create a new instance of Marketplace_Product_Software and Marketplace_Product_Installer.
4 *
5 * @since 5.5.0
6 * @package WPCOM_Marketplace
7 */
8require_once __DIR__ . '/products/class-marketplace-product-software.php';
9require_once __DIR__ . '/products/class-marketplace-plugin-software.php';
10require_once __DIR__ . '/products/class-marketplace-theme-software.php';
11require_once __DIR__ . '/installers/class-marketplace-product-installer.php';
12require_once __DIR__ . '/installers/class-marketplace-plugin-installer.php';
13require_once __DIR__ . '/installers/class-marketplace-theme-installer.php';
14require_once __DIR__ . '/class-marketplace-command-helper.php';
15
16/**
17 *  Factory class to create a new instance of Marketplace_Product_Software.
18 *
19 *  This class is responsible for creating a new instance of Marketplace_Product_Software based on the product type.
20 *  This class also validates the input data before creating the instance.
21 */
22class Marketplace_Software_Factory {
23
24    /**
25     * Create a new instance of Marketplace_Product_Software depending on the product type.
26     *
27     * @param array $software The software data.
28     *
29     * array{
30     *      'product_type': string,
31     *      'software_slug': string,
32     *      'download_url': string,
33     *      'is_managed': bool,
34     *      'plugin_dependencies': array<string>,
35     *      'theme_dependencies': array<string>,
36     * } The software data.
37     *
38     * @return Marketplace_Product_Software|WP_Error
39     */
40    public static function get_product_software( array $software ) {
41        if ( ! isset( $software['product_type'] ) ) {
42            return new WP_Error( 'missing_product_type', 'Product type is missing.' );
43        }
44
45        if ( ! isset( $software['software_slug'] ) ) {
46            return new WP_Error( 'missing_software_slug', 'Software slug is missing.' );
47        }
48
49        if ( ! isset( $software['is_managed'] ) ) {
50            return new WP_Error( 'missing_is_managed', 'Is managed is missing.' );
51        }
52
53        if ( ! $software['is_managed'] && ! isset( $software['download_url'] ) ) {
54            return new WP_Error( 'missing_download_url', 'Download URL is missing.' );
55        }
56
57        switch ( $software['product_type'] ) {
58            case 'plugin':
59                return new Marketplace_Plugin_Software(
60                    $software['software_slug'],
61                    $software['is_managed'],
62                    $software['download_url'],
63                    $software['plugin_dependencies'],
64                    $software['theme_dependencies']
65                );
66            case 'theme':
67                return new Marketplace_Theme_Software(
68                    $software['software_slug'],
69                    $software['is_managed'],
70                    $software['download_url'],
71                    $software['plugin_dependencies']
72                );
73            default:
74                return new WP_Error( 'invalid_product_type', 'Invalid product type.' );
75        }
76    }
77
78    /**
79     * Get the product installer instance depending on the product type.
80     *
81     * @param Marketplace_Product_Software $product_software The product software.
82     *
83     * @return Marketplace_Product_Installer|WP_Error
84     */
85    public static function get_product_installer( Marketplace_Product_Software $product_software ) {
86        $command_helper = new Marketplace_Command_Helper();
87
88        if ( $product_software instanceof Marketplace_Plugin_Software ) {
89            return new Marketplace_Plugin_Installer( $command_helper, $product_software );
90        }
91
92        if ( $product_software instanceof Marketplace_Theme_Software ) {
93            return new Marketplace_Theme_Installer( $command_helper, $product_software );
94        }
95
96        return new WP_Error( 'invalid_product_type', 'Invalid product type.' );
97    }
98}