Compare commits

...

4 commits

Author SHA1 Message Date
mitsuha_s 8bceb97550 save method in PasteRepository changed 2023-07-22 17:42:30 +03:00
mitsuha_s ddfbba344a DBAL types added to all Paste class properties 2023-07-22 17:29:24 +03:00
mitsuha_s c164ed446b static method fromFormData added to Paste class 2023-07-22 17:07:06 +03:00
mitsuha_s f2072b9b10 getDescription method removed from migration class 2023-07-22 16:16:04 +03:00
5 changed files with 34 additions and 35 deletions

View file

@ -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');

View file

@ -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'));
}

View file

@ -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)
{

View file

@ -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,
);
}
}

View file

@ -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();
}
}
}