src/Domain/Entity/StatisticLog/MonthlyStatistic.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Domain\Entity\StatisticLog;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\Get;
  5. use ApiPlatform\Metadata\GetCollection;
  6. use App\Domain\Entity\Behavior\Identifiable;
  7. use Gedmo\Timestampable\Traits\Timestampable;
  8. use Gedmo\SoftDeleteable\Traits\SoftDeleteable;
  9. /**
  10.  * Class MonthlyStatistic
  11.  *
  12.  * @package App\Domain\Entity\StatisticLog
  13.  */
  14. #[ApiResource(
  15.     operations: [
  16.         new Get(),
  17.         new GetCollection(),
  18.     ],
  19.     normalizationContext: ['groups' => ['monthly_stats_read']],
  20.     filters: ['monthlyStatistic.search_filter'],
  21. )]
  22. class MonthlyStatistic
  23. {
  24.     use Identifiable;
  25.     use Timestampable;
  26.     use SoftDeleteable;
  27.     private int $year;
  28.     private int $month;
  29.     private int $totalRead 0;
  30.     private int $totalReadList 0;
  31.     private int $totalClick 0;
  32.     private float $averageRead 0.0;
  33.     private float $averageReadList 0.0;
  34.     private float $averageClick 0.0;
  35.     private string $entity;
  36.     private ?int $entityId null;
  37.     public ?string $name;
  38.     public ?bool $isEntityPro false;
  39.     private ?array $data null;
  40.     public function __construct(
  41.         int $year,
  42.         int $month,
  43.         string $entity,
  44.         ?int $entityId null,
  45.         ?string $name null,
  46.         ?bool $isEntityPro null,
  47.         ?array $data null
  48.     ) {
  49.         $this->year $year;
  50.         $this->month $month;
  51.         $this->entity $entity;
  52.         $this->entityId $entityId;
  53.         $this->name $name;
  54.         $this->isEntityPro $isEntityPro;
  55.         $this->data $data;
  56.     }
  57.     public function getYear(): int
  58.     {
  59.         return $this->year;
  60.     }
  61.     public function setYear(int $year): self
  62.     {
  63.         $this->year $year;
  64.         return $this;
  65.     }
  66.     public function getMonth(): int
  67.     {
  68.         return $this->month;
  69.     }
  70.     public function setMonth(int $month): self
  71.     {
  72.         $this->month $month;
  73.         return $this;
  74.     }
  75.     public function getTotalRead(): int
  76.     {
  77.         return $this->totalRead;
  78.     }
  79.     public function setTotalRead(int $totalRead): self
  80.     {
  81.         $this->totalRead $totalRead;
  82.         return $this;
  83.     }
  84.     public function getTotalReadList(): int
  85.     {
  86.         return $this->totalReadList;
  87.     }
  88.     public function setTotalReadList(int $totalReadList): self
  89.     {
  90.         $this->totalReadList $totalReadList;
  91.         return $this;
  92.     }
  93.     public function getTotalClick(): int
  94.     {
  95.         return $this->totalClick;
  96.     }
  97.     public function setTotalClick(int $totalClick): self
  98.     {
  99.         $this->totalClick $totalClick;
  100.         return $this;
  101.     }
  102.     public function getAverageRead(): float
  103.     {
  104.         return $this->averageRead;
  105.     }
  106.     public function setAverageRead(float $averageRead): self
  107.     {
  108.         $this->averageRead $averageRead;
  109.         return $this;
  110.     }
  111.     public function getAverageReadList(): float
  112.     {
  113.         return $this->averageReadList;
  114.     }
  115.     public function setAverageReadList(float $averageReadList): self
  116.     {
  117.         $this->averageReadList $averageReadList;
  118.         return $this;
  119.     }
  120.     public function getAverageClick(): float
  121.     {
  122.         return $this->averageClick;
  123.     }
  124.     public function setAverageClick(float $averageClick): self
  125.     {
  126.         $this->averageClick $averageClick;
  127.         return $this;
  128.     }
  129.     public function getEntity(): string
  130.     {
  131.         return $this->entity;
  132.     }
  133.     public function setEntity(string $entity): self
  134.     {
  135.         $this->entity $entity;
  136.         return $this;
  137.     }
  138.     public function getEntityId(): ?int
  139.     {
  140.         return $this->entityId;
  141.     }
  142.     public function setEntityId(?int $entityId): self
  143.     {
  144.         $this->entityId $entityId;
  145.         return $this;
  146.     }
  147.     public function getName(): string
  148.     {
  149.         return $this->name;
  150.     }
  151.     public function setName(string $name): self
  152.     {
  153.         $this->name $name;
  154.         return $this;
  155.     }
  156.     public function getData(): ?array
  157.     {
  158.         return $this->data;
  159.     }
  160.     public function setData(?array $data): self
  161.     {
  162.         $this->data $data;
  163.         return $this;
  164.     }
  165.     /**
  166.      * Récupère le nombre de jours du mois stocké.
  167.      *
  168.      * @return int Le nombre de jours dans le mois.
  169.      */
  170.     public function getDaysInMonth(): int
  171.     {
  172.         // Vérification que le mois est valide (entre 1 et 12)
  173.         if ($this->month || $this->month 12) {
  174.             throw new \InvalidArgumentException("Le mois doit être compris entre 1 et 12.");
  175.         }
  176.         // Pour les mois avec 31 jours
  177.         if (in_array($this->month, [135781012])) {
  178.             return 31;
  179.         }
  180.         // Pour les mois avec 30 jours
  181.         if (in_array($this->month, [46911])) {
  182.             return 30;
  183.         }
  184.         // Pour février
  185.         if ($this->month == 2) {
  186.             // Vérification de l'année bissextile
  187.             if ($this->isLeapYear()) {
  188.                 return 29;
  189.             } else {
  190.                 return 28;
  191.             }
  192.         }
  193.         // Ce code ne devrait jamais être atteint, mais ajouté par précaution
  194.         throw new \LogicException("Erreur inattendue lors du calcul du nombre de jours du mois.");
  195.     }
  196.     /**
  197.      * Vérifie si l'année stockée est une année bissextile.
  198.      *
  199.      * @return bool True si l'année est bissextile, false sinon.
  200.      */
  201.     private function isLeapYear(): bool
  202.     {
  203.         return ($this->year == && $this->year 100 != 0) || ($this->year 400 == 0);
  204.     }
  205.     public function incrementRead(int $amount 1): self
  206.     {
  207.         $this->totalRead += $amount;
  208.         return $this;
  209.     }
  210.     public function incrementReadList(int $amount 1): self
  211.     {
  212.         $this->totalReadList += $amount;
  213.         return $this;
  214.     }
  215.     public function incrementClick(int $amount 1): self
  216.     {
  217.         $this->totalClick += $amount;
  218.         return $this;
  219.     }
  220.     public function calculateAverages(): void
  221.     {
  222.         $this->averageRead $this->totalRead $this->getDaysInMonth();
  223.         $this->averageReadList $this->totalReadList $this->getDaysInMonth();
  224.         $this->averageClick $this->totalClick $this->getDaysInMonth();
  225.     }
  226.     public function isEntityPro(): ?bool
  227.     {
  228.         return $this->isEntityPro;
  229.     }
  230.     public function setIsEntityPro(?bool $isEntityPro): self
  231.     {
  232.         $this->isEntityPro $isEntityPro;
  233.         return $this;
  234.     }
  235. }