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

CurrencyContext   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getCurrencyByName() 0 11 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\Transform;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Currency\Converter\CurrencyNameConverterInterface;
16
use Sylius\Component\Resource\Repository\RepositoryInterface;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * @author Anna Walasek <[email protected]>
21
 */
22
final class CurrencyContext implements Context
23
{
24
    /**
25
     * @var CurrencyNameConverterInterface
26
     */
27
    private $currencyNameConverter;
28
29
    /**
30
     * @var RepositoryInterface
31
     */
32
    private $currencyRepository;
33
34
    /**
35
     * @param CurrencyNameConverterInterface $currencyNameConverter
36
     * @param RepositoryInterface $currencyRepository
37
     */
38
    public function __construct(
39
        CurrencyNameConverterInterface $currencyNameConverter,
40
        RepositoryInterface $currencyRepository
41
    ) {
42
        $this->currencyNameConverter = $currencyNameConverter;
43
        $this->currencyRepository = $currencyRepository;
44
    }
45
46
    /**
47
     * @Transform :currency
48
     */
49
    public function getCurrencyByName($currencyName)
50
    {
51
        $currencyCode = $this->currencyNameConverter->convertToCode($currencyName);
52
        $currency = $this->currencyRepository->findOneBy(['code' => $currencyCode]);
53
        Assert::notNull(
54
            $currency,
55
            sprintf('Currency with name %s does not exist.', $currencyName)
56
        );
57
58
        return $currency;
59
    }
60
}
61