<?php


namespace App\Model\Services;


use App\Model\Entities\Security\BattleNetAccount;
use App\Model\Entities\Security\User;

class BattleNetService extends BaseService
{
    /**
     * @param $battleNetId
     * @return null|BattleNetAccount
     */
    public function findBattleNetAccountByBattleNetId($battleNetId)
    {
        $criteria = array('battleNetId' => $battleNetId);

        return $this->getRepository(BattleNetAccount::class)->findOneBy($criteria);
    }

    /**
     * @param User $user
     * @param $battleNetId
     * @param $battleTag
     * @param $accessToken
     * @return BattleNetAccount
     */
    public function createBattleNetAccount(User $user, $battleNetId, $battleTag, $accessToken)
    {
        $battleNetAccount = new BattleNetAccount($battleNetId, $battleTag);
        $battleNetAccount->setUser($user);
        $battleNetAccount->setAccessToken($accessToken);

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

    public function getUsersBattleNetAccount(User $userEntity)
    {
        $criteria = array('user' => $userEntity);

        return $this->getRepository(BattleNetAccount::class)->findOneBy($criteria);
    }
}