src/Entity/StatsExport.php line 54

  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\Post,
  11.     ApiPlatform\Metadata\ApiFilter,
  12.     ApiPlatform\Doctrine\Orm\Filter\SearchFilter,
  13.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  14. use App\Entity\Trait\IdTrait,
  15.     App\Entity\Trait\UuidTrait,
  16.     App\Entity\Trait\TimestampableTrait,
  17.     App\Entity\Trait\OwnerTrait,
  18.     App\Entity\Trait\Media\ProtectedMediaTrait,
  19.     App\Entity\Interface\OwnerableInterface,
  20.     App\Repository\StatsExportRepository,
  21.     App\DTO\StatsExportDTO,
  22.     App\StateProcessor\StatsExportProcessor;
  23. use App\Enum\StatsExportState;
  24. use App\Enum\StatsExportType;
  25. #[ApiResource(
  26.     description'Stats exports',
  27.     normalizationContext: ['groups' => ['read''read:' self::class]],
  28.     security'is_granted("ROLE_MODULE_STATS")',
  29.     order: ['createdAt' => 'desc'],
  30.     operations: [
  31.         new GetCollection(),
  32.         new Get(),
  33.         new Post(
  34.             inputStatsExportDTO::class,
  35.             processorStatsExportProcessor::class,
  36.         )
  37.     ],
  38.     extraProperties: ['standard_put' => false],
  39. )]
  40. #[ApiFilter(SearchFilter::class, properties: [
  41.     'uuid' => 'exact',
  42.     'createdBy.fullName' => 'ipartial',
  43.     'updatedAt' => 'ipartial',
  44.     'ip' => 'ipartial'
  45. ])]
  46. #[ApiFilter(OrderFilter::class, properties: ['ip''updatedAt'])]
  47. #[ORM\Entity(repositoryClassStatsExportRepository::class)]
  48. class StatsExport implements OwnerableInterface
  49. {
  50.     use IdTrait,
  51.         UuidTrait,
  52.         TimestampableTrait,
  53.         OwnerTrait,
  54.         ProtectedMediaTrait;
  55.     #[ApiProperty(description'Columns')]
  56.     #[Groups(['read:' self::class])]
  57.     #[ORM\Column(typeTypes::JSON)]
  58.     private array $columns = [];
  59.     #[ApiProperty(description'Type')]
  60.     #[Groups(['read:' self::class])]
  61.     #[ORM\Column(
  62.         typeTypes::STRING,
  63.         enumTypeStatsExportType::class,
  64.         length255,
  65.         options: ['default' => StatsExportType::JOURNAL_ARTICLE]
  66.     )]
  67.     private StatsExportType $type StatsExportType::JOURNAL_ARTICLE;
  68.     #[ApiProperty(description'Origin ip')]
  69.     #[Groups(['read:' self::class])]
  70.     #[ORM\Column(typeTypes::STRINGlength255)]
  71.     private string $ip;
  72.     #[ApiProperty(description'Request')]
  73.     #[Groups(['read:' self::class])]
  74.     #[ORM\Column(typeTypes::STRINGlength2047)]
  75.     private string $request;
  76.     #[ApiProperty(description'State')]
  77.     #[Groups(['read:' self::class])]
  78.     #[ORM\Column(
  79.         typeTypes::STRING,
  80.         enumTypeStatsExportState::class,
  81.         length255,
  82.         options: ['default' => StatsExportState::WAITING]
  83.     )]
  84.     private StatsExportState $state StatsExportState::WAITING;
  85.     public function __construct(
  86.         Admin $createdBy,
  87.         StatsExportType $type,
  88.         array $columns,
  89.         string $request,
  90.         string $ip,
  91.     ) {
  92.         $this->setUuid();
  93.         $this->createdBy $createdBy;
  94.         $this->updatedBy $createdBy;
  95.         $this->type $type;
  96.         $this->columns $columns;
  97.         $this->request $request;
  98.         $this->ip $ip;
  99.         $this->createdAt = new \DateTimeImmutable();
  100.         $this->updatedAt = new \DateTimeImmutable();
  101.     }
  102.     public function getType(): StatsExportType
  103.     {
  104.         return $this->type;
  105.     }
  106.     public function getColumns(): array
  107.     {
  108.         return $this->columns;
  109.     }
  110.     public function getIp(): string
  111.     {
  112.         return $this->ip;
  113.     }
  114.     public function getRequest(): string
  115.     {
  116.         return $this->request;
  117.     }
  118.     public function getRequestPaginated(int $perPage): string
  119.     {
  120.         if (str_contains($this->request'?')) {
  121.             return "{$this->request}&page=1&perPage={$perPage}";
  122.         }
  123.         return "{$this->request}?page=1&perPage={$perPage}";
  124.     }
  125.     public function getState(): StatsExportState
  126.     {
  127.         return $this->state;
  128.     }
  129.     public function start(): self
  130.     {
  131.         $this->state StatsExportState::PENDING;
  132.         return $this;
  133.     }
  134.     public function fail(): self
  135.     {
  136.         $this->state StatsExportState::WAITING;
  137.         return $this;
  138.     }
  139.     public function finish(ProtectedMedia $media): self
  140.     {
  141.         $this->media $media;
  142.         $this->state StatsExportState::READY;
  143.         return $this;
  144.     }
  145. }