. */ namespace SP\Domain\CustomField\Services; use Exception; use SP\Core\Acl\ActionsInterface; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Common\Services\Service; use SP\Domain\CustomField\CustomFieldServiceInterface; use SP\Domain\CustomField\UpgradeCustomFieldDataServiceInterface; use SP\Infrastructure\Database\DatabaseInterface; use SP\Infrastructure\Database\QueryData; /** * Class UpgradeCustomFieldData * * @package SP\Domain\Upgrade\Services */ final class UpgradeCustomFieldDataService extends Service implements UpgradeCustomFieldDataServiceInterface { private CustomFieldServiceInterface $customFieldService; private DatabaseInterface $database; public function __construct( Application $application, CustomFieldServiceInterface $customFieldService, DatabaseInterface $database ) { parent::__construct($application); $this->database = $database; $this->customFieldService = $customFieldService; } /** * upgrade_300_18072902 * * @throws Exception */ public function upgrade_300_18072902(): void { $this->eventDispatcher->notifyEvent( 'upgrade.customField.start', new Event( $this, EventMessage::factory() ->addDescription(__u('Custom fields update')) ->addDescription(__FUNCTION__) ) ); try { $this->transactionAware(function () { foreach ($this->customFieldService->getAll() as $item) { $queryData = new QueryData(); $queryData->setQuery('UPDATE CustomFieldData SET moduleId = ? WHERE id = ? LIMIT 1'); $queryData->setParams([$this->moduleMapper($item->getModuleId()), $item->getId()]); $this->database->doQuery($queryData); $this->eventDispatcher->notifyEvent( 'upgrade.customField.process', new Event( $this, EventMessage::factory()->addDescription(__u('Field updated')) ) ); } }, $this->database); } catch (Exception $e) { processException($e); $this->eventDispatcher->notifyEvent('exception', new Event($e)); throw $e; } $this->eventDispatcher->notifyEvent( 'upgrade.customField.end', new Event( $this, EventMessage::factory() ->addDescription(__u('Custom fields update')) ->addDescription(__FUNCTION__) ) ); } private function moduleMapper(int $moduleId): int { switch ($moduleId) { case 10: return ActionsInterface::ACCOUNT; case 61: return ActionsInterface::CATEGORY; case 62: return ActionsInterface::CLIENT; case 71: return ActionsInterface::USER; case 72: return ActionsInterface::GROUP; } return $moduleId; } }