Closed FabRiviere closed 1 year ago
<?php
namespace App\Repository;
use App\Entity\Comment;
use App\Entity\Conference;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Comment>
*
* @method Comment|null find($id, $lockMode = null, $lockVersion = null)
* @method Comment|null findOneBy(array $criteria, array $orderBy = null)
* @method Comment[] findAll()
* @method Comment[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CommentRepository extends ServiceEntityRepository
{
public const PAGINATOR_PER_PAGE = 2;
private const DAYS_BEFORE_REJECTED_REMOVAL = 7;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Comment::class);
}
public function getCommentPaginator(Conference $conference, int $offset): Paginator
{
$query = $this->createQueryBuilder('c')
->andWhere('c.conference = :conference')
->andWhere('c.state = :state')
->setParameter('conference', $conference)
->setParameter('state', 'published')
->orderBy('c.createdAt', 'DESC')
->setMaxResults(self::PAGINATOR_PER_PAGE)
->setFirstResult($offset)
->getQuery()
;
return new Paginator($query);
}
public function countOldRejected(): int
{
return $this->getOldRejectedQueryBuilder()->select('COUNT(c.id)')->getQuery()->getSingleScalarResult();
}
public function deleteOldRejected(): int
{
return $this->getOldRejectedQueryBuilder()->delete()->getQuery()->execute();
}
private function getOldRejectedQueryBuilder(): QueryBuilder
{
return $this->createQueryBuilder('c')
->andWhere('c.state = :state_rejected or c.state = :state_spam')
->andWhere('c.createdAt < :date')
->setParameters([
'state_rejected' => 'rejected',
'state_spam' => 'spam',
'date' => new \DateTimeImmutable(-self::DAYS_BEFORE_REJECTED_REMOVAL.' days'),
])
;
}
}
La commande créée sera :
app:comment:cleanup
Le code du fichier CommentCleanupCommand.php qui sera créé dans un nouveau dossier src/command:
<?php
namespace App\Command;
use App\Repository\CommentRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand('app:comment:cleanup', 'Deletes rejected and spam comments from the database')]
class CommentCleanupCommand extends Command
{
public function __construct(private CommentRepository $commentRepository)
{
parent::__construct();
}
protected function configure()
{
$this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dry run');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
if ($input->getOption('dry-run')) {
$io->note('Dry mode enabled');
$count = $this->commentRepository->countOldRejected();
} else {
$count = $this->commentRepository->deleteOldRejected();
}
$io->success(sprintf('Deleted "%d" old rejected/spam comments.', $count));
return Command::SUCCESS;
}
}
crons:
security-check:
# Check that no security issues have been found for PHP packages deployed in production
# See https://github.com/fabpot/local-php-security-checker
spec: '50 23 * * *'
cmd: if [ "$PLATFORM_ENVIRONMENT_TYPE" = "production" ]; then croncape php-security-checker; fi
comment_cleanup:
# Cleanup every night at 11:50 pm (UTC)
spec: '50 23 * * *'
cmd: if [ "$PLATFORM_ENVIRONMENT_TYPE" = "production" ]; then croncape symfony console app:comment:cleanup; fi