Completed
Push — master ( b70f30...259e2d )
by Kamil
32:06
created

CountryContext   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getCountryByName() 0 11 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\Behat\Context\Transform;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Addressing\Converter\CountryNameConverterInterface;
16
use Sylius\Component\Resource\Repository\RepositoryInterface;
17
18
/**
19
 * @author Łukasz Chruściel <[email protected]>
20
 */
21
final class CountryContext implements Context
22
{
23
    /**
24
     * @var CountryNameConverterInterface
25
     */
26
    private $countryNameConverter;
27
28
    /**
29
     * @var RepositoryInterface
30
     */
31
    private $countryRepository;
32
33
    /**
34
     * @param CountryNameConverterInterface $countryNameConverter
35
     * @param RepositoryInterface $countryRepository
36
     */
37
    public function __construct(
38
        CountryNameConverterInterface $countryNameConverter,
39
        RepositoryInterface $countryRepository
40
    ) {
41
        $this->countryNameConverter = $countryNameConverter;
42
        $this->countryRepository = $countryRepository;
43
    }
44
45
    /**
46
     * @Transform /^country "([^"]+)"$/
47
     */
48
    public function getCountryByName($countryName)
49
    {
50
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
51
        $country = $this->countryRepository->findOneBy(['code' => $countryCode]);
52
53
        if (null === $country) {
54
            throw new \InvalidArgumentException(sprintf('Country with name %s does not exist', $countryName));
55
        }
56
57
        return $country;
58
    }
59
}
60