Completed
Push — master ( 65e2ae...a777b3 )
by Paweł
23s
created

ExchangeRateRepository::findOneWithCurrencyPair()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CurrencyBundle\Doctrine\ORM;
13
14
use Doctrine\ORM\NonUniqueResultException;
15
use Doctrine\ORM\Query\Expr\Orx;
16
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
17
use Sylius\Component\Currency\Model\CurrencyInterface;
18
use Sylius\Component\Currency\Model\ExchangeRateInterface;
19
use Sylius\Component\Currency\Repository\ExchangeRateRepositoryInterface;
20
21
/**
22
 * @author Jan Góralski <[email protected]>
23
 */
24
class ExchangeRateRepository extends EntityRepository implements ExchangeRateRepositoryInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @throws NonUniqueResultException
30
     */
31
    public function findOneWithCurrencyPair($firstCurrencyCode, $secondCurrencyCode)
32
    {
33
        $expr = $this->getEntityManager()->getExpressionBuilder();
34
35
        return $this->createQueryBuilder('o')
36
            ->addSelect('sourceCurrency')
37
            ->addSelect('targetCurrency')
38
            ->innerJoin('o.sourceCurrency', 'sourceCurrency')
39
            ->innerJoin('o.targetCurrency', 'targetCurrency')
40
            ->andWhere($expr->orX(
41
                'sourceCurrency.code = :firstCurrency AND targetCurrency.code = :secondCurrency',
42
                'targetCurrency.code = :firstCurrency AND sourceCurrency.code = :secondCurrency'
43
            ))
44
            ->setParameter('firstCurrency', $firstCurrencyCode)
45
            ->setParameter('secondCurrency', $secondCurrencyCode)
46
            ->getQuery()
47
            ->getOneOrNullResult()
48
        ;
49
    }
50
}
51