code style changes

This commit is contained in:
mitsuha_s 2023-07-21 18:00:14 +03:00
parent 561b016e38
commit ffcfa29968
4 changed files with 148 additions and 111 deletions

View file

@ -1,53 +1,53 @@
<?php
declare(strict_types = 1);
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('/')]
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
{
$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());
$paste->setPublishDate(new \DateTime());
if ($paste->isPrivate()) {
$paste->setSecret(hash('sha1', random_bytes(25)));
$paste->setSecret(hash('sha1', random_bytes(25)));
}
$pasteRepository->save($paste);
$pasteRepository->save($paste);
return $this->redirectToRoute($request->attributes->get('_route'));
return $this->redirectToRoute($request->attributes->get('_route'));
}
return $this->render('paste.html.twig', [
'form' => $form,
]);
]);
}
#[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
{
$paste = $pasteRepository->findOneBy(['id' => $id, 'secret' => $secret]);
$form = $this->createForm(PasteForm::class, $paste);
$form = $this->createForm(PasteForm::class, $paste);
return $this->render('paste.html.twig', [
'form' => $form,
]);
]);
}
}

View file

@ -1,12 +1,12 @@
<?php
declare(strict_types = 1);
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;
#[ORM\Entity(repositoryClass: PasteRepository::class)]
class Paste
@ -14,128 +14,150 @@ class Paste
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(nullable: false)]
private int $id;
private int $id;
#[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;
#[ORM\Column(length: 25, nullable: true)]
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)]
#[Assert\NotBlank]
private string $author = 'anonymous';
private string $author = 'anonymous';
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: false)]
#[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;
public function getId(): int {
return $this->id;
public function getId(): int
{
return $this->id;
}
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
{
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;
}
public function setExpirationDate(?\DateTime $date): void {
$this->expirationDate = $date;
public function setExpirationDate(?\DateTime $date): void
{
$this->expirationDate = $date;
}
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;
}
}

View file

@ -1,30 +1,43 @@
<?php
declare(strict_types = 1);
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
{
$builder
->add('language', ChoiceType::class, ['choices' => ['Python' => 'python', 'PHP' => 'php', 'Plain text' => NULL]])
->add('language', ChoiceType::class, [
'choices' => [
'Python' => 'python',
'PHP' => 'php',
'Plain text' => NULL,
]
]
)
->add('description', TextType::class, ['required' => false])
->add('text', TextareaType::class)
->add('author', TextType::class, ['attr' => ['maxlength' =>128]])
->add('filename', TextType::class, ['required' => false, 'attr' => ['maxlength' =>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)
;
;
}
}

View file

@ -1,21 +1,23 @@
<?php
declare(strict_types = 1);
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)
{
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
{
$entityManager = $this->getEntityManager();
$entityManager->persist($paste);
$entityManager->flush();
}
}