Completed
Push — master ( f906b5...0ffad1 )
by Kamil
17s
created

CurrencyContext::theStoreHasDisabledCurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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 Sylius\Component\Core\Test\Services\SharedStorageInterface;
16
use Sylius\Component\Currency\Converter\CurrencyNameConverterInterface;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
20
/**
21
 * @author Arkadiusz Krakowiak <[email protected]>
22
 */
23
final class CurrencyContext implements Context
24
{
25
    /**
26
     * @var SharedStorageInterface
27
     */
28
    private $sharedStorage;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $currencyRepository;
34
35
    /**
36
     * @var FactoryInterface
37
     */
38
    private $currencyFactory;
39
40
    /**
41
     * @var CurrencyNameConverterInterface
42
     */
43
    private $currencyNameConverter;
44
45
    /**
46
     * @param SharedStorageInterface $sharedStorage
47
     * @param RepositoryInterface $currencyRepository
48
     * @param FactoryInterface $currencyFactory
49
     * @param CurrencyNameConverterInterface $currencyNameConverter
50
     */
51
    public function __construct(
52
        SharedStorageInterface $sharedStorage,
53
        RepositoryInterface $currencyRepository,
54
        FactoryInterface $currencyFactory,
55
        CurrencyNameConverterInterface $currencyNameConverter
56
    ) {
57
        $this->sharedStorage = $sharedStorage;
58
        $this->currencyRepository = $currencyRepository;
59
        $this->currencyFactory = $currencyFactory;
60
        $this->currencyNameConverter = $currencyNameConverter;
61
    }
62
63
    /**
64
     * @Given default currency is :currencyCode
65
     */
66
    public function defaultCurrencyIs($currencyCode)
67
    {
68
        $currency = $this->currencyFactory->createNew();
69
        $currency->setCode($currencyCode);
70
        $currency->setExchangeRate(1.0);
71
        $channel = $this->sharedStorage->get('channel');
72
        $channel->setDefaultCurrency($currency);
73
74
        $this->currencyRepository->add($currency);
75
    }
76
77
    /**
78
     * @Given the store has currency :currencyName
79
     */
80
    public function theStoreHasCurrency($currencyName)
81
    {
82
        $this->createCurrency($currencyName);
83
    }
84
85
    /**
86
     * @Given the store has disabled currency :currencyName
87
     */
88
    public function theStoreHasDisabledCurrency($currencyName)
89
    {
90
        $this->createCurrency($currencyName, false);
91
    }
92
93
    /**
94
     * @Given the store has currency :currencyName with exchange rate :exchangeRate
95
     */
96
    public function theStoreHasCurrencyWithExchangeRate($currencyName, $exchangeRate)
97
    {
98
        $this->createCurrency($currencyName, true, $exchangeRate);
99
    }
100
101
    /**
102
     * @param string $currencyName
103
     * @param bool $enabled
104
     * @param float $exchangeRate
105
     */
106
    private function createCurrency($currencyName, $enabled = true, $exchangeRate = 1.0)
107
    {
108
        $currency = $this->currencyFactory->createNew();
109
        $currency->setCode($this->currencyNameConverter->convertToCode($currencyName));
110
        $currency->setExchangeRate($exchangeRate);
111
        if (!$enabled) {
112
            $currency->disable();
113
        }
114
115
        $this->sharedStorage->set('currency', $currency);
116
        $this->currencyRepository->add($currency);
117
    }
118
}
119