<?php
namespace App\Domain\Entity\StatisticLog;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\Domain\Entity\Behavior\Identifiable;
use Gedmo\Timestampable\Traits\Timestampable;
use Gedmo\SoftDeleteable\Traits\SoftDeleteable;
/**
* Class MonthlyStatistic
*
* @package App\Domain\Entity\StatisticLog
*/
#[ApiResource(
operations: [
new Get(),
new GetCollection(),
],
normalizationContext: ['groups' => ['monthly_stats_read']],
filters: ['monthlyStatistic.search_filter'],
)]
class MonthlyStatistic
{
use Identifiable;
use Timestampable;
use SoftDeleteable;
private int $year;
private int $month;
private int $totalRead = 0;
private int $totalReadList = 0;
private int $totalClick = 0;
private float $averageRead = 0.0;
private float $averageReadList = 0.0;
private float $averageClick = 0.0;
private string $entity;
private ?int $entityId = null;
public ?string $name;
public ?bool $isEntityPro = false;
private ?array $data = null;
public function __construct(
int $year,
int $month,
string $entity,
?int $entityId = null,
?string $name = null,
?bool $isEntityPro = null,
?array $data = null
) {
$this->year = $year;
$this->month = $month;
$this->entity = $entity;
$this->entityId = $entityId;
$this->name = $name;
$this->isEntityPro = $isEntityPro;
$this->data = $data;
}
public function getYear(): int
{
return $this->year;
}
public function setYear(int $year): self
{
$this->year = $year;
return $this;
}
public function getMonth(): int
{
return $this->month;
}
public function setMonth(int $month): self
{
$this->month = $month;
return $this;
}
public function getTotalRead(): int
{
return $this->totalRead;
}
public function setTotalRead(int $totalRead): self
{
$this->totalRead = $totalRead;
return $this;
}
public function getTotalReadList(): int
{
return $this->totalReadList;
}
public function setTotalReadList(int $totalReadList): self
{
$this->totalReadList = $totalReadList;
return $this;
}
public function getTotalClick(): int
{
return $this->totalClick;
}
public function setTotalClick(int $totalClick): self
{
$this->totalClick = $totalClick;
return $this;
}
public function getAverageRead(): float
{
return $this->averageRead;
}
public function setAverageRead(float $averageRead): self
{
$this->averageRead = $averageRead;
return $this;
}
public function getAverageReadList(): float
{
return $this->averageReadList;
}
public function setAverageReadList(float $averageReadList): self
{
$this->averageReadList = $averageReadList;
return $this;
}
public function getAverageClick(): float
{
return $this->averageClick;
}
public function setAverageClick(float $averageClick): self
{
$this->averageClick = $averageClick;
return $this;
}
public function getEntity(): string
{
return $this->entity;
}
public function setEntity(string $entity): self
{
$this->entity = $entity;
return $this;
}
public function getEntityId(): ?int
{
return $this->entityId;
}
public function setEntityId(?int $entityId): self
{
$this->entityId = $entityId;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getData(): ?array
{
return $this->data;
}
public function setData(?array $data): self
{
$this->data = $data;
return $this;
}
/**
* Récupère le nombre de jours du mois stocké.
*
* @return int Le nombre de jours dans le mois.
*/
public function getDaysInMonth(): int
{
// Vérification que le mois est valide (entre 1 et 12)
if ($this->month < 1 || $this->month > 12) {
throw new \InvalidArgumentException("Le mois doit être compris entre 1 et 12.");
}
// Pour les mois avec 31 jours
if (in_array($this->month, [1, 3, 5, 7, 8, 10, 12])) {
return 31;
}
// Pour les mois avec 30 jours
if (in_array($this->month, [4, 6, 9, 11])) {
return 30;
}
// Pour février
if ($this->month == 2) {
// Vérification de l'année bissextile
if ($this->isLeapYear()) {
return 29;
} else {
return 28;
}
}
// Ce code ne devrait jamais être atteint, mais ajouté par précaution
throw new \LogicException("Erreur inattendue lors du calcul du nombre de jours du mois.");
}
/**
* Vérifie si l'année stockée est une année bissextile.
*
* @return bool True si l'année est bissextile, false sinon.
*/
private function isLeapYear(): bool
{
return ($this->year % 4 == 0 && $this->year % 100 != 0) || ($this->year % 400 == 0);
}
public function incrementRead(int $amount = 1): self
{
$this->totalRead += $amount;
return $this;
}
public function incrementReadList(int $amount = 1): self
{
$this->totalReadList += $amount;
return $this;
}
public function incrementClick(int $amount = 1): self
{
$this->totalClick += $amount;
return $this;
}
public function calculateAverages(): void
{
$this->averageRead = $this->totalRead / $this->getDaysInMonth();
$this->averageReadList = $this->totalReadList / $this->getDaysInMonth();
$this->averageClick = $this->totalClick / $this->getDaysInMonth();
}
public function isEntityPro(): ?bool
{
return $this->isEntityPro;
}
public function setIsEntityPro(?bool $isEntityPro): self
{
$this->isEntityPro = $isEntityPro;
return $this;
}
}