<?php
namespace App\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Money\Money;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Entity\Repository\GameRepository")
* @ORM\Table(name="`match`")
* @Serializer\ExclusionPolicy("all")
*/
class Game
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
* @Serializer\Expose()
*/
private $id;
/**
* @var DateTime
* @ORM\Column(type="datetime", nullable=true)
* @Serializer\Expose()
*/
#[Assert\NotBlank]
private $date;
/**
* Many Matches have One homeClub.
*
* @var Club|null
*
* @ORM\ManyToOne(targetEntity="Club", inversedBy="homematches")
* @Serializer\Expose()
*/
private $homeClub;
/**
* Many Matches have One awayClub.
*
* @var Club|null
*
* @ORM\ManyToOne(targetEntity="Club")
* @Serializer\Expose()
*/
private $awayClub;
/**
* Many Matches have One matchCountry.
*
* @var Country|null
*
* @ORM\ManyToOne(targetEntity="Country")
*/
private $matchCountry;
/**
* Many Matches have One Competition.
*
* @var Competition|null
*
* @ORM\ManyToOne(targetEntity="Competition", inversedBy="matches")
* @Serializer\Expose()
* @Serializer\Type("App\Entity\Competition")
*/
private $competition;
/**
* Many Matches have One Stadium.
*
* @var Stadium|null
*
* @ORM\ManyToOne(targetEntity="Stadium")
* @Serializer\Expose()
*/
private $stadium;
/**
* @var Money|null
* @ORM\Column(name="cheapest_accommodation", type="money", nullable=true)
* @Serializer\Type("Money\Money")
*/
private $cheapestAccommodation;
/**
* @var Money|null
* @ORM\Column(name="match_ticket_price", type="money", nullable=true)
*/
private $matchTicketPrice;
/**
* @var Money|null
* @ORM\Column(name="match_ticket_price_base", type="money", nullable=true)
*/
private $matchTicketPriceBase;
/**
* One Match have Many GameCategories.
*
* @var GameCategory[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity="GameCategory", mappedBy="match", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $categories;
/**
* @var bool
* @ORM\Column(type="boolean")
* @Serializer\Expose()
*/
private $final = false;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
private $offer = false;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
private $popular = false;
/**
* @var bool
* @ORM\Column(type="boolean")
* @Serializer\Expose()
*/
private $hasFlight = true;
/**
* @var bool
* @ORM\Column(type="boolean")
* @Serializer\Expose()
*/
private $disableBooking = false;
/**
* @var bool
* @ORM\Column(type="boolean")
* @Serializer\Expose()
*/
private $firstOfMonth = false;
/**
* @var DateTime
* @ORM\Column(type="datetime", nullable=true)
* @Serializer\Expose()
*/
private $lastSyncDate;
/**
* @var int
* @ORM\Column(type="integer", nullable=true)
*/
private $syncTime;
/**
* @var int
* @ORM\Column(type="integer", nullable=true)
*/
private $syncStatus;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $syncMessage;
/**
* Match constructor.
*/
public function __construct()
{
$this->categories = new ArrayCollection();
}
/**
* @return int
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return DateTime
*/
public function getDate(): ?DateTime
{
return $this->date;
}
/**
* @param DateTime $date
*/
public function setDate(?DateTime $date): self
{
$this->date = $date;
return $this;
}
public function getHomeClub(): ?Club
{
return $this->homeClub;
}
public function setHomeClub(?Club $homeClub): self
{
$this->homeClub = $homeClub;
return $this;
}
public function getAwayClub(): ?Club
{
return $this->awayClub;
}
public function setAwayClub(?Club $awayClub): self
{
$this->awayClub = $awayClub;
return $this;
}
public function getMatchCountry(): ?Country
{
return $this->matchCountry;
}
public function setMatchCountry(?Country $matchCountry): self
{
$this->matchCountry = $matchCountry;
return $this;
}
public function getCompetition(): ?Competition
{
return $this->competition;
}
public function setCompetition(?Competition $competition): self
{
$this->competition = $competition;
return $this;
}
public function getStadium(): ?Stadium
{
return $this->stadium;
}
public function setStadium(?Stadium $stadium): self
{
$this->stadium = $stadium;
return $this;
}
public function getCheapestAccommodation(): ?Money
{
return $this->cheapestAccommodation;
}
public function calculateCheapestAccommodation(): ?Money
{
$clone = $this->cheapestAccommodation;
if (!$this->isFinal() && null !== $clone) {
return $clone->multiply(2);
}
return $clone;
}
public function setCheapestAccommodation(?Money $cheapestAccommodation): self
{
$this->cheapestAccommodation = $cheapestAccommodation;
return $this;
}
public function getMatchTicketPrice(): ?Money
{
return $this->matchTicketPrice;
}
public function setMatchTicketPrice(?Money $matchTicketPrice): self
{
$this->matchTicketPrice = $matchTicketPrice;
return $this;
}
public function getMatchTicketPriceBase(): ?Money
{
return $this->matchTicketPriceBase;
}
public function setMatchTicketPriceBase(?Money $matchTicketPriceBase): self
{
$this->matchTicketPriceBase = $matchTicketPriceBase;
return $this;
}
/**
* @return GameCategory[]|ArrayCollection
*/
public function getCategories()
{
return $this->categories;
}
/**
* @return $this
*/
public function addCategory(GameCategory $category): self
{
if ($this->categories->contains($category)) {
return $this;
}
$this->categories->add($category);
$category->setMatch($this);
return $this;
}
/**
* @return $this
*/
public function removeCategory(GameCategory $category): self
{
if (!$this->categories->contains($category)) {
return $this;
}
$this->categories->removeElement($category);
return $this;
}
public function removeCategories()
{
$this->categories->clear();
}
public function isFinal(): bool
{
return $this->final;
}
public function setFinal(bool $final): self
{
$this->final = $final;
return $this;
}
public function isOffer(): bool
{
return $this->offer;
}
public function setOffer(bool $offer): self
{
$this->offer = $offer;
return $this;
}
public function isPopular(): bool
{
return $this->popular;
}
public function setPopular(bool $popular): self
{
$this->popular = $popular;
return $this;
}
/**
* @Serializer\VirtualProperty()
*/
public function getPrice(): ?Money
{
$price = $this->matchTicketPrice;
if ($price && $this->cheapestAccommodation) {
if(!$this->isFinal() && !in_array($this->getStadium()->getCountry()->getSlug(),['duitsland', 'frankrijk', 'denemarken'])) {
$priceForExtraDay = ceil($this->getCheapestAccommodation()->multiply(0.7,Money::ROUND_UP)->getAmount()/100)*100;
$price = $price->add(Money::EUR($priceForExtraDay));
}
$price = $price->add($this->getCheapestAccommodation());
}
return $price;
}
public function isHasFlight(): bool
{
return $this->hasFlight;
}
public function setHasFlight(bool $hasFlight): self
{
$this->hasFlight = $hasFlight;
return $this;
}
public function isDisableBooking(): bool
{
return $this->disableBooking;
}
public function setDisableBooking(bool $disableBooking): self
{
$this->disableBooking = $disableBooking;
return $this;
}
public function setFirstOfMonth(bool $firstOfMonth): self
{
$this->firstOfMonth = $firstOfMonth;
return $this;
}
public function isFirstOfMonth(): bool
{
return $this->firstOfMonth;
}
/**
* @return DateTime
*/
public function getLastSyncDate(): ?DateTime
{
return $this->lastSyncDate;
}
/**
* @param DateTime $lastSyncDate
*/
public function setLastSyncDate(?DateTime $lastSyncDate): self
{
$this->lastSyncDate = $lastSyncDate;
return $this;
}
/**
* @return int
*/
public function getSyncTime(): ?int
{
return $this->syncTime;
}
/**
* @param int $syncTime
*/
public function setSyncTime(int $syncTime): self
{
$this->syncTime = $syncTime;
return $this;
}
/**
* @return int
*/
public function getSyncStatus(): ?int
{
return $this->syncStatus;
}
/**
* @param int $syncStatus
*/
public function setSyncStatus(int $syncStatus): self
{
$this->syncStatus = $syncStatus;
return $this;
}
/**
* @return string
*/
public function getSyncMessage(): ?string
{
return $this->syncMessage;
}
/**
* @param string $syncMessage
*/
public function setSyncMessage(string $syncMessage): self
{
$this->syncMessage = $syncMessage;
return $this;
}
public function getMatchFormattedTitle(): string
{
return sprintf(
'%s - %s in %s, %s',
$this->homeClub->getName(),
$this->awayClub->getName(),
$this->stadium->getName(),
$this->date->format('d-m-Y')
);
}
public function __clone()
{
$this->categories = new ArrayCollection();
}
}