Compare commits

...

6 commits

Author SHA1 Message Date
mitsuha_s ce545176c0 ipv6 support added 2023-07-23 14:50:14 +03:00
mitsuha_s 1e02e96eba PasteFormData->fromPaste private method added 2023-07-23 14:43:24 +03:00
mitsuha_s ea7ddcde65 set nullable: true for author property 2023-07-23 14:39:22 +03:00
mitsuha_s 6a2f15d1d6 Paste->formFormData method changed to Paste->forFormDataAndIp 2023-07-23 13:51:02 +03:00
mitsuha_s 48172fb9c1 code style changes 2023-07-23 13:33:52 +03:00
mitsuha_s d68ec5a333 validation type removed from PasteFromData 2023-07-23 13:33:06 +03:00
6 changed files with 20 additions and 20 deletions

View file

@ -13,7 +13,7 @@ final class Version20230720115905 extends AbstractMigration
public function up(Schema $schema): void
{
$this->addSql('CREATE SEQUENCE paste_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE paste (id INT NOT NULL, text TEXT NOT NULL, language VARCHAR(25), description TEXT, filename VARCHAR(128), author VARCHAR(128) NOT NULL, publish_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, expiration_date TIMESTAMP(0) WITHOUT TIME ZONE, ip VARCHAR(15) NOT NULL, secret VARCHAR(40), PRIMARY KEY(id))');
$this->addSql('CREATE TABLE paste (id INT NOT NULL, text TEXT NOT NULL, language VARCHAR(25), description TEXT, filename VARCHAR(128), author VARCHAR(128), publish_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, expiration_date TIMESTAMP(0) WITHOUT TIME ZONE, ip VARCHAR(39) NOT NULL, secret VARCHAR(40), PRIMARY KEY(id))');
}
public function down(Schema $schema): void

View file

@ -24,9 +24,8 @@ class PasteController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$pasteData = $form->getData();
$pasteData->ip = $request->getClientIp();
$paste = Paste::fromFormData($pasteData);
$paste = Paste::fromFormDataAndIp($pasteData, $request->getClientIp());
$pasteRepository->save($paste, true);
return $this->redirectToRoute($request->attributes->get('_route'));
@ -38,7 +37,7 @@ class PasteController extends AbstractController
}
#[Route('/{id}/{secret}')]
public function showPaste(PasteRepository $pasteRepository, string $id, ?string $secret=NULL): Response
public function showPaste(PasteRepository $pasteRepository, string $id, ?string $secret = NULL): Response
{
$paste = $pasteRepository->findOneBy(['id' => $id, 'secret' => $secret]);
$pasteData = new PasteFormData($paste);

View file

@ -11,29 +11,30 @@ class PasteFormData
{
#[Assert\NotBlank]
public string $text;
#[Assert\Type(\boolean::class)]
public bool $private;
public ?string $language = null;
public ?string $description = null;
public ?string $filename = null;
#[Assert\NotBlank]
public string $author = 'anonymous';
#[Assert\Type(\DateTimeImmutable::class)]
public ?string $author = null;
public ?\DateTimeImmutable $expirationDate;
public string $ip;
public function __construct(?Paste $paste=null)
public function __construct(?Paste $paste = null)
{
if ($paste === null)
{
if ($paste === null) {
return;
}
$this->fromPaste($paste);
}
private function fromPaste(Paste $paste)
{
$this->text = $paste->text;
$this->private = $paste->secret !== null;
$this->language = $paste->language;
$this->description = $paste->description;
$this->filename = $paste->filename;
$this->author = $paste->author;
$this->author = $paste->author !== null ? $paste->author : 'anonymous';
$this->expirationDate = $paste->expirationDate;
}
}

View file

@ -23,19 +23,19 @@ class Paste
public readonly ?string $description,
#[ORM\Column(type: 'string', length: 128, nullable: true)]
public readonly ?string $filename,
#[ORM\Column(type: 'string', length: 128, nullable: false)]
public readonly string $author,
#[ORM\Column(type: 'string', length: 128, nullable: true)]
public readonly ?string $author,
#[ORM\Column(type: 'datetime_immutable', nullable: false)]
public readonly \DateTimeImmutable $publishDate,
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
public readonly ?\DateTimeImmutable $expirationDate,
#[ORM\Column(type: 'string', length: 15, nullable: false)]
#[ORM\Column(type: 'string', length: 39, nullable: false)]
public readonly string $ip,
#[ORM\Column(type: 'string', length: 40, nullable: true)]
public readonly ?string $secret,
) {}
public static function fromFormData(PasteFormData $pasteFormData): Paste
public static function fromFormDataAndIp(PasteFormData $pasteFormData, $ip): Paste
{
return new self(
$pasteFormData->text,
@ -45,7 +45,7 @@ class Paste
$pasteFormData->author,
new \DateTimeImmutable(),
$pasteFormData->expirationDate,
$pasteFormData->ip,
$ip,
$pasteFormData->private ? \hash('sha1', \random_bytes(25)) : null,
);
}

View file

@ -28,7 +28,7 @@ class PasteForm extends AbstractType
)
->add('description', TextType::class, ['required' => false])
->add('text', TextareaType::class)
->add('author', TextType::class, ['attr' => ['maxlength' =>128]])
->add('author', TextType::class, ['attr' => ['maxlength' => 128], 'required' => false])
->add('filename', TextType::class, ['required' => false, 'attr' => ['maxlength' =>128]])
->add('expirationDate', DateTimeType::class, [
'required' => false,

View file

@ -14,7 +14,7 @@ class PasteRepository extends ServiceEntityRepository
parent::__construct($registry, Paste::class);
}
public function save(Paste $paste, bool $flush=false): void
public function save(Paste $paste, bool $flush = false): void
{
$entityManager = $this->getEntityManager();
$entityManager->persist($paste);