<?php


namespace App\Model\Services;


use App\Model\Entities\Game;
use App\Model\Entities\GameData\Mmorpg\Race;
use App\Model\Entities\Security\User;
use Kdyby\Doctrine\EntityRepository;
use Kdyby\Doctrine\QueryBuilder;

class RaceService extends BaseService
{
    /** @var EntityRepository */
    protected $races;

    protected function startup()
    {
        parent::startup();

        $this->races = $this->getRepository(Race::class);
    }

    public function createRace(User $createdBy, Game $game, string $name) : Race
    {
        $race = new Race($createdBy);
        $race->setGame($game);
        $race->setName($name);

        $this->persist($race);
        return $race;
    }

    /**
     * @param int $id
     * @return null|Race
     */
    public function findRaceById(int $id)
    {
        return $this->races->find($id);
    }

    /**
     * @param Game $game
     * @return QueryBuilder
     */
    public function getRacesGridDataSource(Game $game) : QueryBuilder
    {
        return $this->races->createQueryBuilder('race')->where('race.game = :game')->setParameter('game', $game);
    }

    public function getRacesNameAssoc(Game $game) : array
    {
        return $this->races->findAssoc(array('game' => $game), 'name');
    }
}