. */ namespace SP\Domain\Upgrade\Services; use Exception; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Core\Exceptions\SPException; use SP\Domain\Account\Services\UpgradePublicLinkService; use SP\Domain\Account\UpgradePublicLinkServiceInterface; use SP\Domain\Auth\Services\UpgradeAuthTokenService; use SP\Domain\Auth\UpgradeAuthTokenServiceInterface; use SP\Domain\Common\Services\Service; use SP\Domain\Config\In\ConfigDataInterface; use SP\Domain\CustomField\Services\UpgradeCustomFieldDataService; use SP\Domain\CustomField\Services\UpgradeCustomFieldDefinitionService; use SP\Domain\CustomField\UpgradeCustomFieldDataServiceInterface; use SP\Domain\CustomField\UpgradeCustomFieldDefinitionServiceInterface; use SP\Domain\Plugin\UpgradePluginServiceInterface; use SP\Domain\Upgrade\UpgradeAppServiceInterface; use SP\Infrastructure\File\FileException; use SP\Providers\Log\FileLogHandler; use SP\Util\VersionUtil; /** * Class UpgradeAppService * * @package SP\Domain\Upgrade\Services */ final class UpgradeAppService extends Service implements UpgradeAppServiceInterface { private const UPGRADES = [ '300.18010101', '300.18072901', '300.18072902', '310.19012201', '310.19042701', ]; private UpgradeCustomFieldDefinitionService $upgradeCustomFieldDefinition; private UpgradePublicLinkService $upgradePublicLink; private UpgradeAuthTokenService $upgradeAuthToken; private UpgradeCustomFieldDataService $upgradeCustomFieldData; private UpgradePluginService $upgradePlugin; public function __construct( Application $application, FileLogHandler $fileLogHandler, UpgradeCustomFieldDefinitionServiceInterface $upgradeCustomFieldDefinition, UpgradePublicLinkServiceInterface $upgradePublicLink, UpgradeAuthTokenServiceInterface $upgradeAuthToken, UpgradeCustomFieldDataServiceInterface $upgradeCustomFieldData, UpgradePluginServiceInterface $upgradePlugin ) { parent::__construct($application); $this->eventDispatcher->attach($fileLogHandler); $this->upgradeCustomFieldDefinition = $upgradeCustomFieldDefinition; $this->upgradePublicLink = $upgradePublicLink; $this->upgradeAuthToken = $upgradeAuthToken; $this->upgradeCustomFieldData = $upgradeCustomFieldData; $this->upgradePlugin = $upgradePlugin; } public static function needsUpgrade(string $version): bool { return empty($version) || VersionUtil::checkVersion($version, self::UPGRADES); } /** * @throws UpgradeException * @throws FileException */ public function upgrade( string $version, ConfigDataInterface $configData ): void { $this->eventDispatcher->notifyEvent( 'upgrade.app.start', new Event( $this, EventMessage::factory()->addDescription(__u('Update Application')) ) ); foreach (self::UPGRADES as $appVersion) { if (VersionUtil::checkVersion($version, $appVersion)) { if ($this->applyUpgrade($appVersion) === false) { throw new UpgradeException( __u('Error while applying the application update'), SPException::CRITICAL, __u('Please, check the event log for more details') ); } logger('APP Upgrade: '.$appVersion); $configData->setAppVersion($appVersion); $this->config->saveConfig($configData, false); } } $this->eventDispatcher->notifyEvent( 'upgrade.app.end', new Event( $this, EventMessage::factory()->addDescription(__u('Update Application')) ) ); } /** * Actualizaciones de la aplicación */ private function applyUpgrade(string $version): bool { try { switch ($version) { case '300.18010101': $this->upgradeCustomFieldDefinition->upgrade_300_18010101(); $this->upgradePublicLink->upgrade_300_18010101(); return true; case '300.18072901': $this->upgradeCustomFieldDefinition->upgrade_300_18072901(); $this->upgradeAuthToken->upgrade_300_18072901(); return true; case '300.18072902': $this->upgradeCustomFieldData->upgrade_300_18072902(); return true; case '310.19012201': $this->upgradePlugin->upgrade_310_19012201(); return true; case '310.19042701': $this->upgradeCustomFieldDefinition->upgrade_310_19042701(); return true; } } catch (Exception $e) { processException($e); } return false; } }