<?php
|
|
namespace App\Entity;
|
|
use ApiPlatform\Core\Annotation\ApiProperty;
|
use ApiPlatform\Core\Annotation\ApiResource;
|
use App\Repository\SecretRepository;
|
use Doctrine\ORM\Mapping as ORM;
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
/**
|
* @ApiResource(
|
* collectionOperations={
|
* "create"={
|
* "path"="secret",
|
* "method"="post",
|
* "input"=App\Dto\CreateSecretDTO::class,
|
* * "openapi_context": {
|
* "requestBody": {
|
* "content": {
|
* "application/x-www-form-urlencoded": {
|
* "schema": {
|
* "type": "object",
|
* "properties": {
|
* "secret": {
|
* "type": "string",
|
* "example": "https://i.pinimg.com/originals/e2/b8/e3/e2b8e32a0f9660e8fa0a2d68da618f0f.jpg",
|
* "description": "This text will be saved as a secret"
|
* },
|
* "expireAfterViews": {
|
* "type": "integer",
|
* "example": 10,
|
* "description": "The secret won't be available after the given number of views. It must be greater than 0.",
|
* },
|
* "expireAfter": {
|
* "type": "integer",
|
* "example": "100",
|
* "description": "The secret won't be available after the given time. The value is provided in minutes. 0 means never expires"
|
* },
|
* },
|
* },
|
* },
|
* },
|
* },
|
* },
|
* }
|
* },
|
* itemOperations={
|
* "get"=
|
* {
|
* "path"="secret/{hash}",
|
* "controller"=\App\Controller\RetrieveSecretController::class
|
* },
|
* }
|
* )
|
* @ORM\Entity(repositoryClass=SecretRepository::class)
|
*/
|
class Secret
|
{
|
public function __construct()
|
{
|
$this->setCreatedAt(new \DateTimeImmutable());
|
}
|
|
/**
|
* @ORM\Id
|
* @ORM\Column(type="string", length=255, unique=true)
|
* @ApiProperty(attributes={"openapi_context"={"description" = "Unique hash to identify the secrets"}})
|
* @Assert\NotNull
|
*/
|
private $hash;
|
|
/**
|
* @ORM\Column(type="string", length=255)
|
* @ApiProperty(attributes={"openapi_context"={"description" = "The secret itself"}})
|
* @Assert\NotNull
|
* @Assert\Length(
|
* min = 1,
|
* max = 255
|
* )
|
*/
|
private $secretText;
|
|
/**
|
* @ORM\Column(type="datetime_immutable")
|
* @ApiProperty(attributes={"openapi_context"={"description" = "The date and time of the creation"}})
|
* @Assert\NotNull
|
*/
|
private $createdAt;
|
|
/**
|
* @ORM\Column(type="datetime_immutable", nullable=true)
|
* @ApiProperty(attributes={"openapi_context"={"description" = "The secret cannot be reached after this time"}})
|
*/
|
private $expiresAt;
|
|
/**
|
* @ORM\Column(type="integer")
|
* @ApiProperty(attributes={"openapi_context"={"description" = "How many times the secret can be viewed"}})
|
* @Assert\NotNull
|
* @Assert\Range(
|
* min = 0
|
* )
|
*/
|
private $remainingViews;
|
|
public function getHash(): ?string
|
{
|
return $this->hash;
|
}
|
|
public function getSecretText(): ?string
|
{
|
return $this->secretText;
|
}
|
|
public function setSecretText(string $secretText): self
|
{
|
$this->secretText = $secretText;
|
$this->hash = md5($secretText . uniqid('dont be too salty please', true));
|
return $this;
|
}
|
|
public function getCreatedAt(): ?\DateTimeImmutable
|
{
|
return $this->createdAt;
|
}
|
|
public function setCreatedAt(\DateTimeImmutable $createdAt): self
|
{
|
$this->createdAt = $createdAt;
|
|
return $this;
|
}
|
|
public function getExpiresAt(): ?\DateTimeImmutable
|
{
|
return $this->expiresAt;
|
}
|
|
public function setExpiresAt(?\DateTimeImmutable $expiresAt): self
|
{
|
$this->expiresAt = $expiresAt;
|
|
return $this;
|
}
|
|
public function getRemainingViews(): ?int
|
{
|
return $this->remainingViews;
|
}
|
|
public function setRemainingViews(int $remainingViews): self
|
{
|
$this->remainingViews = $remainingViews;
|
|
return $this;
|
}
|
}
|