Use DI to inject MP2Migrator to MP2Migrator API endpoint and Changelog

[MAILPOET-2203]
This commit is contained in:
Rostislav Wolny
2019-07-24 15:13:11 +02:00
committed by Rostislav Wolný
parent 92495b20a5
commit 47861cdf19
2 changed files with 13 additions and 7 deletions

View File

@@ -11,10 +11,12 @@ class MP2Migrator extends APIEndpoint {
public $permissions = [ public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS, 'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
]; ];
/** @var \MailPoet\Config\MP2Migrator */
private $MP2Migrator; private $MP2Migrator;
public function __construct() { public function __construct(\MailPoet\Config\MP2Migrator $MP2Migrator) {
$this->MP2Migrator = new \MailPoet\Config\MP2Migrator(); $this->MP2Migrator = $MP2Migrator;
} }
/** /**
@@ -25,7 +27,7 @@ class MP2Migrator extends APIEndpoint {
*/ */
public function import($data) { public function import($data) {
try { try {
$process = $this->MP2Migrator->import($data); $process = $this->MP2Migrator->import();
return $this->successResponse($process); return $this->successResponse($process);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->errorResponse([ return $this->errorResponse([

View File

@@ -20,16 +20,21 @@ class Changelog {
/** @var Url */ /** @var Url */
private $url_helper; private $url_helper;
/** @var MP2Migrator */
private $mp2_migrator;
function __construct( function __construct(
SettingsController $settings, SettingsController $settings,
WPFunctions $wp, WPFunctions $wp,
Helper $wooCommerceHelper, Helper $wooCommerceHelper,
Url $url_helper Url $url_helper,
MP2Migrator $mp2_migrator
) { ) {
$this->wooCommerceHelper = $wooCommerceHelper; $this->wooCommerceHelper = $wooCommerceHelper;
$this->settings = $settings; $this->settings = $settings;
$this->wp = $wp; $this->wp = $wp;
$this->url_helper = $url_helper; $this->url_helper = $url_helper;
$this->mp2_migrator = $mp2_migrator;
} }
function init() { function init() {
@@ -67,13 +72,12 @@ class Changelog {
} }
private function checkMp2Migration($version) { private function checkMp2Migration($version) {
$mp2_migrator = new MP2Migrator(); if (!in_array($_GET['page'], ['mailpoet-migration', 'mailpoet-settings']) && $this->mp2_migrator->isMigrationStartedAndNotCompleted()) {
if (!in_array($_GET['page'], ['mailpoet-migration', 'mailpoet-settings']) && $mp2_migrator->isMigrationStartedAndNotCompleted()) {
// Force the redirection if the migration has started but is not completed // Force the redirection if the migration has started but is not completed
return $this->terminateWithRedirect($this->wp->adminUrl('admin.php?page=mailpoet-migration')); return $this->terminateWithRedirect($this->wp->adminUrl('admin.php?page=mailpoet-migration'));
} }
if ($version === null && $mp2_migrator->isMigrationNeeded()) { if ($version === null && $this->mp2_migrator->isMigrationNeeded()) {
$this->terminateWithRedirect($this->wp->adminUrl('admin.php?page=mailpoet-migration')); $this->terminateWithRedirect($this->wp->adminUrl('admin.php?page=mailpoet-migration'));
} }
} }