<?php

use In2code\Powermail\Domain\Repository\MailRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;

/**
 * The PersistenceManager call is necessary when powermail redirects
 * to a "thank you" page. Otherwise it's persisted automatically.
 *
 * This is with TYPO3v11 and powermail 10.9.2
 */
class ExampleFinisher extends AbstractFinisher
{
    public function submitFinisher(): void
    {
        $apiData = ['foo' => 'bar'];

        $logAnswer = $this->getMail()->getAnswersByFieldMarker()['postdatalog'];
        $logAnswer->setValue($apiData);

        $mailRepository = GeneralUtility::makeInstance(MailRepository::class);
        $mailRepository->update($this->getMail());

        $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
        $persistenceManager->persistAll();
    }
}