src/Entity/ReportPageStatistic.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Service\StatisticsDataAggregation;
  4. use App\Service\WorldMap;
  5. use App\Twig\FlagExtension;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Timestampable\Traits\TimestampableEntity;
  11. /**
  12.  * @ORM\Entity(repositoryClass="App\Repository\ReportPageStatisticRepository")
  13.  * @ORM\Table(name="`report_page_statistic`")
  14.  */
  15. class ReportPageStatistic
  16. {
  17.     use TimestampableEntity;
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue
  21.      * @ORM\Column(type="integer")
  22.      */
  23.     private ?int $id null;
  24.     /**
  25.      * @ORM\ManyToOne(targetEntity="App\Entity\Report", inversedBy="pageStatistics")
  26.      * @ORM\JoinColumn(name="report_id", referencedColumnName="id")
  27.      */
  28.     private Report $report;
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity="App\Entity\ReportSnapshot", inversedBy="pageStatistics")
  31.      * @ORM\JoinColumn(name="report_snapshot_id", referencedColumnName="id")
  32.      */
  33.     private ?ReportSnapshot $snapshot null;
  34.     /**
  35.      * @ORM\Column(type="text")
  36.      */
  37.     private ?string $url;
  38.     /**
  39.      * @ORM\Column(type="float")
  40.      */
  41.     private ?float $kWh;
  42.     /**
  43.      * @ORM\Column(type="float")
  44.      */
  45.     private ?float $tCO2;
  46.     /**
  47.      * @ORM\OneToMany(targetEntity="App\Entity\ReportPageCountryStatistic", mappedBy="pageStatistic", cascade={"persist", "remove"}, orphanRemoval=true)
  48.      */
  49.     private ?Collection $countryStatistics;
  50.     public function __construct()
  51.     {
  52.         $this->countryStatistics = new ArrayCollection();
  53.     }
  54.     public function getId(): ?int
  55.     {
  56.         return $this->id;
  57.     }
  58.     public function getReport(): Report
  59.     {
  60.         return $this->report;
  61.     }
  62.     public function setReport(Report $report): void
  63.     {
  64.         $this->report $report;
  65.     }
  66.     public function getSnapshot(): ?ReportSnapshot
  67.     {
  68.         return $this->snapshot;
  69.     }
  70.     public function setSnapshot(?ReportSnapshot $snapshot): void
  71.     {
  72.         $this->snapshot $snapshot;
  73.     }
  74.     public function getUrl(): ?string
  75.     {
  76.         return $this->url;
  77.     }
  78.     public function setUrl(?string $url): void
  79.     {
  80.         $this->url $url;
  81.     }
  82.     public function getKWh(): ?float
  83.     {
  84.         return $this->kWh;
  85.     }
  86.     public function setKWh(?float $kWh): void
  87.     {
  88.         $this->kWh $kWh;
  89.     }
  90.     public function getTCO2(): ?float
  91.     {
  92.         return $this->tCO2;
  93.     }
  94.     public function setTCO2(?float $tCO2): void
  95.     {
  96.         $this->tCO2 $tCO2;
  97.     }
  98.     public function getCountryStatistics(string $field 'tCO2'string $criteria Criteria::DESC): ?Collection
  99.     {
  100.         $criteria Criteria::create()
  101.             ->orderBy(
  102.                 [
  103.                     $field => $criteria,
  104.                 ]
  105.             );
  106.         return $this->countryStatistics->matching($criteria);
  107.     }
  108.     public function setCountryStatistics(?Collection $countryStatistics): void
  109.     {
  110.         $this->countryStatistics $countryStatistics;
  111.     }
  112.     public function getData(): array
  113.     {
  114.         $totalEmissionsByNations['tCO2'] = [];
  115.         $totalEmissionsByNations['kWh'] = [];
  116.         foreach ($this->getCountryStatistics() as $statistic) {
  117.             $totalEmissionsByNations['tCO2'][FlagExtension::getFlagNameAlpha3($statistic->getCode())] = $statistic->getTCO2();
  118.         }
  119.         foreach ($this->getCountryStatistics('kWh') as $statistic) {
  120.             $totalEmissionsByNations['kWh'][FlagExtension::getFlagNameAlpha3($statistic->getCode())] = $statistic->getkWh();
  121.         }
  122.         $groupedTotalEmissions WorldMap::mergeTotalEmissions(
  123.             $totalEmissionsByNations['tCO2'],
  124.             $totalEmissionsByNations['kWh'],
  125.         );
  126.         return [
  127.             'totals' => $groupedTotalEmissions,
  128.             'tCO2' => StatisticsDataAggregation::compact($totalEmissionsByNations['tCO2']),
  129.             'kWh' => StatisticsDataAggregation::compact($totalEmissionsByNations['kWh']),
  130.         ];
  131.     }
  132. }