<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\GameData\Mmorpg\Race;
use App\Model\Services\RaceService;
use Kdyby\Doctrine\QueryBuilder;

class RaceReader extends BaseReader
{
    /** @var RaceService */
    protected $raceService;
    /** @var GameReader */
    protected $gameReader;

    public function __construct(RaceService $raceService, GameReader $gameReader)
    {
        $this->raceService = $raceService;
        $this->gameReader = $gameReader;

    }

    /**
     * @param int $id
     * @return Race
     */
    public function findRaceById(int $id) : Race
    {
        return $this->raceService->findRaceById($id);
    }

    /**
     * @param $game
     * @return QueryBuilder
     */
    public function getGridDataSource($game) : QueryBuilder
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->raceService->getRacesGridDataSource($gameEntity);
    }

    public function validateRace($race) : Race
    {
        if ($race instanceof Race) {
            return $race;
        }

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

}