<?php


namespace App\Model\Services;


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

class PublisherService extends BaseService
{
    /** @var EntityRepository */
    protected $publishers;

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

        $this->publishers = $this->getRepository(Publisher::class);
    }

    /**
     * @param User $createdBy
     * @param $name
     * @return Publisher
     */
    public function createPublisher(User $createdBy, $name) : Publisher
    {
        $publisher = new Publisher($createdBy);
        $publisher->setName($name);

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

    /**
     * @param int $id
     * @return null|Publisher
     */
    public function findPublisherById(int $id)
    {
        return $this->publishers->find($id);
    }

    /**
     * @return Publisher[]
     */
    public function getPublishers() :  array
    {
        return $this->publishers->findAll();
    }

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


    /**
     * @return QueryBuilder
     */
    public function getPlatformsGridDataSource() : QueryBuilder
    {
        return $this->publishers->createQueryBuilder('publisher');
    }

}