add mailing entity and commands

This commit is contained in:
Simon Vieille 2020-11-11 14:14:48 +01:00
parent 52426489ef
commit 569e20e301
Signed by: deblan
GPG Key ID: 03383D15A1D31745
10 changed files with 524 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?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;
}
}

View File

@ -0,0 +1,55 @@
<?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 App\Repository\MailingRepository;
class MailingListCommand extends Command
{
protected static $defaultName = 'mailing:list';
protected MailingRepository $repo;
public function __construct(MailingRepository $repo)
{
parent::__construct();
$this->repo = $repo;
}
protected function configure()
{
$this
->setDescription('List mailings')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$headers = ['Label', 'ID', 'Created at', 'Updated at'];
$rows = [];
$entities = $this->repo->findAll([], ['createdAt' => 'DEC']);
foreach ($entities as $entity) {
$rows[] = [
$entity->getLabel(),
$entity->getId(),
$entity->getCreatedAt()->format('Y-m-d H:i:s'),
$entity->getUpdatedAt()->format('Y-m-d H:i:s'),
];
}
$io->table($headers, $rows);
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,50 @@
<?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')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$label = $input->getArgument('label');
$entity = new Mailing();
$entity->setLabel($label);
$this->em->persist($entity);
$this->em->flush();
$io->success(sprintf('"%s" was created with id "%s"', $label, $entity->getId()));
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,62 @@
<?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 MailingRenameCommand extends Command
{
protected static $defaultName = 'mailing:rename';
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('Rename a mailing')
->addArgument('id', InputArgument::OPTIONAL, 'ID of the mailing')
->addArgument('label', InputArgument::OPTIONAL, 'New label')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$id = $input->getArgument('id');
$label = $input->getArgument('label');
$entity = $this->repo->find(['id' => $id]);
if (null === $entity) {
$io->error(sprintf('"%s" is not found!', $id));
return Command::FAILURE;
}
$entity->setLabel($label);
$this->em->persist($entity);
$this->em->flush();
$io->success(sprintf('"%s" was updated with label "%s"', $entity->getId(), $label));
return Command::SUCCESS;
}
}

0
src/Entity/.gitignore vendored Normal file
View File

110
src/Entity/Mail.php Normal file
View File

@ -0,0 +1,110 @@
<?php
namespace App\Entity;
use App\Repository\MailRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=MailRepository::class)
*/
class Mail
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Mailing::class, inversedBy="mails")
* @ORM\JoinColumn(nullable=false)
*/
private $mailing;
/**
* @ORM\Column(type="string", length=255)
*/
private $subject;
/**
* @ORM\Column(type="date")
*/
private $date;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $htmlContent;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $textContent;
public function getId(): ?int
{
return $this->id;
}
public function getMailing(): ?Mailing
{
return $this->mailing;
}
public function setMailing(?Mailing $mailing): self
{
$this->mailing = $mailing;
return $this;
}
public function getSubject(): ?string
{
return $this->subject;
}
public function setSubject(string $subject): self
{
$this->subject = $subject;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getHtmlContent(): ?string
{
return $this->htmlContent;
}
public function setHtmlContent(?string $htmlContent): self
{
$this->htmlContent = $htmlContent;
return $this;
}
public function getTextContent(): ?string
{
return $this->textContent;
}
public function setTextContent(?string $textContent): self
{
$this->textContent = $textContent;
return $this;
}
}

89
src/Entity/Mailing.php Normal file
View File

@ -0,0 +1,89 @@
<?php
namespace App\Entity;
use App\Repository\MailingRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Doctrine\Timestampable;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=MailingRepository::class)
* @ORM\HasLifecycleCallbacks
*/
class Mailing
{
use Timestampable;
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
* @ORM\Column(type="uuid")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $label;
/**
* @ORM\OneToMany(targetEntity=Mail::class, mappedBy="mailing", orphanRemoval=true)
*/
private $mails;
public function __construct()
{
$this->mails = new ArrayCollection();
}
public function getId(): ?string
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
/**
* @return Collection|Mail[]
*/
public function getMails(): Collection
{
return $this->mails;
}
public function addMail(Mail $mail): self
{
if (!$this->mails->contains($mail)) {
$this->mails[] = $mail;
$mail->setMailing($this);
}
return $this;
}
public function removeMail(Mail $mail): self
{
if ($this->mails->removeElement($mail)) {
// set the owning side to null (unless already changed)
if ($mail->getMailing() === $this) {
$mail->setMailing(null);
}
}
return $this;
}
}

0
src/Repository/.gitignore vendored Normal file
View File

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Mail;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Mail|null find($id, $lockMode = null, $lockVersion = null)
* @method Mail|null findOneBy(array $criteria, array $orderBy = null)
* @method Mail[] findAll()
* @method Mail[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class MailRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Mail::class);
}
// /**
// * @return Mail[] Returns an array of Mail objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('m')
->andWhere('m.exampleField = :val')
->setParameter('val', $value)
->orderBy('m.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Mail
{
return $this->createQueryBuilder('m')
->andWhere('m.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Mailing;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Mailing|null find($id, $lockMode = null, $lockVersion = null)
* @method Mailing|null findOneBy(array $criteria, array $orderBy = null)
* @method Mailing[] findAll()
* @method Mailing[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class MailingRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Mailing::class);
}
// /**
// * @return Mailing[] Returns an array of Mailing objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('m')
->andWhere('m.exampleField = :val')
->setParameter('val', $value)
->orderBy('m.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Mailing
{
return $this->createQueryBuilder('m')
->andWhere('m.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}