* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ declare(strict_types=1); namespace PrestaShop\Module\Psshipping\Domain\Accounts; use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManager as LegacyModuleManager; use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder; use PrestaShop\PrestaShop\Core\Module\ModuleManager; use PrestaShop\PsAccountsInstaller\Installer\Facade\PsAccounts; use PrestaShop\PsAccountsInstaller\Installer\Installer as PsAccountsInstaller; use Psshipping; if (!defined('_PS_VERSION_')) { exit(); } class AccountsService { /** * @param Psshipping $module * * @return array * * @throws AccountsIsNotInstalledException */ public function getAccountsContext(Psshipping $module, bool $onInstall = false): array { /** @var ModuleManagerBuilder $moduleManagerBuilder */ $moduleManagerBuilder = ModuleManagerBuilder::getInstance(); if (version_compare(_PS_VERSION_, '1.7.7.0', '>=')) { /** @var ModuleManager $moduleManager */ $moduleManager = $moduleManagerBuilder->build(); } else { /** @var LegacyModuleManager $moduleManager */ $moduleManager = $moduleManagerBuilder->build(); } $psAccountsIsInstalled = $moduleManager->isInstalled('ps_accounts'); if (!$psAccountsIsInstalled) { throw new AccountsIsNotInstalledException('PrestaShop accounts (ps_accounts) module is not installed, please install it.'); } // we use this condition because we can't get the facade on install if ($onInstall === true) { /** @var PsAccounts $accounts */ $accounts = new PsAccounts(new PsAccountsInstaller('5')); } else { /** @var PsAccounts $accounts */ $accounts = $module->get('ps_accounts.facade'); } /** @var AbstractAccountsType $accountPresenter */ $accountPresenter = $accounts->getPsAccountsPresenter(); return $accountPresenter->present($module->name); } public function getPsAccountToken(Psshipping $module): string { /** @var PsAccounts $accounts */ $accounts = $module->get('ps_accounts.facade'); /** @var AbstractAccountsType $accountService */ $accountService = $accounts->getPsAccountsService(); return $accountService->getOrRefreshToken(); } }