Réalisation finale
This commit is contained in:
199
src/Entity/Lock.php
Normal file
199
src/Entity/Lock.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\LockRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ORM\Entity(repositoryClass: LockRepository::class)]
|
||||
#[ORM\Table(name: 'lock_entity')]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
class Lock
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 100)]
|
||||
#[Assert\NotBlank(message: 'L\'entité est obligatoire.')]
|
||||
private ?string $entityType = null;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[Assert\Positive(message: 'L\'ID de l\'entité doit être positif.')]
|
||||
private ?int $entityId = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 100)]
|
||||
#[Assert\NotBlank(message: 'L\'utilisateur est obligatoire.')]
|
||||
private ?string $userId = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 100)]
|
||||
#[Assert\NotBlank(message: 'La session est obligatoire.')]
|
||||
private ?string $sessionId = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
|
||||
private ?\DateTimeInterface $lockedAt = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
|
||||
private ?\DateTimeInterface $expiresAt = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 50, nullable: true)]
|
||||
private ?string $userAgent = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 45, nullable: true)]
|
||||
private ?string $ipAddress = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->lockedAt = new \DateTime();
|
||||
$this->expiresAt = new \DateTime('+30 minutes'); // Verrou expire après 30 minutes
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getEntityType(): ?string
|
||||
{
|
||||
return $this->entityType;
|
||||
}
|
||||
|
||||
public function setEntityType(string $entityType): static
|
||||
{
|
||||
$this->entityType = $entityType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEntityId(): ?int
|
||||
{
|
||||
return $this->entityId;
|
||||
}
|
||||
|
||||
public function setEntityId(int $entityId): static
|
||||
{
|
||||
$this->entityId = $entityId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUserId(): ?string
|
||||
{
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
public function setUserId(string $userId): static
|
||||
{
|
||||
$this->userId = $userId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSessionId(): ?string
|
||||
{
|
||||
return $this->sessionId;
|
||||
}
|
||||
|
||||
public function setSessionId(string $sessionId): static
|
||||
{
|
||||
$this->sessionId = $sessionId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLockedAt(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->lockedAt;
|
||||
}
|
||||
|
||||
public function setLockedAt(\DateTimeInterface $lockedAt): static
|
||||
{
|
||||
$this->lockedAt = $lockedAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getExpiresAt(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->expiresAt;
|
||||
}
|
||||
|
||||
public function setExpiresAt(\DateTimeInterface $expiresAt): static
|
||||
{
|
||||
$this->expiresAt = $expiresAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUserAgent(): ?string
|
||||
{
|
||||
return $this->userAgent;
|
||||
}
|
||||
|
||||
public function setUserAgent(?string $userAgent): static
|
||||
{
|
||||
$this->userAgent = $userAgent;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getIpAddress(): ?string
|
||||
{
|
||||
return $this->ipAddress;
|
||||
}
|
||||
|
||||
public function setIpAddress(?string $ipAddress): static
|
||||
{
|
||||
$this->ipAddress = $ipAddress;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si le verrou est encore valide
|
||||
*/
|
||||
public function isValid(): bool
|
||||
{
|
||||
return $this->expiresAt > new \DateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si le verrou appartient à l'utilisateur
|
||||
*/
|
||||
public function belongsToUser(string $userId, string $sessionId): bool
|
||||
{
|
||||
return $this->userId === $userId && $this->sessionId === $sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prolonge le verrou
|
||||
*/
|
||||
public function extend(): static
|
||||
{
|
||||
$this->expiresAt = new \DateTime('+30 minutes');
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère une clé unique pour l'entité
|
||||
*/
|
||||
public function getEntityKey(): string
|
||||
{
|
||||
return $this->entityType . '_' . $this->entityId;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'Verrou: %s #%d par %s',
|
||||
$this->entityType,
|
||||
$this->entityId,
|
||||
$this->userId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user