src/Entity/RegistryEntry.php line 46

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types;
  5. use Symfony\Component\Serializer\Annotation\Groups;
  6. use ApiPlatform\Metadata\ApiResource,
  7.     ApiPlatform\Metadata\ApiProperty,
  8.     ApiPlatform\Metadata\Get,
  9.     ApiPlatform\Metadata\GetCollection,
  10.     ApiPlatform\Metadata\ApiFilter,
  11.     ApiPlatform\Doctrine\Orm\Filter\SearchFilter,
  12.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  13. use App\Entity\Trait\IdTrait,
  14.     App\Entity\Trait\UuidTrait,
  15.     App\Entity\Trait\TimestampableTrait,
  16.     App\Repository\RegistryEntryRepository;
  17. use Ramsey\Uuid\UuidInterface,
  18.     Ramsey\Uuid\Uuid;
  19. #[ApiResource(
  20.     description'Registry entries',
  21.     normalizationContext: ['groups' => ['read''read:' self::class]],
  22.     denormalizationContext: ['groups' => ['write']],
  23.     security'is_granted("' self::class . '")',
  24.     order: ['createdAt' => 'desc'],
  25.     operations: [
  26.         new GetCollection(),
  27.         new Get()
  28.     ],
  29.     extraProperties: ['standard_put' => false],
  30. )]
  31. #[ApiFilter(SearchFilter::class, properties: [
  32.     'username' => 'partial',
  33.     'operationName' => 'partial',
  34.     'resourceUuid' => 'exact',
  35.     'requestMethod' => 'exact',
  36.     'ip' => 'partial'
  37. ])]
  38. #[ApiFilter(OrderFilter::class, properties: ['username''ip''createdAt''updatedAt'])]
  39. #[ORM\Entity(repositoryClassRegistryEntryRepository::class)]
  40. class RegistryEntry
  41. {
  42.     use IdTrait,
  43.         UuidTrait,
  44.         TimestampableTrait;
  45.     #[ApiProperty(description'Admin')]
  46.     #[Groups(['read:' self::class])]
  47.     #[ORM\ManyToOne(targetEntityAdmin::class, inversedBy'registryEntries')]
  48.     #[ORM\JoinColumn(onDelete'set null')]
  49.     private Admin $admin;
  50.     #[ApiProperty(description'Username')]
  51.     #[Groups(['read:' self::class])]
  52.     #[ORM\Column(typeTypes::STRINGlength191)]
  53.     private string $username;
  54.     #[ApiProperty(description'Operation name')]
  55.     #[Groups(['read:' self::class])]
  56.     #[ORM\Column(typeTypes::STRINGlength255)]
  57.     private string $operationName;
  58.     #[ApiProperty(description'Resource class')]
  59.     #[ORM\Column(typeTypes::STRINGlength255)]
  60.     private string $resourceClass;
  61.     #[ApiProperty(description'Input class')]
  62.     #[ORM\Column(typeTypes::STRINGlength255nullabletrue)]
  63.     private ?string $inputClass;
  64.     #[ApiProperty(description'Uuid of resource')]
  65.     #[Groups(['read:' self::class])]
  66.     #[ORM\Column(typeTypes::STRINGlength255nullabletrue)]
  67.     private ?string $resourceUuid null;
  68.     #[ApiProperty(description'Request method')]
  69.     #[Groups(['read:' self::class])]
  70.     #[ORM\Column(typeTypes::STRINGlength127)]
  71.     private string $requestMethod;
  72.     #[ApiProperty(description'Request uri')]
  73.     #[Groups(['read:' self::class])]
  74.     #[ORM\Column(typeTypes::STRING)]
  75.     private string $requestUri;
  76.     #[ApiProperty(description'Origin ip')]
  77.     #[Groups(['read:' self::class])]
  78.     #[ORM\Column(typeTypes::STRINGlength255)]
  79.     private string $ip;
  80.     public function __construct(
  81.         Admin $admin,
  82.         string $username,
  83.         string $operationName,
  84.         string $resourceClass,
  85.         mixed $inputData,
  86.         ?string $resourceUuid,
  87.         string $requestMethod,
  88.         string $requestUri,
  89.         string $ip
  90.     ) {
  91.         $this->setUuid();
  92.         $this->admin $admin;
  93.         $this->username $username;
  94.         $this->operationName $operationName;
  95.         $this->resourceClass $resourceClass;
  96.         $this->inputClass $inputData && is_object($inputData) ? get_class($inputData) : null;
  97.         $this->resourceUuid $resourceUuid;
  98.         $this->requestMethod $requestMethod;
  99.         $this->requestUri $requestUri;
  100.         $this->ip $ip;
  101.         $this->createdAt = new \DateTimeImmutable();
  102.         $this->updatedAt = new \DateTimeImmutable();
  103.     }
  104.     public function getAdmin(): ?Admin
  105.     {
  106.         return $this->admin;
  107.     }
  108.     public function getUsername(): string
  109.     {
  110.         return $this->username;
  111.     }
  112.     public function getOperationName(): string
  113.     {
  114.         return $this->operationName;
  115.     }
  116.     public function getResourceFullyQualifiedClass(): string
  117.     {
  118.         return $this->resourceClass;
  119.     }
  120.     #[Groups(['read'])]
  121.     public function getResourceClass(): string
  122.     {
  123.         return short_class_name($this->resourceClass);
  124.     }
  125.     public function getInputClass(): ?string
  126.     {
  127.         return $this->inputClass;
  128.     }
  129.     public function getResourceUuid(): UuidInterface|string|null
  130.     {
  131.         return $this->resourceUuid && Uuid::isValid($this->resourceUuid)
  132.             ? Uuid::fromString($this->resourceUuid)
  133.             : $this->resourceUuid;
  134.     }
  135.     public function getRequestMethod(): string
  136.     {
  137.         return $this->requestMethod;
  138.     }
  139.     public function getRequestUri(): string
  140.     {
  141.         return $this->requestUri;
  142.     }
  143.     public function getIp(): string
  144.     {
  145.         return $this->ip;
  146.     }
  147. }