Compare commits
4 commits
6640112c54
...
8bceb97550
Author | SHA1 | Date | |
---|---|---|---|
|
8bceb97550 | ||
|
ddfbba344a | ||
|
c164ed446b | ||
|
f2072b9b10 |
migrations
src
|
@ -10,11 +10,6 @@ use Doctrine\Migrations\AbstractMigration;
|
|||
|
||||
final class Version20230720115905 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('CREATE SEQUENCE paste_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
|
||||
|
|
|
@ -24,24 +24,10 @@ class PasteController extends AbstractController
|
|||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$pasteData = $form->getData();
|
||||
$pasteData->ip = $request->getClientIp();
|
||||
|
||||
$secret = null;
|
||||
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);
|
||||
$paste = Paste::fromFormData($pasteData);
|
||||
$pasteRepository->save($paste, true);
|
||||
|
||||
return $this->redirectToRoute($request->attributes->get('_route'));
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ class PasteFormData
|
|||
public string $author = 'anonymous';
|
||||
#[Assert\Type(\DateTimeImmutable::class)]
|
||||
public ?\DateTimeImmutable $expirationDate;
|
||||
public string $ip;
|
||||
|
||||
public function __construct(?Paste $paste=null)
|
||||
{
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
declare(strict_types = 1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\DTO\PasteFormData;
|
||||
use App\Repository\PasteRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: PasteRepository::class)]
|
||||
|
@ -15,24 +14,39 @@ class Paste
|
|||
#[ORM\Column]
|
||||
public readonly int $id;
|
||||
|
||||
public function __construct(
|
||||
private function __construct(
|
||||
#[ORM\Column(type: 'text', nullable: false)]
|
||||
public readonly string $text,
|
||||
#[ORM\Column(length: 25, nullable: true)]
|
||||
#[ORM\Column(type: 'string', length: 25, nullable: true)]
|
||||
public readonly ?string $language,
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
public readonly ?string $description,
|
||||
#[ORM\Column(length: 128, nullable: true)]
|
||||
#[ORM\Column(type: 'string', length: 128, nullable: true)]
|
||||
public readonly ?string $filename,
|
||||
#[ORM\Column(length: 128, nullable: false)]
|
||||
#[ORM\Column(type: 'string', length: 128, nullable: false)]
|
||||
public readonly string $author,
|
||||
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: false)]
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: false)]
|
||||
public readonly \DateTimeImmutable $publishDate,
|
||||
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
||||
public readonly ?\DateTimeImmutable $expirationDate,
|
||||
#[ORM\Column(length: 15, nullable: false)]
|
||||
#[ORM\Column(type: 'string', length: 15, nullable: false)]
|
||||
public readonly string $ip,
|
||||
#[ORM\Column(length: 40, nullable: true)]
|
||||
#[ORM\Column(type: 'string', length: 40, nullable: true)]
|
||||
public readonly ?string $secret,
|
||||
) {}
|
||||
}
|
||||
|
||||
public static function fromFormData(PasteFormData $pasteFormData): Paste
|
||||
{
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,10 +14,13 @@ class PasteRepository extends ServiceEntityRepository
|
|||
parent::__construct($registry, Paste::class);
|
||||
}
|
||||
|
||||
public function save(Paste $paste): void
|
||||
public function save(Paste $paste, bool $flush=false): void
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->persist($paste);
|
||||
$entityManager->flush();
|
||||
|
||||
if ($flush) {
|
||||
$entityManager->flush();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue