Skip to content

Commit

Permalink
refactor: improve (un)installation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 27, 2023
1 parent 6852315 commit a4398b4
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions myparcelnl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use MyParcelNL\Pdk\Base\Pdk as PdkInstance;
use MyParcelNL\Pdk\Facade\Installer;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Hooks\HasPdkCheckoutDeliveryOptionsHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkCheckoutHooks;
Expand Down Expand Up @@ -158,33 +159,21 @@ public function getOrderShippingCostExternal($params): bool
*/
public function install(): bool
{
$success = parent::install();

try {
Installer::install($this);
} catch (Throwable $e) {
$this->_errors[] = $e->getMessage();
$success = false;
}

return $success;
return parent::install()
&& $this->withErrorHandling(function () {
Installer::install($this);
});
}

/**
* @return bool
*/
public function uninstall(): bool
{
$success = true;

try {
Installer::uninstall($this);
} catch (Throwable $e) {
$this->_errors[] = $e->getMessage();
$success = false;
}

return parent::uninstall() && $success;
return $this->withErrorHandling(function () {
Installer::uninstall($this);
})
&& parent::uninstall();
}

/**
Expand All @@ -211,4 +200,23 @@ private function getVersionFromComposer(): string

return $composerData['version'];
}

/**
* @param callable $callback
*
* @return bool
*/
private function withErrorHandling(callable $callback): bool
{
try {
$callback();

return true;
} catch (Throwable $e) {
Logger::error("An error occurred: {$e->getMessage()}", ['exception' => $e->getTraceAsString()]);
$this->_errors[] = $e->getMessage();

return false;
}
}
}

0 comments on commit a4398b4

Please sign in to comment.