<?php


namespace App\Model\Services;


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

class PlatformService extends BaseService
{
    /** @var  EntityRepository */
    protected $platforms;

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

        $this->platforms = $this->getRepository(Platform::class);
    }

    /**
     * @param User $createdBy
     * @param $name
     * @param DateTime $releasedOn
     * @return Platform
     */
    public function createPlatform(User $createdBy, $name, DateTime $releasedOn) : Platform
    {
        $platform = new Platform($createdBy);
        $platform->setName($name);
        $platform->setReleasedOn($releasedOn);

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

    /**
     * @param int $id
     * @return null|Platform
     */
    public function findPlatformById(int $id)
    {
        return $this->platforms->find($id);
    }

    /**
     * @return Platform[]
     */
    public function getPlatforms() : array
    {
        return $this->platforms->findAll();
    }

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