From 1d08cfdc09c0a4fb0f53e4400900dd2050c97a61 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Fri, 21 Jul 2023 20:47:24 +0300 Subject: [PATCH] DTO for paste data added, Paste Entity changed --- src/Controller/PasteController.php | 27 +++-- src/DTO/PasteFormData.php | 122 +++++++++++++++++++++ src/Entity/Paste.php | 169 ++++------------------------- 3 files changed, 163 insertions(+), 155 deletions(-) create mode 100644 src/DTO/PasteFormData.php diff --git a/src/Controller/PasteController.php b/src/Controller/PasteController.php index 6ab962c..495623d 100644 --- a/src/Controller/PasteController.php +++ b/src/Controller/PasteController.php @@ -3,8 +3,9 @@ declare(strict_types = 1); namespace App\Controller; -use App\Form\Type\PasteForm; +use App\DTO\PasteFormData; use App\Entity\Paste; +use App\Form\Type\PasteForm; use App\Repository\PasteRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; @@ -17,19 +18,29 @@ class PasteController extends AbstractController #[Route('/')] public function new(Request $request, PasteRepository $pasteRepository): Response { - $paste = new Paste(); - $form = $this->createForm(PasteForm::class, $paste); + $pasteData = new PasteFormData(); + $form = $this->createForm(PasteForm::class, $pasteData); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $paste = $form->getData(); - $paste->setIp($request->getClientIp()); - $paste->setPublishDate(new \DateTime()); + $pasteData = $form->getData(); - if ($paste->isPrivate()) { - $paste->setSecret(hash('sha1', random_bytes(25))); + $secret = null; + if ($pasteData->isPrivate()) { + $secret = hash('sha1', random_bytes(25)); } + $paste = new Paste( + $pasteData->getText(), + $pasteData->getLanguage(), + $pasteData->getDescription(), + $pasteData->getFilename(), + $pasteData->getAuthor(), + new \DateTimeImmutable(), + $pasteData->getExpirationDate(), + $request->getClientIp(), + $secret + ); $pasteRepository->save($paste); return $this->redirectToRoute($request->attributes->get('_route')); diff --git a/src/DTO/PasteFormData.php b/src/DTO/PasteFormData.php new file mode 100644 index 0000000..1eed046 --- /dev/null +++ b/src/DTO/PasteFormData.php @@ -0,0 +1,122 @@ +id; + } + + public function setId(int $id): void + { + $this->id = $id; + } + + public function getText(): string + { + return $this->text; + } + + public function setText(string $text): void + { + $this->text = $text; + } + + public function getLanguage(): ?string + { + return $this->language; + } + + public function setLanguage(?string $language): void + { + $this->language = $language; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(?string $description): void + { + $this->description = $description; + } + + public function getFilename(): ?string + { + return $this->filename; + } + + public function setFilename(?string $filename): void + { + $this->filename = $filename; + } + + public function getAuthor(): string + { + return $this->author; + } + + public function setAuthor(string $author): void + { + $this->author = $author; + } + + public function getExpirationDate(): ?\DateTime + { + return $this->expirationDate; + } + + public function setExpirationDate(?\DateTime $date): void + { + $this->expirationDate = $date; + } + + public function getSecret(): ?string + { + return $this->secret; + } + + public function setSecret(?string $secret): void + { + $this->secret = $secret; + } + + public function isPrivate(): bool + { + return $this->private; + } + + public function setPrivate(bool $private): void + { + $this->private = $private; + } +} \ No newline at end of file diff --git a/src/Entity/Paste.php b/src/Entity/Paste.php index d4c9c59..54c36dc 100644 --- a/src/Entity/Paste.php +++ b/src/Entity/Paste.php @@ -6,158 +6,33 @@ namespace App\Entity; use App\Repository\PasteRepository; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; -use Symfony\Component\Validator\Constraints as Assert; #[ORM\Entity(repositoryClass: PasteRepository::class)] class Paste { #[ORM\Id] #[ORM\GeneratedValue] - #[ORM\Column(nullable: false)] - private int $id; + #[ORM\Column] + public readonly int $id; - #[ORM\Column(type: 'text', nullable: false)] - #[Assert\NotBlank] - private string $text; - - #[ORM\Column(type: 'boolean', nullable: false)] - #[Assert\Type(\boolean::class)] - private bool $private; - - #[ORM\Column(length: 25, nullable: true)] - private ?string $language; - - #[ORM\Column(type: 'text', nullable: true)] - private ?string $description; - - #[ORM\Column(length: 128, nullable: true)] - private ?string $filename; - - #[ORM\Column(length: 128, nullable: false)] - #[Assert\NotBlank] - private string $author = 'anonymous'; - - #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: false)] - #[Assert\Type(\DateTime::class)] - private \DateTime $publishDate; - - #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)] - #[Assert\Type(\DateTime::class)] - private ?\DateTime $expirationDate; - - #[ORM\Column(length: 15, nullable: false)] - private string $ip; - - #[ORM\Column(length: 40, nullable: true)] - private ?string $secret; - - public function getId(): int - { - return $this->id; - } - - public function setId(int $id): void - { - $this->id = $id; - } - - public function getText(): string - { - return $this->text; - } - - public function setText(string $text): void - { - $this->text = $text; - } - - public function getLanguage(): ?string - { - return $this->language; - } - - public function setLanguage(?string $language): void - { - $this->language = $language; - } - - public function getDescription(): ?string - { - return $this->description; - } - - public function setDescription(?string $description): void - { - $this->description = $description; - } - - public function getFilename(): ?string - { - return $this->filename; - } - - public function setFilename(?string $filename): void - { - $this->filename = $filename; - } - - public function getAuthor(): string - { - return $this->author; - } - - public function setAuthor(string $author): void - { - $this->author = $author; - } - - public function getPublishDate(): \DateTime - { - return $this->publishDate; - } - - public function setPublishDate(\DateTime $date): void - { - $this->publishDate = $date; - } - - public function getExpirationDate(): ?\DateTime - { - return $this->expirationDate; - } - - public function setExpirationDate(?\DateTime $date): void - { - $this->expirationDate = $date; - } - - public function getIp(): string - { - return $this->ip; - } - - public function setIP(string $ip): void - { - $this->ip = $ip; - } - - public function getSecret(): ?string - { - return $this->secret; - } - - public function setSecret(?string $secret): void - { - $this->secret = $secret; - } - - public function isPrivate(): bool - { - return $this->private; - } - - public function setPrivate(bool $private): void - { - $this->private = $private; - } + public function __construct( + #[ORM\Column(type: 'text', nullable: false)] + public readonly string $text, + #[ORM\Column(length: 25, nullable: true)] + public readonly ?string $language, + #[ORM\Column(type: 'text', nullable: true)] + public readonly ?string $description, + #[ORM\Column(length: 128, nullable: true)] + public readonly ?string $filename, + #[ORM\Column(length: 128, nullable: false)] + public readonly string $author, + #[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: false)] + public readonly \DateTimeImmutable $publishDate, + #[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)] + public readonly ?\DateTimeImmutable $expirationDate, + #[ORM\Column(length: 15, nullable: false)] + public readonly string $ip, + #[ORM\Column(length: 40, nullable: true)] + public readonly ?string $secret, + ) {} } \ No newline at end of file