Skip to content

Commit

Permalink
✨ Make the mbo installation through ajax process
Browse files Browse the repository at this point in the history
  • Loading branch information
intraordinaire committed Mar 10, 2023
1 parent 1282ad3 commit 7d5f526
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/DependencyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

namespace Prestashop\ModuleLibMboInstaller;

use Prestashop\ModuleLibGuzzleAdapter\Interfaces\ClientExceptionInterface;
use Symfony\Component\Routing\Router;

class DependencyBuilder
{
const DEPENDENCY_FILENAME = 'ps_dependencies.json';
const GET_PARAMETER = 'mbo_action_needed';
const INSTALL_ACTION = 'install';
const ENABLE_ACTION = 'enable';
const APP_STATE_LAUNCHABLE = 'launchable';
const APP_STATE_MBO_IN_PROGRESS = 'mbo_in_progress';
const APP_STATE_AUTOSTART = 'autostart';

/**
* @var \ModuleCore
Expand Down Expand Up @@ -46,78 +40,82 @@ public function __construct($module)
* "ps_version": string,
* "php_version": string,
* "locale": string,
* "app_state": string,
* "dependencies": array{}|array{ps_mbo: array<string, bool|string>}
* }
*
* @throws \Exception|ClientExceptionInterface
* @throws \Exception
*/
public function handleDependencies()
{
$appState = $this->handleMboInstallation();
$this->handleMboInstallation();

return $this->buildDependenciesContext($appState);
return $this->buildDependenciesContext();
}

/**
* Install or enable the MBO depending on the action requested
*
* @return string
*
* @throws \Exception|ClientExceptionInterface
* @return void
*/
protected function handleMboInstallation()
{
if (!isset($_GET[self::GET_PARAMETER]) || !$this->isMboNeeded()) {
return self::APP_STATE_LAUNCHABLE;
return;
}

$mboStatus = (new Presenter())->present();
$installer = new Installer(_PS_VERSION_);

if ($mboStatus['isInstalled'] && $mboStatus['isEnabled']) {
return self::APP_STATE_AUTOSTART;
return;
}

if (!$mboStatus['isInstalled']) {
$installer->installModule();
} elseif (!$mboStatus['isEnabled']) {
$installer->enableModule();
$data = [Installer::MODULE_NAME => [
'status' => true,
]];

try {
if (!$mboStatus['isInstalled']) {
$installer->installModule();
} elseif (!$mboStatus['isEnabled']) {
$installer->enableModule();
}
} catch (\Exception $e) {
$data[Installer::MODULE_NAME] = [
'status' => false,
'msg' => $e->getMessage(),
];
}

// Force another refresh of the page to correctly clear the cache and load MBO configurations
header('Refresh:0');
// To avoid wasting time rerendering the entire page, die immediately
return self::APP_STATE_MBO_IN_PROGRESS;
// This call is done in ajax by the CDC, bypass the normal return
header('Content-type: application/json');
echo json_encode($data);
exit();
}

/**
* Build the dependencies data array to be given to the CDC
*
* @param string $appState
*
* @return array{
* "module_display_name": string,
* "module_name": string,
* "module_version": string,
* "ps_version": string,
* "php_version": string,
* "locale": string,
* "app_state": string,
* "dependencies": array{}|array{ps_mbo: array<string, bool|string>}
* }
*
* @throws \Exception
*/
protected function buildDependenciesContext($appState = self::APP_STATE_LAUNCHABLE)
protected function buildDependenciesContext()
{
$data = [
'module_display_name' => (string) $this->module->displayName,
'module_name' => (string) $this->module->name,
'module_version' => (string) $this->module->version,
'ps_version' => (string) _PS_VERSION_,
'php_version' => (string) PHP_VERSION,
'app_state' => $appState,
'dependencies' => [],
];

Expand Down

0 comments on commit 7d5f526

Please sign in to comment.