<?php


namespace App\Model\Services;


use App\Model\Entities\GameData\Studio;
use App\Model\Entities\Security\User;
use Kdyby\Doctrine\EntityRepository;
use Kdyby\Doctrine\QueryBuilder;

class StudioService extends BaseService
{
    /** @var EntityRepository */
    protected $studios;

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

        $this->studios = $this->getRepository(Studio::class);
    }

    /**
     * @param User $createdBy
     * @param $name
     * @return Studio
     */
    public function createStudio(User $createdBy, $name) : Studio
    {
        $studio = new Studio($createdBy);
        $studio->setName($name);

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

    /**
     * @param int $id
     * @return null|Studio
     */
    public function findStudioById(int $id)
    {
        return $this->studios->find($id);
    }

    /**
     * @return Studio[]
     */
    public function getStudios() : array
    {
        return $this->studios->findAll();
    }

    /**
     * @return array
     */
    public function getSelectBoxPairs() : array
    {
        return $this->studios->findPairs("name", ["name" => "ASC"]);
    }

    /**
     * @return QueryBuilder
     */
    public function getStudiosDataSource() : QueryBuilder
    {
        return $this->studios->createQueryBuilder('studio');
    }

}