<?php


namespace App\Model\Services;


use App\Model\Entities\Dkp;
use App\Model\Entities\Security\User;
use App\Model\Interfaces\ISite;
use Kdyby\Doctrine\EntityRepository;

class DkpService extends BaseService
{
    /** @var  EntityRepository */
    protected $dkp;

    protected function startup()
    {
        $this->dkp = $this->getRepository(Dkp::class);
    }

    public function grantDkp(ISite $guild, User $grantedBy, User $grantedTo, string $reason, int $amount) : Dkp
    {
        $dkp = new Dkp($grantedBy);
        $dkp->setSite($guild);
        $dkp->setPlayer($grantedTo);
        $dkp->setReason($reason);
        $dkp->setAmount($amount);

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

    public function getUsersDkp(ISite $guild, User $player)
    {
        return $this->dkp->createQueryBuilder('d')
            ->where('d.site = :site')->setParameter('site', $guild)
            ->andWhere('d.player = :player')->setParameter('player', $player)
            ->select('SUM(d.amount)')
            ->getQuery()->getSingleScalarResult();
    }

}