* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ namespace PrestaShop\Module\PsAccounts\Account\CommandHandler; use PrestaShop\Module\PsAccounts\Account\Command\UpgradeModuleCommand; use PrestaShop\Module\PsAccounts\Account\Command\UpgradeModuleMultiCommand; use PrestaShop\Module\PsAccounts\Account\Dto\UpgradeModule; use PrestaShop\Module\PsAccounts\Cqrs\CommandBus; use PrestaShop\Module\PsAccounts\Exception\DtoException; use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException; use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository; use PrestaShopDatabaseException; class UpgradeModuleMultiHandler { /** * @var ConfigurationRepository */ private $configRepo; /** * @var CommandBus */ private $commandBus; public function __construct( CommandBus $commandBus, ConfigurationRepository $configRepo ) { $this->commandBus = $commandBus; $this->configRepo = $configRepo; } /** * @param UpgradeModuleMultiCommand $command * * @return void */ public function handle(UpgradeModuleMultiCommand $command) { foreach ($this->getShops($this->configRepo->isMultishopActive()) as $id) { try { $this->commandBus->handle(new UpgradeModuleCommand(new UpgradeModule([ 'shopId' => $id, // FIXME: should be part of the command payload 'version' => \Ps_accounts::VERSION, ]))); } catch (RefreshTokenException $e) { } catch (DtoException $e) { } } } /** * @param bool $multishop * * @return array|null[] */ private function getShops($multishop) { $shops = [null]; if ($multishop) { $shops = []; $db = \Db::getInstance(); try { $result = $db->query('SELECT id_shop FROM ' . _DB_PREFIX_ . 'shop'); while ($row = $db->nextRow($result)) { /* @phpstan-ignore-next-line */ $shops[] = $row['id_shop']; } } catch (PrestaShopDatabaseException $e) { return []; } } return $shops; } }