Skip to content

Commit

Permalink
feat: refacto upgrade self check calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ga-devfront committed Dec 17, 2024
1 parent 155ab21 commit b210c1e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 64 deletions.
40 changes: 8 additions & 32 deletions classes/Commands/CheckRequirementsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
use PrestaShop\Module\AutoUpgrade\Exceptions\DistributionApiException;
use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Services\DistributionApiService;
use PrestaShop\Module\AutoUpgrade\Services\PhpVersionResolverService;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeSelfCheck;
Expand All @@ -46,8 +44,6 @@ class CheckRequirementsCommand extends AbstractCommand
/** @var string */
protected static $defaultName = 'update:check-requirements';
const MODULE_CONFIG_DIR = 'autoupgrade';
/** @var UpgradeSelfCheck */
private $upgradeSelfCheck;
/** @var OutputInterface */
private $output;
/** @var int */
Expand Down Expand Up @@ -96,26 +92,6 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$this->upgradeContainer->initPrestaShopCore();
$this->upgradeContainer->getState()->initDefault($this->upgradeContainer->getProperty(UpgradeContainer::PS_VERSION), $this->upgradeContainer->getUpgrader()->getDestinationVersion());

$distributionApiService = new DistributionApiService();
$phpVersionResolverService = new PhpVersionResolverService(
$distributionApiService,
$this->upgradeContainer->getFileLoader(),
$this->upgradeContainer->getState()->getCurrentVersion()
);

$this->upgradeSelfCheck = new UpgradeSelfCheck(
$this->upgradeContainer->getUpgrader(),
$this->upgradeContainer->getState(),
$this->upgradeContainer->getUpgradeConfiguration(),
$this->upgradeContainer->getPrestaShopConfiguration(),
$this->upgradeContainer->getTranslator(),
$phpVersionResolverService,
$this->upgradeContainer->getChecksumCompare(),
_PS_ROOT_DIR_,
_PS_ADMIN_DIR_,
$moduleConfigPath
);

$output->writeln('Result of prerequisite checks:');
$this->exitCode = ExitCode::SUCCESS;
$this->processRequirementWarnings();
Expand All @@ -137,8 +113,8 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
*/
private function processRequirementErrors(): void
{
foreach ($this->upgradeSelfCheck->getErrors() as $error => $value) {
$wording = $this->upgradeSelfCheck->getRequirementWording($error);
foreach ($this->upgradeContainer->getUpgradeSelfCheck()->getErrors() as $error => $value) {
$wording = $this->upgradeContainer->getUpgradeSelfCheck()->getRequirementWording($error);
$this->writeError($wording['message']);
if (isset($wording['list'])) {
foreach ($wording['list'] as $item) {
Expand All @@ -154,35 +130,35 @@ private function processRequirementErrors(): void
*/
private function processRequirementWarnings(): void
{
foreach ($this->upgradeSelfCheck->getWarnings() as $warning => $value) {
$wording = $this->upgradeSelfCheck->getRequirementWording($warning);
foreach ($this->upgradeContainer->getUpgradeSelfCheck()->getWarnings() as $warning => $value) {
$wording = $this->upgradeContainer->getUpgradeSelfCheck()->getRequirementWording($warning);
$this->writeWarning($wording['message']);

switch ($warning) {
case UpgradeSelfCheck::CORE_TEMPERED_FILES_LIST_NOT_EMPTY:
$missingFiles = $this->upgradeSelfCheck->getCoreMissingFiles();
$missingFiles = $this->upgradeContainer->getUpgradeSelfCheck()->getCoreMissingFiles();

$this->output->writeln("\t" . count($missingFiles) . ' files are missing:');
foreach ($missingFiles as $missingFile) {
$this->output->writeln("\t\t" . $missingFile);
}

$alteredFiles = $this->upgradeSelfCheck->getCoreAlteredFiles();
$alteredFiles = $this->upgradeContainer->getUpgradeSelfCheck()->getCoreAlteredFiles();

$this->output->writeln("\t" . count($alteredFiles) . ' files are altered:');
foreach ($alteredFiles as $alteredFile) {
$this->output->writeln("\t\t" . $alteredFile);
}
break;
case UpgradeSelfCheck::THEME_TEMPERED_FILES_LIST_NOT_EMPTY:
$missingFiles = $this->upgradeSelfCheck->getThemeMissingFiles();
$missingFiles = $this->upgradeContainer->getUpgradeSelfCheck()->getThemeMissingFiles();

$this->output->writeln("\t" . count($missingFiles) . ' files are missing:');
foreach ($missingFiles as $missingFile) {
$this->output->writeln("\t\t" . $missingFile);
}

$alteredFiles = $this->upgradeSelfCheck->getThemeAlteredFiles();
$alteredFiles = $this->upgradeContainer->getUpgradeSelfCheck()->getThemeAlteredFiles();

$this->output->writeln("\t" . count($alteredFiles) . ' files are altered:');
foreach ($alteredFiles as $alteredFile) {
Expand Down
62 changes: 52 additions & 10 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@ class UpgradeContainer
*/
private $prestashopVersionService;

/**
* @var UpgradeSelfCheck
*/
private $upgradeSelfCheck;

/**
* @var PhpVersionResolverService
*/
private $phpVersionResolverService;

/**
* @var DistributionApiService
*/
private $distributionApiService;

/**
* AdminSelfUpgrade::$autoupgradePath
* Ex.: /var/www/html/PrestaShop/admin-dev/autoupgrade.
Expand Down Expand Up @@ -685,33 +700,60 @@ public function getUpgradeConfigurationStorage(): UpgradeConfigurationStorage
return new UpgradeConfigurationStorage($this->getProperty(self::WORKSPACE_PATH) . DIRECTORY_SEPARATOR);
}

public function getDistributionApiService(): DistributionApiService
{
if (null !== $this->distributionApiService) {
return $this->distributionApiService;
}

$this->distributionApiService = new DistributionApiService();

return $this->distributionApiService;
}

/**
* @throws Exception
*/
public function getUpgradeSelfCheck(): UpgradeSelfCheck
public function getPhpVersionResolverService(): PhpVersionResolverService
{
$this->initPrestaShopCore();
$state = $this->getState();
if (null !== $this->phpVersionResolverService) {
return $this->phpVersionResolverService;
}

$distributionApiService = new DistributionApiService();
$phpVersionResolverService = new PhpVersionResolverService(
$distributionApiService,
$this->phpVersionResolverService = new PhpVersionResolverService(
$this->getDistributionApiService(),
$this->getFileLoader(),
$state->getCurrentVersion()
$this->getState()->getCurrentVersion()
);

return new UpgradeSelfCheck(
return $this->phpVersionResolverService;
}

/**
* @throws Exception
*/
public function getUpgradeSelfCheck(): UpgradeSelfCheck
{
if (null !== $this->upgradeSelfCheck) {
return $this->upgradeSelfCheck;
}

$this->initPrestaShopCore();

$this->upgradeSelfCheck = new UpgradeSelfCheck(
$this->getUpgrader(),
$state,
$this->getState(),
$this->getUpgradeConfiguration(),
$this->getPrestaShopConfiguration(),
$this->getTranslator(),
$phpVersionResolverService,
$this->getPhpVersionResolverService(),
$this->getChecksumCompare(),
_PS_ROOT_DIR_,
_PS_ADMIN_DIR_,
$this->getProperty(UpgradeContainer::WORKSPACE_PATH)
);

return $this->upgradeSelfCheck;
}

/**
Expand Down
24 changes: 2 additions & 22 deletions controllers/admin/AdminSelfUpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Router\Router;
use PrestaShop\Module\AutoUpgrade\Services\DistributionApiService;
use PrestaShop\Module\AutoUpgrade\Services\PhpVersionResolverService;
use PrestaShop\Module\AutoUpgrade\Tools14;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradePage;
use PrestaShop\Module\AutoUpgrade\UpgradeSelfCheck;
use Symfony\Component\HttpFoundation\Request;

class AdminSelfUpgradeController extends ModuleAdminController
Expand Down Expand Up @@ -485,30 +482,13 @@ public function initContent()
}

$upgrader = $this->upgradeContainer->getUpgrader();
$distributionApiService = new DistributionApiService();
$phpVersionResolverService = new PhpVersionResolverService(
$distributionApiService,
$this->upgradeContainer->getFileLoader(),
$this->upgradeContainer->getState()->getCurrentVersion()
);
$upgradeSelfCheck = new UpgradeSelfCheck(
$upgrader,
$this->upgradeContainer->getState(),
$this->upgradeContainer->getUpgradeConfiguration(),
$this->upgradeContainer->getPrestaShopConfiguration(),
$this->upgradeContainer->getTranslator(),
$phpVersionResolverService,
$this->upgradeContainer->getChecksumCompare(),
$this->prodRootDir,
$this->adminDir,
$this->autoupgradePath
);

$response = new AjaxResponse($this->upgradeContainer->getState(), $this->upgradeContainer->getLogger());
$this->content = (new UpgradePage(
$this->upgradeContainer->getUpgradeConfiguration(),
$this->upgradeContainer->getTwig(),
$this->upgradeContainer->getTranslator(),
$upgradeSelfCheck,
$this->upgradeContainer->getUpgradeSelfCheck(),
$upgrader,
$backupFinder,
$this->autoupgradePath,
Expand Down

0 comments on commit b210c1e

Please sign in to comment.