WIP: feature_paste #1

Draft
Miroslavsckaya wants to merge 24 commits from feature_paste into master
2 changed files with 30 additions and 113 deletions
Showing only changes of commit 21d651f377 - Show all commits

View file

@ -26,18 +26,18 @@ class PasteController extends AbstractController
$pasteData = $form->getData(); $pasteData = $form->getData();
skobkin marked this conversation as resolved Outdated

You can just set it in the constructor.

You can just set it in the constructor.
skobkin marked this conversation as resolved Outdated

Do you need IP in PasteData?

Do you need IP in `PasteData`?
$secret = null; $secret = null;
if ($pasteData->isPrivate()) { if ($pasteData->private) {
skobkin marked this conversation as resolved Outdated

Do we need double quotes here?

By the way, we can also do that in the constructor.

Do we need double quotes here? By the way, we can also do that in the constructor.
skobkin marked this conversation as resolved Outdated

This still could be done in the entity itself.

This still could be done in the entity itself.
$secret = hash('sha1', random_bytes(25)); $secret = hash('sha1', random_bytes(25));
} }
$paste = new Paste( $paste = new Paste(
skobkin marked this conversation as resolved Outdated

I still suggest to make Paste::fromFormData().

I still suggest to make `Paste::fromFormData()`.
$pasteData->getText(), $pasteData->text,
$pasteData->getLanguage(), $pasteData->language,
skobkin marked this conversation as resolved Outdated

Just set a name for your route and you won't need such workarounds.

BTW, where are you redirecting exactly? 🤔 You should be redirecting to the route which is processed by PasteController::show_paste() as far as I understand your code.

Just set a `name` for your route and you won't need such workarounds. BTW, where are you redirecting exactly? 🤔 You should be redirecting to the route which is processed by `PasteController::show_paste()` as far as I understand your code.
$pasteData->getDescription(), $pasteData->description,
skobkin marked this conversation as resolved Outdated

I see you needed some space 🤔

I see you needed some space 🤔
$pasteData->getFilename(), $pasteData->filename,
$pasteData->getAuthor(), $pasteData->author,
new \DateTimeImmutable(), new \DateTimeImmutable(),
skobkin marked this conversation as resolved Outdated

I mean you don't need to pass creation date here at all.

I mean you don't need to pass creation date here at all.
$pasteData->getExpirationDate(), $pasteData->expirationDate,
Review

show_paste at least.

`show_paste` at least.
$request->getClientIp(), $request->getClientIp(),
$secret $secret
skobkin marked this conversation as resolved Outdated
  • it's a good idea to add , to the last argument too if you use multi-line formatting
  • if you're doing this in the controller, you can just do something like $pasteData->private ? \hash('sha1', \random_bytes(25)) : null. But I suggest to do that in the static method (named constructor) as I also suggested nearby.
- it's a good idea to add `,` to the last argument too if you use multi-line formatting - if you're doing this in the controller, you can just do something like `$pasteData->private ? \hash('sha1', \random_bytes(25)) : null`. But I suggest to do that in the static method (named constructor) as I also suggested nearby.
); );
@ -52,10 +52,11 @@ class PasteController extends AbstractController
} }
#[Route('/{id}/{secret}')] #[Route('/{id}/{secret}')]
public function showPaste(PasteRepository $pasteRepository, Request $request, string $id, ?string $secret=NULL): Response public function showPaste(PasteRepository $pasteRepository, string $id, ?string $secret=NULL): Response
{ {
$paste = $pasteRepository->findOneBy(['id' => $id, 'secret' => $secret]); $paste = $pasteRepository->findOneBy(['id' => $id, 'secret' => $secret]);
$form = $this->createForm(PasteForm::class, $paste); $pasteData = new PasteFormData($paste);
$form = $this->createForm(PasteForm::class, $pasteData);
return $this->render('paste.html.twig', [ return $this->render('paste.html.twig', [
'form' => $form, 'form' => $form,

View file

@ -3,120 +3,36 @@ declare(strict_types = 1);
namespace App\DTO; namespace App\DTO;
use App\Entity\Paste;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
class PasteFormData class PasteFormData
{ {
private int $id;
#[Assert\NotBlank] #[Assert\NotBlank]
private string $text; public string $text;
#[Assert\Type(\boolean::class)] #[Assert\Type(\boolean::class)]
skobkin marked this conversation as resolved Outdated

Huh?

Huh?
private bool $private; public bool $private;
public ?string $language = null;
private ?string $language = null; public ?string $description = null;
public ?string $filename = null;
private ?string $description = null;
private ?string $filename = null;
#[Assert\NotBlank] #[Assert\NotBlank]
private string $author = 'anonymous'; public string $author = 'anonymous';
skobkin marked this conversation as resolved
Review

Why string and Assert\NotBlank?

Why `string` and `Assert\NotBlank`?
#[Assert\Type(\DateTimeImmutable::class)] #[Assert\Type(\DateTimeImmutable::class)]
private ?\DateTimeImmutable $expirationDate; public ?\DateTimeImmutable $expirationDate;
skobkin marked this conversation as resolved
Review

Is this validation being processed BEFORE storing the data in the DTO?
If not, it's pointless as with bool field earlier.

Is this validation being processed BEFORE storing the data in the DTO? If not, it's pointless as with `bool` field earlier.
private ?string $secret; public function __construct(?Paste $paste=null)
skobkin marked this conversation as resolved
Review

Or you can make constructor private, use property promotion and add fromPaste() method to create it from the entity.

Or you can make constructor `private`, use property promotion and add `fromPaste()` method to create it from the entity.
public function getId(): int
{ {
return $this->id; if ($paste === null)
}
public function setId(int $id): void
{ {
$this->id = $id; return;
} }
$this->text = $paste->text;
public function getText(): string $this->private = $paste->secret !== null;
{ $this->language = $paste->language;
return $this->text; $this->description = $paste->description;
} $this->filename = $paste->filename;
$this->author = $paste->author;
public function setText(string $text): void $this->expirationDate = $paste->expirationDate;
{
$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;
} }
} }