WIP: feature_paste #1
|
@ -1,53 +1,53 @@
|
|||
<?php
|
||||
skobkin marked this conversation as resolved
|
||||
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\Controller;
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Form\Type\PasteForm;
|
||||
use App\Entity\Paste;
|
||||
use App\Repository\PasteRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use App\Form\Type\PasteForm;
|
||||
use App\Entity\Paste;
|
||||
use App\Repository\PasteRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
|
||||
class PasteController extends AbstractController
|
||||
{
|
||||
#[Route('/')]
|
||||
skobkin
commented
I'd suggest explicitly defining which request methods we're processing here. P.S. We'll move this to YAML in the end. I'd suggest explicitly defining which request methods we're processing here.
P.S. We'll move this to YAML in the end.
|
||||
public function new(Request $request, PasteRepository $pasteRepository): Response {
|
||||
$paste = new Paste();
|
||||
$form = $this->createForm(PasteForm::class, $paste);
|
||||
public function new(Request $request, PasteRepository $pasteRepository): Response
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You don't really need Make your repository implement You don't really need `EntityManager` here.
Make your repository implement `save` like I showed you and use it instead.
|
||||
{
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
I'd suggest we refactor this to to DTO usage and make our I'd suggest we refactor this to to DTO usage and make our `Paste` entity immutable.
|
||||
$paste = new Paste();
|
||||
$form = $this->createForm(PasteForm::class, $paste);
|
||||
|
||||
$form->handleRequest($request);
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$paste = $form->getData();
|
||||
$paste->setIp($request->getClientIp());
|
||||
$paste->setPublishDate(new \DateTime());
|
||||
$paste = $form->getData();
|
||||
$paste->setIp($request->getClientIp());
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You can just set it in the constructor. You can just set it in the constructor.
|
||||
$paste->setPublishDate(new \DateTime());
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Do you need IP in Do you need IP in `PasteData`?
|
||||
|
||||
if ($paste->isPrivate()) {
|
||||
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.
|
||||
$paste->setSecret(hash('sha1', random_bytes(25)));
|
||||
$paste->setSecret(hash('sha1', random_bytes(25)));
|
||||
}
|
||||
|
||||
$pasteRepository->save($paste);
|
||||
$pasteRepository->save($paste);
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
I still suggest to make I still suggest to make `Paste::fromFormData()`.
|
||||
|
||||
return $this->redirectToRoute($request->attributes->get('_route'));
|
||||
|
||||
return $this->redirectToRoute($request->attributes->get('_route'));
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Just set a BTW, where are you redirecting exactly? 🤔 You should be redirecting to the route which is processed by 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.
|
||||
}
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
I see you needed some space 🤔 I see you needed some space 🤔
|
||||
|
||||
return $this->render('paste.html.twig', [
|
||||
'form' => $form,
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
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.
|
||||
]);
|
||||
]);
|
||||
skobkin
commented
`show_paste` at least.
|
||||
}
|
||||
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
- 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.
|
||||
#[Route('/{id}/{secret}')]
|
||||
public function show_paste(PasteRepository $pasteRepository, Request $request, string $id, ?string $secret=NULL): Response {
|
||||
public function showPaste(PasteRepository $pasteRepository, Request $request, string $id, ?string $secret=NULL): Response
|
||||
{
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You're violating Symfony code style here which as I can guess you used above. You can just set Symfony style in the settings of your IDE for this project and auto-format this file to fix. Or fix it manually if you want. You're violating [Symfony code style](https://symfony.com/doc/current/contributing/code/standards.html#symfony-coding-standards-in-detail) here which as I can guess you used above.
You can just set Symfony style in the settings of your IDE for this project and auto-format this file to fix. Or fix it manually if you want.
|
||||
$paste = $pasteRepository->findOneBy(['id' => $id, 'secret' => $secret]);
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Do you need double quotes here too? Do you need double quotes here too?
|
||||
$form = $this->createForm(PasteForm::class, $paste);
|
||||
$form = $this->createForm(PasteForm::class, $paste);
|
||||
|
||||
return $this->render('paste.html.twig', [
|
||||
skobkin
commented
I'd suggest you implementing Twig extension wih filter and function to use highlighter in the template. I'd suggest you implementing Twig extension wih filter and function to use highlighter in the template.
|
||||
'form' => $form,
|
||||
]);
|
||||
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
<?php
|
||||
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\Repository\PasteRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use App\Repository\PasteRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
skobkin
commented
I'd suggest explicitly defining Especially since you're defining I'd suggest explicitly defining `Table` attribute here.
Especially since you're defining `Column` attributes below ⬇️
skobkin
commented
I'd also suggest to make this entity readonly. It'll give some performance benefits. You can read about that here. I'd also suggest to make this entity readonly. It'll give some performance benefits.
You can read about that [here](https://www.doctrine-project.org/projects/doctrine-orm/en/2.15/reference/attributes-reference.html#attrref_entity).
|
||||
|
||||
#[ORM\Entity(repositoryClass: PasteRepository::class)]
|
||||
class Paste
|
||||
|
@ -14,128 +14,150 @@ class Paste
|
|||
#[ORM\Id]
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
I don't think you need But I'd suggest to explicitly set it for every other I don't think you need `nullable=false` here since it's a Primary Key.
But I'd suggest to explicitly set it for every other `Column` and also set a `name` for them.
|
||||
#[ORM\GeneratedValue]
|
||||
skobkin
commented
Why not also constructor promotion though? Why not also constructor promotion though?
|
||||
#[ORM\Column(nullable: false)]
|
||||
private int $id;
|
||||
private 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.
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: false)]
|
||||
#[Assert\NotBlank]
|
||||
private string $text;
|
||||
private string $text;
|
||||
|
||||
#[ORM\Column(type: 'boolean', nullable: false)]
|
||||
#[Assert\Type(\boolean::class)]
|
||||
private bool $private;
|
||||
private bool $private;
|
||||
skobkin
commented
What type? Why 25 exactly? What type? Why 25 exactly?
skobkin
commented
Why 128? Why 128?
|
||||
|
||||
#[ORM\Column(length: 25, nullable: true)]
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Why not nullable? Why not nullable?
|
||||
private ?string $language;
|
||||
private ?string $language;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $description;
|
||||
private ?string $description;
|
||||
|
||||
#[ORM\Column(length: 128, nullable: true)]
|
||||
private ?string $filename;
|
||||
private ?string $filename;
|
||||
|
||||
#[ORM\Column(length: 128, nullable: false)]
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Why double quotes? Why double quotes?
|
||||
#[Assert\NotBlank]
|
||||
private string $author = 'anonymous';
|
||||
private string $author = 'anonymous';
|
||||
skobkin
commented
You can use You can use `self` as return type hint too.
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: false)]
|
||||
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?
|
||||
#[Assert\Type(\DateTime::class)]
|
||||
private \DateTime $publishDate;
|
||||
private \DateTime $publishDate;
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
|
||||
#[Assert\Type(\DateTime::class)]
|
||||
private ?\DateTime $expirationDate;
|
||||
private ?\DateTime $expirationDate;
|
||||
|
||||
#[ORM\Column(length: 15, nullable: false)]
|
||||
private string $ip;
|
||||
private string $ip;
|
||||
|
||||
#[ORM\Column(length: 40, nullable: true)]
|
||||
private ?string $secret;
|
||||
private ?string $secret;
|
||||
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.
|
||||
|
||||
public function getId(): int {
|
||||
return $this->id;
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Let this thread be for now, but we'd better make this entity immutable and remove every setter method from here. This way we can use Let this thread be for now, but we'd better make this entity immutable and remove every setter method from here.
This way we can use `private readonly` instead of just `private` properties. Or even `public readonly` 🤔
|
||||
}
|
||||
|
||||
public function setId(int $id): void {
|
||||
$this->id = $id;
|
||||
public function setId(int $id): void
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function getText(): string {
|
||||
return $this->text;
|
||||
public function getText(): string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
public function setText(string $text): void {
|
||||
$this->text = $text;
|
||||
public function setText(string $text): void
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
public function getLanguage(): ?string {
|
||||
return $this->language;
|
||||
public function getLanguage(): ?string
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
public function setLanguage(?string $language): void {
|
||||
$this->language = $language;
|
||||
public function setLanguage(?string $language): void
|
||||
{
|
||||
$this->language = $language;
|
||||
}
|
||||
|
||||
public function getDescription(): ?string {
|
||||
return $this->description;
|
||||
public function getDescription(): ?string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function setDescription(?string $description): void {
|
||||
$this->description = $description;
|
||||
public function setDescription(?string $description): void
|
||||
{
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
public function getFilename(): ?string {
|
||||
return $this->filename;
|
||||
public function getFilename(): ?string
|
||||
{
|
||||
return $this->filename;
|
||||
}
|
||||
|
||||
public function setFilename(?string $filename): void {
|
||||
$this->filename = $filename;
|
||||
public function setFilename(?string $filename): void
|
||||
{
|
||||
$this->filename = $filename;
|
||||
}
|
||||
|
||||
public function getAuthor(): string {
|
||||
return $this->author;
|
||||
public function getAuthor(): string
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You don't need this. You don't need this.
|
||||
{
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
public function setAuthor(string $author): void {
|
||||
$this->author = $author;
|
||||
public function setAuthor(string $author): void
|
||||
{
|
||||
$this->author = $author;
|
||||
}
|
||||
|
||||
public function getPublishDate(): \DateTime {
|
||||
return $this->publishDate;
|
||||
public function getPublishDate(): \DateTime
|
||||
{
|
||||
return $this->publishDate;
|
||||
}
|
||||
|
||||
public function setPublishDate(\DateTime $date): void {
|
||||
$this->publishDate = $date;
|
||||
public function setPublishDate(\DateTime $date): void
|
||||
{
|
||||
$this->publishDate = $date;
|
||||
}
|
||||
|
||||
public function getExpirationDate(): ?\DateTime {
|
||||
return $this->expirationDate;
|
||||
public function getExpirationDate(): ?\DateTime
|
||||
{
|
||||
return $this->expirationDate;
|
||||
}
|
||||
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You don't need this. You don't need this.
|
||||
public function setExpirationDate(?\DateTime $date): void {
|
||||
$this->expirationDate = $date;
|
||||
public function setExpirationDate(?\DateTime $date): void
|
||||
{
|
||||
$this->expirationDate = $date;
|
||||
}
|
||||
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Do you really need this You can just Do you really need this `bool` flag?
You can just `return $this->secret !== null`.
|
||||
public function getIp(): string {
|
||||
return $this->ip;
|
||||
public function getIp(): string
|
||||
{
|
||||
return $this->ip;
|
||||
}
|
||||
|
||||
public function setIP(string $ip): void {
|
||||
$this->ip = $ip;
|
||||
public function setIP(string $ip): void
|
||||
{
|
||||
$this->ip = $ip;
|
||||
}
|
||||
|
||||
public function getSecret(): ?string {
|
||||
return $this->secret;
|
||||
public function getSecret(): ?string
|
||||
{
|
||||
return $this->secret;
|
||||
}
|
||||
|
||||
public function setSecret(?string $secret): void {
|
||||
$this->secret = $secret;
|
||||
public function setSecret(?string $secret): void
|
||||
{
|
||||
$this->secret = $secret;
|
||||
}
|
||||
|
||||
public function isPrivate(): bool {
|
||||
return $this->private;
|
||||
public function isPrivate(): bool
|
||||
{
|
||||
return $this->private;
|
||||
}
|
||||
|
||||
public function setPrivate(bool $private): void {
|
||||
$this->private = $private;
|
||||
public function setPrivate(bool $private): void
|
||||
{
|
||||
$this->private = $private;
|
||||
}
|
||||
}
|
|
@ -1,30 +1,43 @@
|
|||
<?php
|
||||
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\Form\Type;
|
||||
namespace App\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
|
||||
|
||||
class PasteForm extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void {
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Double quotes? Also please multi-line format arrays with more than one element for readability. Double quotes?
Also please multi-line format arrays with more than one element for readability.
|
||||
{
|
||||
$builder
|
||||
->add('language', ChoiceType::class, ['choices' => ['Python' => 'python', 'PHP' => 'php', 'Plain text' => NULL]])
|
||||
->add('language', ChoiceType::class, [
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You can probably extract You can probably extract `128` to the constant and use here and in the validation attribute.
|
||||
'choices' => [
|
||||
'Python' => 'python',
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
This should be a combo-box ( This should be a combo-box (`select` in HTML terms) with a list of available periods like "10 minutes", "1 year" and "Never".
|
||||
'PHP' => 'php',
|
||||
'Plain text' => NULL,
|
||||
]
|
||||
]
|
||||
)
|
||||
->add('description', TextType::class, ['required' => false])
|
||||
->add('text', TextareaType::class)
|
||||
->add('author', TextType::class, ['attr' => ['maxlength' =>128]])
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
`required => false`?
|
||||
->add('filename', TextType::class, ['required' => false, 'attr' => ['maxlength' =>128]])
|
||||
skobkin
commented
Why 128? Why 128?
|
||||
->add('expirationDate', DateTimeType::class, ['required' => false, 'date_widget' => 'single_text', 'input' => 'datetime'])
|
||||
->add('expirationDate', DateTimeType::class, [
|
||||
'required' => false,
|
||||
'date_widget' => 'single_text',
|
||||
'input' => 'datetime',
|
||||
]
|
||||
)
|
||||
->add('private', CheckboxType::class, ['required' => false])
|
||||
->add('save', SubmitType::class)
|
||||
;
|
||||
;
|
||||
}
|
||||
}
|
|
@ -1,21 +1,23 @@
|
|||
<?php
|
||||
skobkin marked this conversation as resolved
skobkin
commented
Can't leave a comment for Do you need it? Can't leave a comment for `src/Repository/.gitignore`, so I'll ask here.
Do you need it?
|
||||
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\Repository;
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Paste;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use App\Entity\Paste;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class PasteRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry) {
|
||||
parent::__construct($registry, Paste::class);
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
I'd suggest adding I'd suggest adding `save()` here like I showed you.
skobkin
commented
Better with optional Better with optional `flush`.
Flushng from the repo itself should be a rare action.
|
||||
parent::__construct($registry, Paste::class);
|
||||
}
|
||||
|
||||
public function save(Paste $paste): void {
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->persist($paste);
|
||||
$entityManager->flush();
|
||||
public function save(Paste $paste): void
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Do not forget to use code formatting. It'd surrounded Do not forget to use code formatting. It'd surrounded `=` with spaces for you.
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->persist($paste);
|
||||
$entityManager->flush();
|
||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You shouldn't do flush here by default. Only in certain cases if needed. You shouldn't do flush here by default. Only in certain cases if needed.
|
||||
}
|
||||
}
|
I can't leave you a comment on
src/Controller/.gitignore
, so I'll ask here.Why do you need it?