mail-rss/src/Command/MailingNewCommand.php

54 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\Entity\Mailing;
class MailingNewCommand extends Command
{
protected static $defaultName = 'mailing:new';
protected EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
parent::__construct();
$this->em = $em;
}
protected function configure()
{
$this
->setDescription('Create a new mailing')
->addArgument('label', InputArgument::REQUIRED, 'Label of the mailing')
->addOption('public', 'p', InputOption::VALUE_NONE, 'Make the mailing public')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$label = $input->getArgument('label');
$entity = new Mailing();
$entity
->setLabel($label)
->setIsPublic($input->getOption('public'));
$this->em->persist($entity);
$this->em->flush();
$io->success(sprintf('"%s" was created with id "%s"', $label, $entity->getId()));
return Command::SUCCESS;
}
}