setInterval(24 * 3600); $this->setTimeSensitivity(self::TIME_INSENSITIVE); } /** * @inheritDoc */ #[\Override] public function run($argument) { $accounts = $this->accountMapper->getAllAccounts(); foreach ($accounts as $account) { $account = new Account($account); $retentionDays = $account->getMailAccount()->getTrashRetentionDays(); if ($retentionDays === null || $retentionDays <= 0) { continue; } $retentionSeconds = $retentionDays * 24 * 3600; try { $this->cleanTrash($account, $retentionSeconds); } catch (ServiceException|ClientException $e) { $this->logger->error('Could not clean trash mailbox', [ 'exception' => $e, 'userId' => $account->getUserId(), 'accountId' => $account->getId(), 'trashMailboxId' => $account->getMailAccount()->getTrashMailboxId(), ]); } } } /** * @throws ClientException * @throws ServiceException */ private function cleanTrash(Account $account, int $retentionSeconds): void { $trashMailboxId = $account->getMailAccount()->getTrashMailboxId(); if ($trashMailboxId === null) { return; } try { $trashMailbox = $this->mailboxMapper->findById($trashMailboxId); } catch (DoesNotExistException $e) { return; } $now = $this->time->getTime(); $messages = $this->messageMapper->findMessagesKnownSinceBefore( $trashMailboxId, $now - $retentionSeconds, ); if (count($messages) === 0) { return; } $client = $this->clientFactory->getClient($account); try { foreach ($messages as $message) { $this->mailManager->deleteMessageWithClient( $account, $trashMailbox, $message->getUid(), $client, ); $this->messageRetentionMapper->deleteByMailboxIdAndUid( $message->getMailboxId(), $message->getUid(), ); } } finally { $client->logout(); } } }