<?php


namespace App\Model\Services;


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

class CharacterClassesService extends BaseService
{
    /** @var  EntityRepository */
    protected $classes;

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

        $this->classes = $this->getRepository(CharacterClass::class);
    }

    /**
     * @param User $createdBy
     * @param Game $game
     * @param string $name
     * @return CharacterClass
     */
    public function createClass(User $createdBy, Game $game, string $name) : CharacterClass
    {
        $class = new CharacterClass($createdBy);
        $class->setName($name);
        $class->setGame($game);

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

    /**
     * @param int $id
     * @return null|CharacterClass
     */
    public function findClassById(int $id)
    {
        return $this->classes->find($id);
    }

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