tournament/src/Entity/Game/Game.php
2024-03-11 15:17:09 +02:00

92 lines
2.3 KiB
PHP

<?php
namespace App\Entity\Game;
use App\Entity\Player;
use App\Repository\Game\GameRepository;
use App\Service\Game\PlayerScore;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
#[ORM\Entity(repositoryClass: GameRepository::class)]
#[ORM\Table(name: 'game')]
class Game implements GameInterface
{
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME, unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
private ?string $id = null;
public function __construct(
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: false)]
private readonly DateTimeImmutable $createdAt = new DateTimeImmutable(),
#[ORM\OneToMany(targetEntity: GameScore::class, mappedBy: 'game', cascade: ['persist'], orphanRemoval: true)]
private Collection $scoreList = new ArrayCollection(),
) {}
public function getId(): ?string
{
return $this->id;
}
/**
* @return DateTimeImmutable
*/
public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}
/**
* @return Collection<int, GameScore>
*/
public function getScoreList(): Collection
{
return $this->scoreList;
}
/**
* @param GameScore $score
* @return Game
*/
public function addScore(GameScore $score): Game
{
if (!$this->scoreList->contains($score)) {
$this->scoreList->add($score);
}
return $this;
}
/**
* @param GameScore $score
* @return Game
*/
public function removeScore(GameScore $score): Game
{
if ($this->scoreList->contains($score)) {
$this->scoreList->removeElement($score);
}
return $this;
}
/**
* @param Player $player
* @return PlayerScore|null
*/
public function getPlayerScore(Player $player): ?GameScore
{
$gameScore = $this->scoreList->filter(
function (GameScore $playerScore) use ($player) {
return $playerScore->getPlayer() === $player;
}
);
return $gameScore->count() > 0 ? $gameScore->first() : null;
}
}