* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ namespace PrestaShop\Module\AutoUpgrade\Parameters; use Configuration; use Doctrine\Common\Collections\ArrayCollection; use Shop; /** * Contains the module configuration (form params). * * @extends ArrayCollection */ class UpgradeConfiguration extends ArrayCollection { const UPGRADE_CONST_KEYS = [ 'PS_AUTOUP_PERFORMANCE', 'PS_AUTOUP_CUSTOM_MOD_DESACT', 'PS_AUTOUP_UPDATE_DEFAULT_THEME', 'PS_AUTOUP_CHANGE_DEFAULT_THEME', 'PS_AUTOUP_UPDATE_RTL_FILES', 'PS_AUTOUP_KEEP_MAILS', 'PS_AUTOUP_BACKUP', 'PS_AUTOUP_KEEP_IMAGES', 'PS_DISABLE_OVERRIDES', ]; const PS_CONST_DEFAULT_VALUE = [ 'PS_AUTOUP_PERFORMANCE' => 1, 'PS_AUTOUP_CUSTOM_MOD_DESACT' => 1, 'PS_AUTOUP_UPDATE_DEFAULT_THEME' => 1, 'PS_AUTOUP_CHANGE_DEFAULT_THEME' => 0, 'PS_AUTOUP_UPDATE_RTL_FILES' => 1, 'PS_AUTOUP_KEEP_MAILS' => 0, 'PS_AUTOUP_BACKUP' => 1, 'PS_AUTOUP_KEEP_IMAGES' => 1, ]; /** * Performance settings, if your server has a low memory size, lower these values. * * @var array */ private const PERFORMANCE_VALUES = [ 'loopFiles' => [400, 800, 1600], // files 'loopTime' => [6, 12, 25], // seconds 'maxBackupFileSize' => [15728640, 31457280, 62914560], // bytes 'maxWrittenAllowed' => [4194304, 8388608, 16777216], // bytes ]; /** * Get the name of the new release archive. */ public function getArchiveFilename(): string { return $this->get('archive.filename'); } /** * Get the version included in the new release. */ public function getArchiveVersion(): string { return $this->get('archive.version_num'); } /** * Get channel selected on config panel (Minor, major ...). */ public function getChannel(): string { return $this->get('channel'); } /** * @return int Number of files to handle in a single call to avoid timeouts */ public function getNumberOfFilesPerCall(): int { return $this::PERFORMANCE_VALUES['loopFiles'][$this->getPerformanceLevel()]; } /** * @return int Number of seconds allowed before having to make another request */ public function getTimePerCall(): int { return $this::PERFORMANCE_VALUES['loopTime'][$this->getPerformanceLevel()]; } /** * @return int Kind of reference for SQL file creation, giving a file size before another request is needed */ public function getMaxSizeToWritePerCall(): int { return $this::PERFORMANCE_VALUES['maxWrittenAllowed'][$this->getPerformanceLevel()]; } /** * @return int Max file size allowed in backup */ public function getMaxFileToBackup(): int { return $this::PERFORMANCE_VALUES['maxBackupFileSize'][$this->getPerformanceLevel()]; } /** * @return int level of performance selected (0 for low, 2 for high) */ public function getPerformanceLevel(): int { return $this->get('PS_AUTOUP_PERFORMANCE') - 1; } public function shouldBackupFilesAndDatabase(): bool { return (bool) $this->get('PS_AUTOUP_BACKUP'); } /** * @return bool True if the autoupgrade module backup should include the images */ public function shouldBackupImages(): bool { return (bool) $this->get('PS_AUTOUP_KEEP_IMAGES'); } /** * @return bool True if non-native modules must be disabled during upgrade */ public function shouldDeactivateCustomModules(): bool { return (bool) $this->get('PS_AUTOUP_CUSTOM_MOD_DESACT'); } /** * @return bool true if we should keep the merchant emails untouched */ public function shouldKeepMails(): bool { return (bool) $this->get('PS_AUTOUP_KEEP_MAILS'); } /** * @return bool True if we have to set the native theme by default */ public function shouldSwitchToDefaultTheme(): bool { return (bool) $this->get('PS_AUTOUP_CHANGE_DEFAULT_THEME'); } /** * @return bool True if we are allowed to update the default theme files */ public function shouldUpdateDefaultTheme(): bool { return (bool) $this->get('PS_AUTOUP_UPDATE_DEFAULT_THEME'); } /** * @return bool True if we should update RTL files */ public function shouldUpdateRTLFiles(): bool { return (bool) $this->get('PS_AUTOUP_UPDATE_RTL_FILES'); } public static function isOverrideAllowed(): bool { return (bool) Configuration::get('PS_DISABLE_OVERRIDES'); } public static function updateDisabledOverride(bool $value, ?int $shopId = null): void { if ($shopId) { Configuration::updateValue('PS_DISABLE_OVERRIDES', $value, false, null, (int) $shopId); } else { Configuration::updateGlobalValue('PS_DISABLE_OVERRIDES', $value); } } public static function updatePSDisableOverrides(bool $value): void { foreach (Shop::getCompleteListOfShopsID() as $id_shop) { self::updateDisabledOverride($value, $id_shop); } self::updateDisabledOverride($value); } /** * @param array $array * * @return void */ public function merge(array $array = []): void { foreach ($array as $key => $value) { $this->set($key, $value); } } }