59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use App\Repository\MailingRepository;
|
|
|
|
class MailingDeleteCommand extends Command
|
|
{
|
|
protected static $defaultName = 'mailing:delete';
|
|
|
|
protected EntityManagerInterface $em;
|
|
|
|
protected MailingRepository $repo;
|
|
|
|
public function __construct(EntityManagerInterface $em, MailingRepository $repo)
|
|
{
|
|
parent::__construct();
|
|
|
|
$this->em = $em;
|
|
$this->repo = $repo;
|
|
}
|
|
|
|
protected function configure()
|
|
{
|
|
$this
|
|
->setDescription('Delete a mailing')
|
|
->addArgument('id', InputArgument::OPTIONAL, 'ID of the mailing')
|
|
;
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$io = new SymfonyStyle($input, $output);
|
|
$id = $input->getArgument('id');
|
|
|
|
$entity = $this->repo->find(['id' => $id]);
|
|
|
|
if (null === $entity) {
|
|
$io->error(sprintf('"%s" is not found!', $id));
|
|
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$this->em->remove($entity);
|
|
$this->em->flush();
|
|
|
|
$io->success(sprintf('"%s" was deleted', $id));
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|