Completed
Push — symfony3-fqcn ( 04e8c2...ea8d6b )
by Kamil
18:56
created

ExchangeRateRepository::findOneWithCurrencyPair()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 15
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 Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
16
use Sylius\Component\Currency\Model\CurrencyInterface;
17
use Sylius\Component\Currency\Model\ExchangeRateInterface;
18
use Sylius\Component\Currency\Repository\ExchangeRateRepositoryInterface;
19
20
/**
21
 * @author Jan Góralski <[email protected]>
22
 */
23
class ExchangeRateRepository extends EntityRepository implements ExchangeRateRepositoryInterface
24
{
25
    /**
26
     * @param CurrencyInterface $firstCurrency
27
     * @param CurrencyInterface $secondCurrency
28
     *
29
     * @return ExchangeRateInterface|null
30
     *
31
     * @throws NonUniqueResultException
32
     */
33
    public function findOneWithCurrencyPair(CurrencyInterface $firstCurrency, CurrencyInterface $secondCurrency)
34
    {
35
        $queryBuilder = $this->createQueryBuilder('o');
36
37
        return $queryBuilder
38
            ->where(
39
                $queryBuilder->expr()->andX(
40
                    $queryBuilder->expr()->eq('o.sourceCurrency', ':firstCurrency'),
41
                    $queryBuilder->expr()->eq('o.targetCurrency', ':secondCurrency')
42
                ))
43
            ->orWhere(
44
                $queryBuilder->expr()->andX(
45
                    $queryBuilder->expr()->eq('o.sourceCurrency', ':secondCurrency'),
46
                    $queryBuilder->expr()->eq('o.targetCurrency', ':firstCurrency')
47
                ))
48
            ->setParameter('firstCurrency', $firstCurrency)
49
            ->setParameter('secondCurrency', $secondCurrency)
50
            ->getQuery()
51
            ->getOneOrNullResult()
52
        ;
53
    }
54
}
55