<?php
namespace App\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\StaticReportRepository")
* @ORM\Table(name="`static_report`")
*/
class StaticReport
{
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\Column(type="string", nullable=true)
*/
private ?string $name = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="staticReports")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private ?User $user;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Site", inversedBy="reports", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="site_id", referencedColumnName="id")
*/
private ?Site $site;
/**
* @ORM\Column(type="bigint")
*/
private $pageViews = 0;
/**
* @ORM\OneToMany(targetEntity="App\Entity\StaticReportStatistic", mappedBy="staticReport", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private Collection $statistics;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected ?\DateTime $latestReportCreatedAt = null;
/**
* @ORM\Column(type="string", nullable=true)
*/
private ?string $hash = null;
public function __construct()
{
$this->hash = uniqid('', true);
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): void
{
$this->user = $user;
}
public function getSite(): ?Site
{
return $this->site;
}
public function setSite(?Site $site): void
{
$this->site = $site;
}
public function getPageViews(): int
{
return $this->pageViews;
}
public function setPageViews(int $pageViews): void
{
$this->pageViews = $pageViews;
}
public function getStatistics(): Collection
{
return $this->statistics;
}
public function setStatistics(Collection $statistics): void
{
$this->statistics = $statistics;
}
public function getLatestReportCreatedAt(): ?\DateTime
{
return $this->latestReportCreatedAt;
}
public function setLatestReportCreatedAt(?\DateTime $latestReportCreatedAt): void
{
$this->latestReportCreatedAt = $latestReportCreatedAt;
}
public function isReady(): bool
{
return null !== $this->getLatestReportCreatedAt();
}
public function isConfigured(): bool
{
return $this->getSite()
&& false === $this->getSite()->getPages()->isEmpty()
&& false === $this->getSite()->getNations()->isEmpty();
}
public function getDomain(): string
{
return strtolower($this->getSite()->getDomain());
}
public function getDisplayName(): string
{
return $this->getDomain();
}
public function getHash(): ?string
{
return $this->hash;
}
public function setHash(?string $hash): void
{
$this->hash = $hash;
}
public function isValidForYear($year): bool
{
$created = $this->getCreatedAt();
$dateStart = clone $created;
$dateStart->modify('LAST YEAR');
return $year === $dateStart->format('Y');
}
public function getDisplayPeriod(): string
{
$created = $this->getCreatedAt();
$dateStart = clone $created;
$dateStart->modify('LAST YEAR');
$dateStart = $dateStart->modify('1 JANUARY')->setTime(0, 0);
$dateEnd = clone $dateStart;
$dateEnd = $dateEnd->modify('31 DECEMBER')->setTime(0, 0);
return sprintf('%s - %s', $dateStart->format('d M Y'), $dateEnd->format('d M Y'));
}
public function getTotalTCO2(): float
{
$tCO2 = 0.00;
/** @var StaticReportStatistic $statistic */
foreach ($this->statistics as $statistic) {
$tCO2 += $statistic->getTCO2();
}
return $tCO2;
}
public function getTotalKWh(): float
{
$kWh = 0.00;
/** @var StaticReportStatistic $statistic */
foreach ($this->statistics as $statistic) {
$kWh += $statistic->getKWh();
}
return $kWh;
}
public function __toString(): string
{
return $this->id ?? 'New Report';
}
}