mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
Add command to remove old draft accompanying periods
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Service\AccompanyingPeriod;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class OldDraftAccompanyingPeriodRemover implements OldDraftAccompanyingPeriodRemoverInterface
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
private LoggerInterface $logger;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, LoggerInterface $logger)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function remove(DateInterval $interval): void
|
||||
{
|
||||
$this->logger->debug('[' . __CLASS__ . '] start to remove old periods', [
|
||||
'interval' => $interval->format('%d days'),
|
||||
]);
|
||||
|
||||
$beforeDate = (new DateTimeImmutable('now'))->sub($interval);
|
||||
|
||||
$results = $this->em->wrapInTransaction(static function (EntityManagerInterface $em) use ($beforeDate) {
|
||||
$subDQL = 'SELECT p FROM ' . AccompanyingPeriod::class . ' p WHERE p.createdAt < :beforeDate AND p.step = :draft';
|
||||
$parameters = [
|
||||
'beforeDate' => $beforeDate,
|
||||
'draft' => AccompanyingPeriod::STEP_DRAFT,
|
||||
];
|
||||
|
||||
$resources = $em->createQuery(
|
||||
'DELETE ' . AccompanyingPeriod\Resource::class . " r WHERE r.accompanyingPeriod IN ({$subDQL})"
|
||||
)
|
||||
->setParameters($parameters)
|
||||
->getSingleScalarResult();
|
||||
|
||||
$participations = $em->createQuery(
|
||||
'DELETE ' . AccompanyingPeriodParticipation::class . " part WHERE part.accompanyingPeriod IN ({$subDQL})"
|
||||
)
|
||||
->setParameters($parameters)
|
||||
->getSingleScalarResult();
|
||||
|
||||
$userHistory = $em->createQuery(
|
||||
'DELETE ' . AccompanyingPeriod\UserHistory::class . " h WHERE h.accompanyingPeriod IN ({$subDQL})"
|
||||
)
|
||||
->setParameters($parameters)
|
||||
->getSingleScalarResult();
|
||||
|
||||
$comments = $em->createQuery(
|
||||
'DELETE ' . AccompanyingPeriod\Comment::class . " c WHERE c.accompanyingPeriod IN ({$subDQL})"
|
||||
)
|
||||
->setParameters($parameters)
|
||||
->getSingleScalarResult();
|
||||
|
||||
$documents = $em->createQuery(
|
||||
'DELETE ' . AccompanyingCourseDocument::class . " d WHERE d.course IN ({$subDQL})"
|
||||
)
|
||||
->setParameters($parameters)
|
||||
->getSingleScalarResult();
|
||||
|
||||
$periods = $em
|
||||
->createQuery(
|
||||
'DELETE ' . AccompanyingPeriod::class . ' p WHERE p.createdAt < :beforeDate
|
||||
AND p.step = :draft'
|
||||
)
|
||||
->setParameter(':beforeDate', $beforeDate, Types::DATETIME_IMMUTABLE)
|
||||
->setParameter(':draft', AccompanyingPeriod::STEP_DRAFT)
|
||||
->getSingleScalarResult();
|
||||
|
||||
return [
|
||||
'comments' => $comments,
|
||||
'documents' => $documents,
|
||||
'participations' => $participations,
|
||||
'periods' => $periods,
|
||||
'resources' => $resources,
|
||||
'userHistory' => $userHistory,
|
||||
];
|
||||
});
|
||||
|
||||
$this->logger->info('[' . __CLASS__ . '] periods removed', array_merge($results, [
|
||||
'interval' => $interval->format('%d days'),
|
||||
]));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user