<?php
namespace App\Repository;
use App\Domain\ValueObject\CurrencyPair;
use App\Entity\Currency;
use App\Entity\Exchangerate;
use App\Entity\Wholesaler;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* @method Wholesaler|null find($id, $lockMode = null, $lockVersion = null)
* @method Wholesaler|null findOneBy(array $criteria, array $orderBy = null)
* @method Wholesaler[] findAll()
* @method Wholesaler[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ExchangeRateRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Exchangerate::class);
}
public function MyExchangeRateQuery($user)
{
$qb = $this->createQueryBuilder('e');
if ($user->getWholesaler()) {
$qb->where('e.wholesaler = :parameter');
$qb->setParameter('parameter', $user->getWholesaler()->getId());
} else {
return new AccessDeniedHttpException();
}
return $qb;
}
//TO-DO REFACTORIZAR NOMBRE
public function findHistoryChanges(Currency $fromCurrency, Currency $toCurrency, $user)
{
return $this->MyExchangeRateQuery($user)
->andWhere('e.entrycurrency = :entrycurrency')
->andWhere('e.localcurrency = :localcurrency')
->setParameter('entrycurrency', $fromCurrency)
->setParameter('localcurrency', $toCurrency)
->orderBy('e.active', 'DESC')
->orderBy('e.updatedAt', 'DESC')
->getQuery()
->getResult();
}
//TO-DO REFACTORIZAR Y CAMBIAR CURRENCY PAIR Y FIND ACTIVE BY PAIR
public function findActives(Exchangerate $exchangeRate, $user)
{
return $this->MyExchangeRateQuery($user)
->andWhere('e.entrycurrency = :entrycurrency')
->andWhere('e.localcurrency = :localcurrency')
->andWhere('e.active = true')
->setParameter('entrycurrency', $exchangeRate->getEntryCurrency())
->setParameter('localcurrency', $exchangeRate->getLocalCurrency())
->getQuery()
->getResult();
}
//TO-DO REFACTORIZAR NOMBRE
public function findActiveRateByWholesaler(Wholesaler $wholesaler, CurrencyPair $currencyPair)
{
return $this->createQueryBuilder('e')
->andWhere('e.wholesaler = :wholesaler')
->andWhere('e.entrycurrency = :entrycurrency')
->andWhere('e.localcurrency = :localcurrency')
->andWhere('e.active = true')
->setParameter('entrycurrency', $currencyPair->from())
->setParameter('localcurrency', $currencyPair->to())
->setParameter('wholesaler', $wholesaler)
->getQuery()
->setMaxResults(1)
->getOneOrNullResult();
}
}