ImportCli.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint\Cli; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blueprint\ImportSchema; |
| 6 | use Automattic\WooCommerce\Blueprint\ResultFormatters\CliResultFormatter; |
| 7 | |
| 8 | /** |
| 9 | * Class ImportCli |
| 10 | */ |
| 11 | class ImportCli { |
| 12 | /** |
| 13 | * Schema path |
| 14 | * |
| 15 | * @var string $schema_path The path to the schema file. |
| 16 | */ |
| 17 | private $schema_path; |
| 18 | |
| 19 | /** |
| 20 | * ImportCli constructor. |
| 21 | * |
| 22 | * @param string $schema_path The path to the schema file. |
| 23 | */ |
| 24 | public function __construct( $schema_path ) { |
| 25 | $this->schema_path = $schema_path; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Run the import process. |
| 30 | * |
| 31 | * @param array $optional_args Optional arguments. |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public function run( $optional_args ) { |
| 36 | try { |
| 37 | $blueprint = ImportSchema::create_from_file( $this->schema_path ); |
| 38 | } catch ( \Exception $e ) { |
| 39 | \WP_CLI::error( $e->getMessage() ); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | $results = $blueprint->import(); |
| 44 | |
| 45 | $result_formatter = new CliResultFormatter( $results ); |
| 46 | $is_success = $result_formatter->is_success(); |
| 47 | |
| 48 | if ( isset( $optional_args['show-messages'] ) ) { |
| 49 | $result_formatter->format( $optional_args['show-messages'] ); |
| 50 | } |
| 51 | |
| 52 | if ( $is_success ) { |
| 53 | \WP_CLI::success( "$this->schema_path imported successfully" ); |
| 54 | } else { |
| 55 | \WP_CLI::error( "Failed to import $this->schema_path. Run with --show-messages=all to debug" ); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 |