WIP: feature_paste #1
|
@ -24,23 +24,9 @@ class PasteController extends AbstractController
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
$pasteData = $form->getData();
|
$pasteData = $form->getData();
|
||||||
skobkin marked this conversation as resolved
Outdated
|
|||||||
|
$pasteData->ip = $request->getClientIp();
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Do you need IP in Do you need IP in `PasteData`?
|
|||||||
|
|
||||||
$secret = null;
|
$paste = Paste::fromFormData($pasteData);
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
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
skobkin
commented
This still could be done in the entity itself. This still could be done in the entity itself.
|
|||||||
if ($pasteData->private) {
|
|
||||||
$secret = hash('sha1', random_bytes(25));
|
|
||||||
}
|
|
||||||
|
|
||||||
$paste = new Paste(
|
|
||||||
$pasteData->text,
|
|
||||||
$pasteData->language,
|
|
||||||
$pasteData->description,
|
|
||||||
$pasteData->filename,
|
|
||||||
$pasteData->author,
|
|
||||||
new \DateTimeImmutable(),
|
|
||||||
$pasteData->expirationDate,
|
|
||||||
$request->getClientIp(),
|
|
||||||
$secret
|
|
||||||
);
|
|
||||||
$pasteRepository->save($paste);
|
$pasteRepository->save($paste);
|
||||||
|
|
||||||
return $this->redirectToRoute($request->attributes->get('_route'));
|
return $this->redirectToRoute($request->attributes->get('_route'));
|
||||||
|
|
|
@ -20,6 +20,7 @@ class PasteFormData
|
||||||
public string $author = 'anonymous';
|
public string $author = 'anonymous';
|
||||||
skobkin marked this conversation as resolved
skobkin
commented
Why Why `string` and `Assert\NotBlank`?
|
|||||||
#[Assert\Type(\DateTimeImmutable::class)]
|
#[Assert\Type(\DateTimeImmutable::class)]
|
||||||
public ?\DateTimeImmutable $expirationDate;
|
public ?\DateTimeImmutable $expirationDate;
|
||||||
skobkin marked this conversation as resolved
skobkin
commented
Is this validation being processed BEFORE storing the data in the DTO? Is this validation being processed BEFORE storing the data in the DTO?
If not, it's pointless as with `bool` field earlier.
|
|||||||
|
public string $ip;
|
||||||
|
|
||||||
skobkin marked this conversation as resolved
skobkin
commented
Or you can make constructor Or you can make constructor `private`, use property promotion and add `fromPaste()` method to create it from the entity.
|
|||||||
public function __construct(?Paste $paste=null)
|
public function __construct(?Paste $paste=null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
declare(strict_types = 1);
|
declare(strict_types = 1);
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Please do not forget Please do not forget `declare(strict_types=1);`
|
|||||||
|
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
use App\DTO\PasteFormData;
|
||||||
use App\Repository\PasteRepository;
|
use App\Repository\PasteRepository;
|
||||||
use Doctrine\DBAL\Types\Types;
|
use Doctrine\DBAL\Types\Types;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
@ -15,7 +15,7 @@ class Paste
|
||||||
#[ORM\Column]
|
#[ORM\Column]
|
||||||
skobkin
commented
Why not also constructor promotion though? Why not also constructor promotion though?
|
|||||||
public readonly int $id;
|
public readonly int $id;
|
||||||
|
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
No need for double quotes here too. No need for double quotes here too.
|
|||||||
public function __construct(
|
private function __construct(
|
||||||
#[ORM\Column(type: 'text', nullable: false)]
|
#[ORM\Column(type: 'text', nullable: false)]
|
||||||
public readonly string $text,
|
public readonly string $text,
|
||||||
#[ORM\Column(length: 25, nullable: true)]
|
#[ORM\Column(length: 25, nullable: true)]
|
||||||
|
@ -35,4 +35,19 @@ class Paste
|
||||||
#[ORM\Column(length: 40, nullable: true)]
|
#[ORM\Column(length: 40, nullable: true)]
|
||||||
public readonly ?string $secret,
|
public readonly ?string $secret,
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Why double quotes? Why double quotes?
|
|||||||
) {}
|
) {}
|
||||||
}
|
|
||||||
skobkin
commented
You can use You can use `self` as return type hint too.
|
|||||||
|
public static function fromFormData(PasteFormData $pasteFormData): Paste
|
||||||
|
{
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Why mutable date? Do you plan to change publish date? Why mutable date? Do you plan to change publish date?
|
|||||||
|
return new self(
|
||||||
|
$pasteFormData->text,
|
||||||
|
$pasteFormData->language,
|
||||||
|
$pasteFormData->description,
|
||||||
|
$pasteFormData->filename,
|
||||||
|
$pasteFormData->author,
|
||||||
|
new \DateTimeImmutable(),
|
||||||
|
$pasteFormData->expirationDate,
|
||||||
|
$pasteFormData->ip,
|
||||||
|
$pasteFormData->private ? \hash('sha1', \random_bytes(25)) : null,
|
||||||
|
);
|
||||||
|
}
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Same code style problem like above. You're leaving It's ok if you're doing multi-line arguments, but not in this case. Same code style problem like above. You're leaving `{` on the same line with return type.
It's ok if you're doing multi-line arguments, but not in this case.
|
|||||||
|
}
|
||||||
|
|
You can just set it in the constructor.