53 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Export;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\MainBundle\Entity\ExportGeneration;
use Chill\MainBundle\Entity\User;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManagerInterface;
final readonly class ExportGenerator
{
public function __construct(
private ExportManager $exportManager,
private StoredObjectManagerInterface $storedObjectManager,
private EntityManagerInterface $entityManager,
private ExportFormHelper $exportFormHelper,
) {}
public function generate(ExportGeneration $exportGeneration, User $user): void
{
$this->entityManager->wrapInTransaction(function () use ($exportGeneration) {
$object = $exportGeneration->getStoredObject();
$this->entityManager->refresh($exportGeneration, LockMode::PESSIMISTIC_WRITE);
$this->entityManager->refresh($object, LockMode::PESSIMISTIC_WRITE);
if (StoredObject::STATUS_PENDING !== $object->getStatus()) {
return;
}
$generation = $this->exportManager->generateExport(
$exportGeneration->getExportAlias(),
$centers = $this->exportFormHelper->savedExportDataToFormData($exportGeneration, 'centers'),
$this->exportFormHelper->savedExportDataToFormData($exportGeneration, 'export', ['picked_centers' => $centers]),
$this->exportFormHelper->savedExportDataToFormData($exportGeneration, 'formatter', ['picked_centers' => $centers]),
$user,
);
$this->storedObjectManager->write($exportGeneration->getStoredObject(), $generation->content, $generation->contentType);
});
}
}