| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Builder\Factory\TemplateFactory; |
| 7 |
use Templately\Core\Importer\FullSiteImport; |
| 8 |
use Templately\Core\Importer\Import; |
| 9 |
use Templately\Core\Importer\LogHelper; |
| 10 |
use Templately\Core\Importer\Utils\ImportHelper; |
| 11 |
use Templately\Core\Importer\Utils\Utils; |
| 12 |
|
| 13 |
abstract class BaseRunner { |
| 14 |
use LogHelper; |
| 15 |
|
| 16 |
const META_SESSION_KEY = '_templately_import_session_id'; |
| 17 |
/** |
| 18 |
* @var FullSiteImport |
| 19 |
*/ |
| 20 |
protected $origin; |
| 21 |
protected $platform; |
| 22 |
protected $manifest; |
| 23 |
protected $dir_path; |
| 24 |
protected $session_id; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var ImportHelper |
| 28 |
*/ |
| 29 |
protected $json; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var TemplateFactory |
| 33 |
*/ |
| 34 |
protected $factory; |
| 35 |
|
| 36 |
/** |
| 37 |
* @throws Exception |
| 38 |
*/ |
| 39 |
public function __construct( FullSiteImport $full_site_import ) { |
| 40 |
$this->origin = $full_site_import; |
| 41 |
$this->dir_path = $full_site_import->dir_path; |
| 42 |
$this->manifest = &$full_site_import->manifest; |
| 43 |
$this->platform = $this->manifest['platform'] ?? ''; |
| 44 |
$this->session_id = $full_site_import->session_id; |
| 45 |
|
| 46 |
|
| 47 |
$this->factory = new TemplateFactory( $this->platform ); |
| 48 |
$this->json = Utils::get_json_helper( $this->platform ); |
| 49 |
|
| 50 |
if ( empty( $this->platform ) ) { |
| 51 |
throw new Exception( __( 'Platform is not specified. Please try again after specifying the platform.', 'templately' ) ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public function should_log(): bool { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
public function get_action(): string { |
| 60 |
return 'updateLog'; |
| 61 |
} |
| 62 |
|
| 63 |
abstract public function get_name(): string; |
| 64 |
|
| 65 |
abstract public function get_label(): string; |
| 66 |
|
| 67 |
abstract public function should_run( $data, $imported_data = [] ): bool; |
| 68 |
|
| 69 |
abstract public function import( $data, $imported_data ): array; |
| 70 |
|
| 71 |
abstract public function log_message() : string; |
| 72 |
|
| 73 |
public function log( $progress = 0, $message = null, $action = null ) { |
| 74 |
if( ! $this->should_log() ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
if( $message == null ) { |
| 79 |
$message = $this->log_message(); |
| 80 |
} |
| 81 |
|
| 82 |
if( $action == null ) { |
| 83 |
$action = $this->get_action(); |
| 84 |
} |
| 85 |
|
| 86 |
$this->sse_log( $this->get_name(), $message, min( $progress, 100 ), $action ); |
| 87 |
} |
| 88 |
} |