Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Add 24.3: Creating a CLI Command
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 13, 2022
1 parent 7a34cbb commit 1e10459
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Command/CommentCleanupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Command;

use App\Repository\CommentRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class CommentCleanupCommand extends Command
{
private $commentRepository;

protected static $defaultName = 'app:comment:cleanup';

public function __construct(CommentRepository $commentRepository)
{
$this->commentRepository = $commentRepository;

parent::__construct();
}

protected function configure()
{
$this
->setDescription('Deletes rejected and spam comments from the database')
->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 0;
}
}

0 comments on commit 1e10459

Please sign in to comment.