Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

ExchangeRateContext::saveExchangeRate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Behat\Service\SharedStorageInterface;
17
use Sylius\Component\Currency\Model\CurrencyInterface;
18
use Sylius\Component\Currency\Model\ExchangeRateInterface;
19
use Sylius\Component\Currency\Repository\ExchangeRateRepositoryInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
22
/**
23
 * @author Jan Góralski <[email protected]>
24
 */
25
final class ExchangeRateContext implements Context
26
{
27
    /**
28
     * @var SharedStorageInterface
29
     */
30
    private $sharedStorage;
31
32
    /**
33
     * @var FactoryInterface
34
     */
35
    private $exchangeRateFactory;
36
37
    /**
38
     * @var ExchangeRateRepositoryInterface
39
     */
40
    private $exchangeRateRepository;
41
42
    /**
43
     * @var ObjectManager
44
     */
45
    private $entityManager;
46
47
    /**
48
     * @param SharedStorageInterface $sharedStorage
49
     * @param FactoryInterface $exchangeRateFactory
50
     * @param ExchangeRateRepositoryInterface $exchangeRateRepository
51
     * @param ObjectManager $entityManager
52
     */
53
    public function __construct(
54
        SharedStorageInterface $sharedStorage,
55
        FactoryInterface $exchangeRateFactory,
56
        ExchangeRateRepositoryInterface $exchangeRateRepository,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $exchangeRateRepository exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
57
        ObjectManager $entityManager
58
    ) {
59
        $this->sharedStorage = $sharedStorage;
60
        $this->exchangeRateFactory = $exchangeRateFactory;
61
        $this->exchangeRateRepository = $exchangeRateRepository;
62
        $this->entityManager = $entityManager;
63
    }
64
65
    /**
66
     * @Given the exchange rate of :sourceCurrency to :targetCurrency is :ratio
67
     */
68
    public function thereIsAnExchangeRateWithSourceCurrencyAndTargetCurrency(
69
        CurrencyInterface $sourceCurrency,
70
        CurrencyInterface $targetCurrency,
71
        $ratio
72
    ) {
73
        $exchangeRate = $this->createExchangeRate($sourceCurrency, $targetCurrency, $ratio);
74
75
        $this->saveExchangeRate($exchangeRate);
76
    }
77
78
    /**
79
     * @param CurrencyInterface $sourceCurrency
80
     * @param CurrencyInterface $targetCurrency
81
     * @param float $ratio
82
     *
83
     * @return ExchangeRateInterface
84
     */
85
    private function createExchangeRate(CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency, $ratio = 1.00)
86
    {
87
        /** @var ExchangeRateInterface $exchangeRate */
88
        $exchangeRate = $this->exchangeRateFactory->createNew();
89
        $exchangeRate->setSourceCurrency($sourceCurrency);
90
        $exchangeRate->setTargetCurrency($targetCurrency);
91
        $exchangeRate->setRatio((float) $ratio);
92
93
        return $exchangeRate;
94
    }
95
96
    /**
97
     * @param ExchangeRateInterface $exchangeRate
98
     */
99
    private function saveExchangeRate(ExchangeRateInterface $exchangeRate)
100
    {
101
        $this->exchangeRateRepository->add($exchangeRate);
102
        $this->sharedStorage->set('exchange_rate', $exchangeRate);
103
    }
104
}
105