Completed
Push — symfony3-wololo-packages ( fecf70...6bf04d )
by Kamil
28:46 queued 11:35
created

ExchangeRateRepository::findOneWithCurrencyPair()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
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
     * {@inheritdoc}
27
     *
28
     * @throws NonUniqueResultException
29
     */
30
    public function findOneWithCurrencyPair($firstCurrencyCode, $secondCurrencyCode)
31
    {
32
        $queryBuilder = $this
33
            ->createQueryBuilder('o')
34
            ->addSelect('sourceCurrency')
35
            ->leftJoin('o.sourceCurrency', 'sourceCurrency')
36
            ->addSelect('targetCurrency')
37
            ->leftJoin('o.targetCurrency', 'targetCurrency')
38
        ;
39
40
        return $queryBuilder
41
            ->where(
42
                $queryBuilder->expr()->andX(
43
                    $queryBuilder->expr()->eq('sourceCurrency.code', ':firstCurrency'),
44
                    $queryBuilder->expr()->eq('targetCurrency.code', ':secondCurrency')
45
                ))
46
            ->orWhere(
47
                $queryBuilder->expr()->andX(
48
                    $queryBuilder->expr()->eq('sourceCurrency.code', ':secondCurrency'),
49
                    $queryBuilder->expr()->eq('targetCurrency.code', ':firstCurrency')
50
                ))
51
            ->setParameter('firstCurrency', $firstCurrencyCode)
52
            ->setParameter('secondCurrency', $secondCurrencyCode)
53
            ->getQuery()
54
            ->getOneOrNullResult()
55
        ;
56
    }
57
}
58